Home > Software engineering >  How to send WM_COPYDATA from C to AutoHotKey?
How to send WM_COPYDATA from C to AutoHotKey?

Time:09-18

Trying to SendMessage with WM_COPYDATA from a C application to an AutoHotkey script. I tried to follow the example found in the docs:

image

When it should be: Hello World.

I have also checked for GetLastError() after the SendMessage and it output 0.

I must be doing something wrong inside of the COPYDATASTRUCT. AutoHotkey x64.

CodePudding user response:

Your use of StrGet() is wrong:

  • You are not including the std::string's null terminator in the sent data, but you are not passing the value of the COPYDATASTRUCT::cbData field to StrGet(), so it is going to be looking for a null terminator that does not exist. So you need to specify the length that is in the COPYDATASTRUCT::cbData field, eg:

    StringLen := NumGet(lParam   A_PtrSize, "int");
    StringAddress := NumGet(lParam   2*A_PtrSize);
    Data := StrGet(StringAddress, StringLen, Encoding);
    
  • More importantly, you are not specifying an Encoding for StrGet(), so it is going to interpret the raw data in whatever the native encoding of the script is (see A_IsUnicode). Don't do that. Be explicit about the encoding used by the C code. If the std::string holds a UTF-8 string, specify "UTF-8". If the std::string holds a string in the user's default ANSI locale, specify "CP0". And so on. What you are seeing happen is commonly known as Mojibake, which happens when single-byte character data is mis-interpreted in the wrong encoding.

  • Related