I am using the following python code to send String to a RAD Studio C++ Builder built application:
JavaScript
x
26
26
1
import win32con
2
import win32gui
3
import ctypes
4
import ctypes.wintypes
5
6
FindWindow = win32gui.FindWindow
7
SendMessage = ctypes.windll.user32.SendMessageW
8
9
class COPYDATASTRUCT(ctypes.Structure):
10
_fields_ = [
11
('dwData', ctypes.wintypes.LPARAM),
12
('cbData', ctypes.wintypes.DWORD),
13
('lpData', ctypes.c_wchar_p)
14
]
15
16
hwnd = FindWindow(None, "SIGNAL")
17
cds = COPYDATASTRUCT()
18
cds.dwData = 0
19
20
mystr = "This is a message to signal..."
21
22
cds.cbData = ctypes.sizeof(ctypes.create_unicode_buffer(mystr))
23
cds.lpData = ctypes.c_wchar_p(mystr)
24
25
SendMessage(hwnd, win32con.WM_COPYDATA, 0, ctypes.byref(cds))
26
It is perfectly find the opponent application and SendMessage sends data.
My problem: It only sends the 1st character of the string. So instead of: "This is a message to signal..."
, it only sends "T"
.
I am sure that I am overlooking something trivial, but I could not found it.
Advertisement
Answer
To support all may run into the same task/issue, the working “receive” code (RAD Studio C++ builder) is:
JavaScript
1
20
20
1
void __fastcall Tmain_form::WMCopyData(TMessage& Message) // Receiving messages from Python script in C++ Builder
2
{
3
int DataSize;
4
wchar_t* Data;
5
6
COPYDATASTRUCT* CopyData = reinterpret_cast <COPYDATASTRUCT*> (Message.LParam);
7
8
DataSize = CopyData->cbData;
9
10
if (DataSize > 0)
11
{
12
Data = new wchar_t[DataSize];
13
memcpy(Data, CopyData->lpData, DataSize);
14
15
// Process your data....
16
17
delete [] Data;
18
}
19
}
20
Just to be complete, this goes to the header file, below ‘protected’:
JavaScript
1
4
1
BEGIN_MESSAGE_MAP
2
MESSAGE_HANDLER(WM_COPYDATA, TMessage, WMCopyData)
3
END_MESSAGE_MAP(TForm)
4