PostMessage from AHK to C++ Console Program

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
akaza_dorian
Posts: 9
Joined: 30 Nov 2019, 15:44

PostMessage from AHK to C++ Console Program

Post by akaza_dorian » 28 Aug 2022, 15:13

Hi all, I'm trying to use AHKDLL to replace a few OS hotkeys for my C++ program, and prior to that I was testing if PostMessage works with standalone AHK sending message to the C++ side. The problem is that, it just doesn't work, no message being received by the C++ side at all. Here are my codes:

C++ receiver:

Code: Select all

int main()
{
    cout << "PID = " << GetCurrentProcessId() << endl;

    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {
        cout << "Received a message, wParam = " << msg.wParam << endl;
    }
}
AHK sender:

Code: Select all

^n::
  PostMessage, WM_APP, 1, 2, , ahk_pid [put the random PID given in cout of the receiver here]
return
I'm not sure if it due to some limitation of Windows, or a problem of my configuration. If this approach will not work, a better way to achieve so would be greatly appreciated! The only job of the AHK side is to let the C++ side know when a hotkey has been pressed.

Note that the C++ side is a console program which does not offer a GUI.

Thank you in advance!

teadrinker
Posts: 4411
Joined: 29 Mar 2015, 09:41
Contact:

Re: PostMessage from AHK to C++ Console Program

Post by teadrinker » 28 Aug 2022, 15:43

You can send/post messages to a window, not to a process. If process has no windows, it can't handle messages.

akaza_dorian
Posts: 9
Joined: 30 Nov 2019, 15:44

Re: PostMessage from AHK to C++ Console Program

Post by akaza_dorian » 28 Aug 2022, 18:33

teadrinker wrote:
28 Aug 2022, 15:43
You can send/post messages to a window, not to a process. If process has no windows, it can't handle messages.
Thank you for the info! In that case, what will be the recommended way to achieve that? My two ideas were
1. C++ register some other "complicated" hotkeys and let AHK to send it out: easy to implement but possibly a waste in hotkeys (not sure if it will be slower than a regular windows message)
2. Named pipe: seems relatively more resource consuming while busy waiting for the pipe writes

teadrinker
Posts: 4411
Joined: 29 Mar 2015, 09:41
Contact:

Re: PostMessage from AHK to C++ Console Program

Post by teadrinker » 28 Aug 2022, 19:15

Unfortunately, I don't write in C++, so I'm not sure I can give the best advice. Maybe someone else can help.

akaza_dorian
Posts: 9
Joined: 30 Nov 2019, 15:44

Re: PostMessage from AHK to C++ Console Program

Post by akaza_dorian » 29 Aug 2022, 08:29

teadrinker wrote:
28 Aug 2022, 19:15
Unfortunately, I don't write in C++, so I'm not sure I can give the best advice. Maybe someone else can help.
No worries! Still, thank you for the help!

Post Reply

Return to “Ask for Help (v1)”