convert c++ to ahk Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

convert c++ to ahk  Topic is solved

18 Aug 2018, 08:22

Could anyone help me to convert the following program C++ to AHK?

Code: Select all

/*
 * Compiled using:
 *     g++ main.cpp
 */

#include <windows.h>
#include <commctrl.h>
#include <iostream>

int main(void) {
    HWND hTrayWnd = FindWindowEx(
        FindWindowEx(
            FindWindowEx(
                FindWindow("Shell_TrayWnd", NULL)
            , NULL, "TrayNotifyWnd", NULL)
        , NULL, "SysPager", NULL)
    , NULL, "ToolbarWindow32", NULL);

    int buttonCount = SendMessage(hTrayWnd, TB_BUTTONCOUNT, 0, 0);
    
    DWORD trayPid;
    GetWindowThreadProcessId(hTrayWnd, &trayPid);
    HANDLE hTrayProcess = OpenProcess(PROCESS_ALL_ACCESS, false, trayPid);
    
    TBBUTTON buttonData = {0};
    LPVOID pButtonDataBuffer = VirtualAllocEx(hTrayProcess, NULL, sizeof(buttonData), MEM_COMMIT, PAGE_READWRITE);
    char buttonText[1024] = {0};
    LPVOID pButtonTextDataBuffer = VirtualAllocEx(hTrayProcess, NULL, sizeof(buttonText), MEM_COMMIT, PAGE_READWRITE);
    
    for(int i=0; i<buttonCount; ++i) {
        SendMessage(hTrayWnd, TB_GETBUTTON, i, (LPARAM)pButtonDataBuffer);
        ReadProcessMemory(hTrayProcess, pButtonDataBuffer, &buttonData, sizeof(buttonData), NULL);
        SendMessage(hTrayWnd, TB_GETBUTTONTEXT, buttonData.idCommand, (LPARAM)pButtonTextDataBuffer);
        ReadProcessMemory(hTrayProcess, pButtonTextDataBuffer, &buttonText, sizeof(buttonText), NULL);
        
        if (!strcmp(buttonText, "Click to turn off English mode")) { 
            std::cout << "English mode detected" << std::endl;
        } else if (!strcmp(buttonText, "Click to turn off Hangul mode")) { 
            std::cout << "Hangul mode detected" << std::endl;
        }
    }
    return 0;
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: convert c++ to ahk

18 Aug 2018, 08:48

Sorry for the messy code:

Code: Select all

Shell_TrayWnd := DllCall("User32.dll\FindWindowW", "Str", "Shell_TrayWnd", "Ptr", 0, "Ptr")
TrayNotifyWnd := DllCall("User32.dll\FindWindowExW", "Ptr", Shell_TrayWnd, "Ptr", 0, "Str", "TrayNotifyWnd", "Ptr", 0, "Ptr")
SysPager := DllCall("User32.dll\FindWindowExW", "Ptr", TrayNotifyWnd, "Ptr", 0, "Str", "SysPager", "Ptr", 0, "Ptr")
hTrayWnd := DllCall("User32.dll\FindWindowExW", "Ptr", SysPager, "Ptr", 0, "Str", "ToolbarWindow32", "Ptr", 0, "Ptr")

buttonCount := DllCall("User32.dll\SendMessageW", "Ptr", hTrayWnd, "UInt", 0x418, "Ptr", 0, "Ptr", 0)

trayPid := 0
DllCall("User32.dll\GetWindowThreadProcessId", "Ptr", hTrayWnd, "UIntP", trayPid)

hTrayProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0008|0x0010, "Int", FALSE, "UInt", trayPid, "Ptr")

VarSetCapacity(TBBUTTON, sizeof_TBBUTTON := A_PtrSize==4?20:32)
pButtonDataBuffer := DllCall("Kernel32.dll\VirtualAllocEx", "Ptr", hTrayProcess, "UPtr", 0, "UPtr", sizeof_TBBUTTON, "UInt", 0x1000, "UInt", 4, "UPtr")

VarSetCapacity(Buffer, 1024)
pButtonTextDataBuffer := DllCall("Kernel32.dll\VirtualAllocEx", "Ptr", hTrayProcess, "UPtr", 0, "UPtr", 1024, "UInt", 0x1000, "UInt", 4, "UPtr")

Loop % (buttonCount)
{
    DllCall("User32.dll\SendMessageW", "Ptr", hTrayWnd, "UInt", 0x417, "Ptr", A_Index-1, "Ptr", pButtonDataBuffer)
    DllCall("Kernel32.dll\ReadProcessMemory", "Ptr", hTrayProcess, "UPtr", pButtonDataBuffer, "UPtr", &TBBUTTON, "UPtr", sizeof_TBBUTTON, "UPtr", 0)
    DllCall("User32.dll\SendMessageW", "Ptr", hTrayWnd, "UInt", 0x44B, "Ptr", NumGet(&TBBUTTON+4,"Int"), "Ptr", pButtonTextDataBuffer)
    DllCall("Kernel32.dll\ReadProcessMemory", "Ptr", hTrayProcess, "UPtr", pButtonTextDataBuffer, "UPtr", &Buffer, "UPtr", 1024, "UPtr", 0)

    Text := StrGet(&Buffer, "UTF-16")
    if (InStr(Text, "Click to turn off English mode"))
        MsgBox "English mode detected"
    else if (InStr(Text, "Click to turn off Hangul mode"))
        MsgBox "Hangul mode detected"
}

DllCall("Kernel32.dll\VirtualFreeEx", "Ptr", hTrayProcess, "UPtr", pButtonTextDataBuffer, "UPtr", 0, "UInt", 0x8000)

DllCall("Kernel32.dll\VirtualFreeEx", "Ptr", hTrayProcess, "UPtr", pButtonDataBuffer, "UPtr", 0, "UInt", 0x8000)
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: convert c++ to ahk

18 Aug 2018, 09:28

cool! Thank you very much Flipeador! I didn't expect it is that fast.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: convert c++ to ahk

18 Aug 2018, 09:46

You're welcome. :)
:wave:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jollyjoe, just me, Rohwedder and 303 guests