First the big picture: I'm trying to synchronize two external devices through a console program on Windows 7 (64-bit). That is, Device 1 should trigger Device 2. (For the curious, Device 1 is the NI USB-6525, and I'm using its Change Detection function.)
I'm using GetMessage() in a while loop to wait for a message that's triggered by Device 1. (Since I didn't write the software for that hardware, I can't change the fact that I have to read this message.) Once that message is read, it is dispatched. This dispatch results in a callback to a function that performs a measurement using Device 2 and sets measurementComplete to true
. Once the callback returns, the loop ends. Then I perform cleanup and the application exits.
The problem is, the user should also be able to abort while waiting for the message, for example by pressing a key. I tried to implement a check to see whether the received message came from the other thread or the keyboard, but it never recognizes keyboard input:
#include <cstdio>
#include <windows.h>
using namespace std;
bool measurementComplete = false;
BOOL bRet = 0;
MSG threadMessage;
signed long __cdecl callbackFunction(type1 param1, type2 param2) // (pseudo args)
{
measurementComplete = 1;
performMeasurement();
return 0;
}
int main (int argc, char* argv[])
{
/* Load libraries, set up hardware,
call NI function that looks for signal from Device 1 in separate thread
and sends a Windows message upon signal detection */
while (!measurementComplete) { // measurement has not yet been performed
// wait for message
puts("Waiting for message.");
if ((bRet = GetMessage(&threadMessage, NULL, 0, 0)) != 0) { // if message available
if (bRet == -1) {
puts("Error: GetMessage() returned -1. The program will now exit.");
break;
} else {
DispatchMessage(&threadMessage);
if ((TranslateMessage(&threadMessage)) != 0) // if character message (indicates key press)
break;
}
}
puts("Message handled.");
}
/* perform cleanup */
return 0;
}
I'm new to the Windows API, so I'm not that familiar with it. I'm programming in the Code::Blocks 13.12 IDE and using the GCC. I don't have MFC or any paid products like that from Microsoft, so I can't use MFC functions or classes. Unfortunately, many of the answers I found to similar questions included MFC functions.
From my research on this issue, it seems that the keyboard messages might not have a window to go to. I tried to create a message-only window such as described here, but I always get Error 18: There are no more files
upon calling CreateWindowEx(). I can provide that code if requested, but I'm not even sure that I really need to create a window. When I run FindWindowEx(HWND_MESSAGE, NULL, NULL, NULL);
, I can see that such a window already exists. I don't know whether the window that was found by that function is one that was somehow automatically created by my binary or whether it's a message-only window that was created by another program running on my computer. Besides, don't I already have a window (the console window)?
Does anyone have any tips for how to direct keyboard input to the messaging system in my console application? Any help would be greatly appreciated.