Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Need help updating a function from AHK_Basic to AHK_L


  • Please log in to reply
4 replies to this topic
gummby8
  • Members
  • 120 posts
  • Last active: Nov 30 2018 06:35 PM
  • Joined: 05 Jul 2009

I have this function, and I am just trying to update it to work in unicode rather than ansi 32. It is the one function in my overall program that relies on ansi 32 and I don't like that :)

;// AHK Basic 1.0.48.05

Process =        ;// blank to use our script's process
DllName = user32.dll

Process, Exist, %Process%
PID = %ErrorLevel%

SetFormat, Integer, Hex

Base := GetDllBase(DllName, PID)
MsgBox, Base = %Base%
Msgbox, PID = %PID%
MsgBox, % ListAllLoadedModules(PID)

Return




GetDllBase(DllName, PID = 0)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32First", "Uint", snapMod, "Uint", &me32)){
        while(DllCall("Module32Next", "Uint", snapMod, "UInt", &me32)) {
            If !DllCall("lstrcmpi", "Str", DllName, "UInt", &me32 + 32) {
                DllCall("CloseHandle", "UInt", snapMod)
                Return NumGet(&me32 + 20)
            }
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return 0
}









ListAllLoadedModules(PID)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32First", "Uint", snapMod, "Uint", &me32)){
        list := StrGet(&me32 + 32) . "`n"
        while(DllCall("Module32Next", "Uint", snapMod, "UInt", &me32)) {
            list .= StrGet(&me32 + 32) . "`n"
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return list
}





StrGet(Address, Length=-1, Encoding=0)
{
    if A_IsUnicode
    {
        MsgBox %A_ThisFunc% does not support Unicode. Use the AutoHotkey_L (revision 46+) built-in version instead. The script will now exit.
        ExitApp
    }
    
    ; Flexible parameter handling:
    if Length is not integer
        Encoding := Length,  Length := -1
    
    ; Check for obvious errors.
    if (Address+0 < 1024)
        return
    
    ; Ensure 'Encoding' contains a numeric identifier.
    if Encoding = UTF-16
        Encoding = 1200
    else if Encoding = UTF-8
        Encoding = 65001
    else if SubStr(Encoding,1,2)="CP"
        Encoding := SubStr(Encoding,3)
    
    if !Encoding ; "" or 0
    {
        ; No conversion necessary, but we might not want the whole string.
        if (Length == -1)
            Length := DllCall("lstrlen", "uint", Address)
        VarSetCapacity(String, Length)
        DllCall("lstrcpyn", "str", String, "uint", Address, "int", Length + 1)
    }
    else if Encoding = 1200 ; UTF-16
    {
        char_count := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "uint", 0, "uint", 0, "uint", 0, "uint", 0)
        VarSetCapacity(String, char_count)
        DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "str", String, "int", char_count, "uint", 0, "uint", 0)
    }
    else if Encoding is integer
    {
        ; Convert from target encoding to UTF-16 then to the active code page.
        char_count := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", Address, "int", Length, "uint", 0, "int", 0)
        VarSetCapacity(String, char_count * 2)
        char_count := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", Address, "int", Length, "uint", &String, "int", char_count * 2)
        String := StrGet(&String, char_count, 1200)
    }
    return String
}


noname
  • Members
  • 650 posts
  • Last active:
  • Joined: 12 Nov 2011
Process =        ;// blank to use our script's process
DllName = user32.dll

Process, Exist, %Process%
PID = %ErrorLevel%

SetFormat, Integer, Hex

Base := GetDllBase(DllName, PID)
MsgBox, Base = %Base%
Msgbox, PID = %PID%
MsgBox, % ListAllLoadedModules(PID)

Return




GetDllBase(DllName, PID = 0)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32First", "Uint", snapMod, "Uint", &me32)){
        while(DllCall("Module32Next", "Uint", snapMod, "UInt", &me32)) {
            If !DllCall("lstrcmpi", "Str", DllName, "UInt", &me32 + 32) {
                DllCall("CloseHandle", "UInt", snapMod)
                Return NumGet(&me32 + 20)
            }
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return 0
}







ListAllLoadedModules(PID)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32First", "Uint", snapMod, "Uint", &me32)){
        list := StrGet(&me32 + 32,(A_IsUnicode ? "utf-8" : "cp0")) . "`n"
        while(DllCall("Module32Next", "Uint", snapMod, "UInt", &me32)) {
            list .= StrGet(&me32 + 32,(A_IsUnicode ? "utf-8" : "cp0")) . "`n"
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return list
}

winXP  and ahk unicode


gummby8
  • Members
  • 120 posts
  • Last active: Nov 30 2018 06:35 PM
  • Joined: 05 Jul 2009

...snip

 

 

Unfortunately that does not seem to work. I deleted my autohotkey folder. Re-installed autohotkey using express settings and the script always returns 0. I should have specified that it was this section that most likely needs the updating

GetDllBase(DllName, PID = 0)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32First", "Uint", snapMod, "Uint", &me32)){
        while(DllCall("Module32Next", "Uint", snapMod, "UInt", &me32)) {
            If !DllCall("lstrcmpi", "Str", DllName, "UInt", &me32 + 32) {
                DllCall("CloseHandle", "UInt", snapMod)
                Return NumGet(&me32 + 20)
            }
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return 0
}


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
If you are running the unicode version of AutoHotkey (which is installed by default), you need to append a W to your Module32 calls.
Module32FirstW
Module32NextW
ect.
That may also be the case for some of the other DllCalls you are using.

See http://tinyurl.com/monrs48

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


gummby8
  • Members
  • 120 posts
  • Last active: Nov 30 2018 06:35 PM
  • Joined: 05 Jul 2009

If you are running the unicode version of AutoHotkey (which is installed by default), you need to append a W to your Module32 calls.

Module32FirstW
Module32NextW
ect.
That may also be the case for some of the other DllCalls you are using.

See http://tinyurl.com/monrs48

 

 

Unfortunately it still does not work. I always get a return of 0 when using unicode.

GetDllBase(DllName, PID = 0)
{
    TH32CS_SNAPMODULE := 0x00000008
    INVALID_HANDLE_VALUE = -1
    VarSetCapacity(me32, 548, 0)
    NumPut(548, me32)
    snapMod := DllCall("CreateToolhelp32Snapshot", "Uint", TH32CS_SNAPMODULE
                                                 , "Uint", PID)
    If (snapMod = INVALID_HANDLE_VALUE) {
        Return 0
    }
    If (DllCall("Module32FirstW", "Uint", snapMod, "Uint", &me32)){
        while(DllCall("Module32NextW", "Uint", snapMod, "UInt", &me32)) {
            If !DllCall("lstrcmpi", "Str", DllName, "UInt", &me32 + 32) {
                DllCall("CloseHandle", "UInt", snapMod)
                Return NumGet(&me32 + 20)
            }
        }
    }
    DllCall("CloseHandle", "Uint", snapMod)
    Return 0
}