Reputation: 77
I have a C# console application where I'm using CEN XFS(Extensions for Financial Services) specification to get ATM information. I'm using PInvoke to communicate with msxfs.dll. I'm registering to listen for SERVICE_EVENTS, USER_EVENTS, SYSTEM_EVENTS, EXECUTE_EVENTS invoking method
HRESULT WFSRegister(hService, dwEventClass, hWndReg)
In my code I create an instance of WNDCLASSEX assign a method that will process Windows Messages to its lpfnWndProc field; then I register a class using RegisterClassEx([In] ref WNDCLASSEX lpWndClass) and create a Window calling CreateWindowEx.
In my code later I execute WFS_CMD_CDM_SET_CASH_UNIT_INFO command and it returns WFS_SUCCESS, base on the documentation this command will generate WFS_SRVE_CDM_CASHUNITINFOCHANGED service event but I'm not getting any window message with value 304 (#define WFS_SRVE_CDM_CASHUNITINFOCHANGED (CDM_SERVICE_OFFSET + 4)) or any message related with XFS Events.
It is possible that the msxfs.dll or Service Providers I'm using does not have events notification implemented. Am I missing something that prevents me to get windows messages?
Upvotes: 0
Views: 752
Reputation: 1613
In order to receive window messages you must have a separate thread which is running the equivalent of the standard Windows message pump:
MSG msg = { 0 };
BOOL bRet;
while ((bRet = GetMessage(&msg, hWnd, 0, 0)) != 0) {
if (bRet == -1) {
// handle the error and possibly exit
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Furthermore, it is not necessary to do all this via P/Invoke — you can do it in C# natively by creating a hidden form and overriding WndProc
. You still have to create and run that form on a separate thread, and to use Invoke
when you need to close it.
Note that for the form to be fully hidden in addition to setting the FormBorderStyle
to None
, ShowIcon
to false
, ShownInTaskbar
to false, Visible
to false
, and WindowState
to Minimized
, you should also change the Extended Window Styles to include WS_EX_TOOLWINDOW
during form creation:
protected override CreateParams CreateParams {
get {
var Params = base.CreateParams;
Params.ExStyle |= WS_EX_TOOLWINDOW;
return Params;
}
}
Finally, note that the WFSRESULT
pointer which you get via lParam
in WndProc
when you receive an event needs to be freed using WFSFreeResult()
. Inspecting the Buffer
member of the WFSRESULT
structure and marshaling any nested structures returned in it needs to be completed before calling WFSFreeResult()
or the data will be invalid.
Upvotes: 0