Page 1 of 1

Memory error with this code

Posted: 15 Aug 2019, 04:13
by think
I get a memory error if running this code with AHK_H. Any idea?
Display a list of fonts on the system: https://www.autohotkey.com/boards/viewtopic.php?t=14838

Code: Select all

#NoEnv
#Warn

Gui Add, ListView, x8 y8 w273 h362, Font name
LV_ModifyCol(1, 250)

Global FontList := []

EnumFonts() {
    hDC := DllCall("GetDC", "UInt", DllCall("GetDesktopWindow"))
    Callback := RegisterCallback("EnumFontsCallback", "F")
    DllCall("EnumFontFamilies", "UInt", hDC, "UInt", 0, "Ptr", Callback, "UInt", lParam := 0)
    DllCall("ReleaseDC", "UInt", hDC)
}

EnumFontsCallback(lpelf) {
    FontList.Push(StrGet(lpelf + 28, 32))
    Return True
}

EnumFonts()

Loop % FontList.MaxIndex() {
	LV_Add("", FontList[A_Index])
}

Gui Show, w290 h379, Fonts
Return

GuiEscape:
GuiClose:
    ExitApp

Re: Memory error with this code

Posted: 16 Aug 2019, 14:23
by HotKeyIt
As far as I understand you need to use all parameters for callback:

Code: Select all

#Warn

Gui Add, ListView, x8 y8 w273 h362, Font name
LV_ModifyCol(1, 250)

Global FontList := []

EnumFonts() {
    hDC := DllCall("GetDC", "UInt", DllCall("GetDesktopWindow"))
    Callback := RegisterCallback("EnumFontsCallback", "F")
    DllCall("EnumFontFamilies", "UInt", hDC, "UInt", 0, "Ptr", Callback, "UInt", lParam := 0)
    DllCall("ReleaseDC", "UInt", hDC)
}

EnumFontsCallback(lpelf, lpntm, FontType, lParam) {
    FontList.Push(StrGet(lpelf + 28,32))
    Return True
}

EnumFonts()

Loop % FontList.MaxIndex() {
	LV_Add("", FontList[A_Index])
}

Gui Show, w290 h379, Fonts
Return

GuiEscape:
GuiClose:
    ExitApp