Hello,
I'm trying to write a program that passes Windows messages back and forth from another program that controls a laboratory instrument. I'm in a bit over my head but with the help of the manual I was able to write a program that successfully passes instructions, as evidenced by the instrument doing what I tell it. However, I am having trouble getting a return status from the instrument. The manual instructs the following:
I can't tell if my problem is in generating the replyTag, getting the HWND to my own console window, or the actual receiving part of the code.// demo code, etc.
// send message to the instrument operating software here...
SendMessage(hwnd, WM_COPYDATA, tag, (LPARAM) &cd)
Either a completion message or return data is returned. Remote commands ReturnStatus,
ReturnTiming, and ReturnData return data. In either case, data is received through an
asynchronous windows message inside Win32 COPYDATASTRUCT type data packet.
For example, a typical OnCopyData window callback is shown below, where the string
data retrieved is finally stored into a Microsoft CString object. Note the use of variable
replyTag, discussed above, which is used to isolate the correct windows message returned.
BOOL CUserDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* cd)
{
….
if (cd->dwData == replyTag)
{
/* String pointing to status */
CString retStatus = (char*) cd->lpData;
}
….
}
When setting the replyTag, the manual instructs: UINT replyTag = RegisterWindowMessage(“SOFTMaxProReplyMsg”);. However, I have to put an "L" in front of the string or I get a data type error (can't convert const char* to LPCWSTR).
When setting a HWND for myself, the manual instructs: HWND MyWnd = GetSafeHwnd().
That produces an error because GetSafeHwnd is a function of the Cwnd class, and I don't have a Cwnd. I have replaced it with HWND MyWnd = GetConsoleWindow();
When listening for the reply message, the manual instructs what I quoted above. However, I again don't have a Cwnd. I therefore simply used
if (cd.dwData == replyTag)
{
CString retStatus = (char*) cd.lpData;
}
The above if statement always evaluates false, and the cd.lpdata contains the message that I had sent out instead of a reply message. If anyone could help me figure out how to get a reply using my console application, I would really appreciate it. Here is the full code of my function:
Code:#include "stdafx.h" #include<iostream> #include<vector> #include<string> #include<afxwin.h> using namespace std; void SendCommand(string command) { // Get tags to identify the receiving and sending messages UINT tag = RegisterWindowMessage(L"SOFTMaxProMsg"); UINT replyTag = RegisterWindowMessage(L"SOFTMaxProReplyMsg"); // Get the window handles for the receiving software and my program HWND hwnd = FindWindowA("SOFTMaxProMainWnd", "SoftMax Pro"); HWND MyWnd = GetConsoleWindow(); // convert the function's parameter appropriately for a COPYDATASTRUCT char *cmdStr = (char*) command.c_str(); char *msgStr = _strdup(cmdStr); // Generate the COPYDATASTRUCT to send COPYDATASTRUCT cd; cd.dwData = (DWORD) MyWnd; cd.cbData = strlen(msgStr)+1; cd.lpData = msgStr; // Send message to the receving software SendMessage(hwnd, WM_COPYDATA, tag, (LPARAM) &cd); // Message gets successfully sent. // Attempt to receive message (not successful and don't know why). if (cd.dwData == replyTag) { cout << "here" << endl; // String pointing to status // CString retStatus = (char*) cd.lpData; cout << retStatus << endl; } else { cout << "else" << endl; cout << (char*) cd.lpData; } }