Trying to SendMessage with WM_COPYDATA from a C application to an AutoHotkey script.
I tried to follow the example found in the docs:
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 theCOPYDATASTRUCT::cbDatafield toStrGet(), 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 theCOPYDATASTRUCT::cbDatafield, 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
EncodingforStrGet(), so it is going to interpret the raw data in whatever the native encoding of the script is (seeA_IsUnicode). Don't do that. Be explicit about the encoding used by the C code. If thestd::stringholds a UTF-8 string, specify"UTF-8". If thestd::stringholds 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.

