Converting PostMessage code from c++ to ahk Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

Converting PostMessage code from c++ to ahk

Post by fabricio234 » 23 Sep 2021, 15:30

Trying to translate this c++ code to autohotkey:

Code: Select all

    
    HWND hwnd = FindWindow(nullptr, L"Administrador: C:\\Windows\\System32\\cmd.exe");
    int i = 0;
    for (i = 0 ; i < msg.length() ; i++)
         PostMessageW(hwnd, WM_CHAR, (int)msg[i], 0);
    
	PostMessageW(hwnd, WM_KEYDOWN, 13, 28*65536+1);
I tried:

Code: Select all


      Run, %comspec% /k cd %path% ,,, PID
      
      WinWait, ahk_pid %PID%
      WinGetTitle, Title, ahk_pid %PID%
      WinGet, hwnd, Id, %Title%
       
      FileAppend, hwnd: %hwnd%.`n,*
      
      Msg := "Hello World"
      Msg := StrSplit(Msg)

      for each, string in Msg {
         PostMessage, WM_CHAR:=0x0102, %string%, 0,, ahk_id %hwnd%
         FileAppend, ErrorLevel: %ErrorLevel% `n,*
      }
    
      ; 13 VK_ENTER
      PostMessage, WM_KEYDOWN:=0x0100, 13, 28*65536+1,, ahk_id %hwnd%

The second postmessage is working the first dont, errorlevel returns 0.

* Testing both codes in the same window, same msg, the c++ is one working correctly.
* Testing on a cmd window.

I must be doing something wrong in this line:

Code: Select all

%string%, 0,, ahk_id %hwnd%
Last edited by fabricio234 on 23 Sep 2021, 15:45, edited 4 times in total.

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Converting PostMessage code from c++ to ahk

Post by mikeyww » 23 Sep 2021, 15:37

If you haven't defined hwnd, then I don't expect much to happen.

fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

Re: Converting PostMessage code from c++ to ahk

Post by fabricio234 » 23 Sep 2021, 15:42

The hwnd is defined. I forgot to include it in the example, gonna edit it.
i also confirmed that the hwnd is correctly, and represents the current window I'm trying to send the postmessage.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Converting PostMessage code from c++ to ahk

Post by boiler » 23 Sep 2021, 15:49

Getting the hwnd using the title as a middleman isn’t a great method (even though you verified it worked this time) since the title can be ambiguous. Better to do this:

Code: Select all

WinWait, ahk_pid %PID%
WinGet, hwnd, ID, ahk_pid %PID%

or:

Code: Select all

WinWait, ahk_pid %PID%
hwnd := WinExist("ahk_pid" PID)

To send a string as a wParam or lParam, it needs to be the address of the variable holding the string using &string (or a quoted literal string), not %string%. See the PostMessage / SendMessage Remarks.

fabricio234
Posts: 122
Joined: 06 Mar 2020, 21:48

Re: Converting PostMessage code from c++ to ahk  Topic is solved

Post by fabricio234 » 23 Sep 2021, 16:50

Sending

Code: Select all

string := "hello world"
PostMessage, 0x0102,  &string, 0,, ahk_id %hwnd%

PostMessage, 0x0102, "hello world", 0,, ahk_id %hwnd%
I received on the cmd window:
>駨駨駨駨駨駨駨駨駨駨駨
'駨駨駨駨駨駨駨駨駨駨駨' is not recognized as an internal or external command, operable program or batch file
Also tested using a notepad, it does receive chinese characters.



-SOLVED-

Code: Select all

PostMessage, 0x0102, Asc(string), 0,, ahk_id %hwnd%

Post Reply

Return to “Ask for Help (v1)”