I made a project that creates a DLL. This project uses the WFS methods and they access some hardware (devices) to get information or execute some commands.
In my project, I first open these devices then register them, I later use other methods to get information or execute.
HRESULT extern WINAPI WFSOpen ( LPSTR lpszLogicalName, HAPP hApp, LPSTR lpszAppID, DWORD dwTraceLevel, DWORD dwTimeOut, DWORD dwSrvcVersionsRequired, LPWFSVERSION lpSrvcVersion, LPWFSVERSION lpSPIVersion, LPHSERVICE lphService);
HRESULT extern WINAPI WFSRegister ( HSERVICE hService, DWORD dwEventClass, HWND hWndReg);
As you can see, the WFSRegister requires HWND as a parameter. WFSRegister uses this parameter to send events or messages to it.
My project is not an MFC project and I have no windows. I decided to create a window and assign the correct HWND to WFSRegister. I also created WndProc to get the messages that the WFS methods will send to me later.
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WFS_EXECUTE_EVENT:
cout<<"WFS_EXECUTE_EVENT";
break;
case WFS_SERVICE_EVENT:
cout<<"WFS_EXECUTE_EVENT";
break;
case WFS_USER_EVENT:
cout<<"WFS_USER_EVENT";
break;
case WFS_SYSTEM_EVENT:
cout<<"WFS_SYSTEM_EVENT";
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam );
}
void Init_Window()
{
WNDCLASS Wclass;
Wclass.hInstance = gHinstance;
Wclass.cbClsExtra = 0;
Wclass.cbWndExtra = 0;
Wclass.lpszClassName = TEXT("Device_Manager_Class_Name");
Wclass.lpszMenuName = NULL;
Wclass.lpfnWndProc = WndProc;
Wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
Wclass.hCursor = LoadIcon(NULL, IDC_ARROW);
Wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Wclass.style = CS_OWNDC;
if(!RegisterClass(&Wclass))
{
cout<<"Unable to Register Class";
}
ULONG Window_Width;
ULONG Window_Height;
DWORD style;
Window_Width = SCREEN_WIDTH;
Window_Height = SCREEN_HEIGHT;
style = WS_OVERLAPPED|WS_SYSMENU;
gHwnd = CreateWindow(TEXT("Device_Manager_Class_Name")
, TEXT("Device_Manager_Class_Title")
, style
, 0
, 0
, Window_Width
, Window_Height
, GetDesktopWindow()
, NULL
, gHinstance
, NULL);
if(!gHwnd){
cout<<"Unable to create the main window";
}
ShowWindow(gHwnd, SW_SHOW);
UpdateWindow(gHwnd);
SetFocus(gHwnd);
}
Init_Window() Successfully create window, I have no prob here.
When I want to register my device, I call the following code to get the correct HWND:
HWND windows_handle = FindWindow(TEXT("Device_Manager_Class_Name"), 0);
HRESULT result = WFSRegister(wfsRes.hService, WFS_EXECUTE_EVENT || WFS_SERVICE_EVENT || WFS_USER_EVENT || WFS_SYSTEM_EVENT , windows_handle);
result is S_OK(meaning the device registered successfully) and windows_handle refers to the same HWND I created in Init_Window(). For example, both have 0x00100a58 values.
Now I change some property on my devices and I expect to get these message on my WndProc(), but it's not working.
WndProc() working somehow and gets some messages, but not the ones I want (not the ones the devices send to it).
I'm sure the devices send message (as events) because I can see they do by reading their logs.
For example:
2013/09/25 16:46:29 HService : 44 Event WFS_SRVE_SIU_PORT_STATUS Sent for HWND = 330d1c hResult = WFS_SUCCESS
HWND in the log refers to same HWND I created in Init_Window() and windows_handle.
Also, you all got what I want to do. If you have any other solution please feel free to mention it.
I found solution thanks to dear Igor Tandetnik
All i needed to do is to add GetMessage()
MSG msg;
BOOL bRet;
HWND windows_handle = FindWindow(TEXT("Device_Manager_Class_Name"), 0);
while( (bRet = GetMessage( &msg, windows_handle, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg); //<< This line send msg to WndProc()
}
}
Related
I wish to handle mouse clicks in my game with the Raw Input API using the WM_INPUT messages.
I'm only interested in the mouse clicks while my app is active.
When the window is inactive and I click into the window putting it in focus no WM_INPUT message is generated for that click. I can handle every other click except the one that brings my window to focus.
With the RIDEV_INPUTSINK flag I can catch that one missing event but then I would have to somehow ignore every other message that I get while my app is inactive.
Most apps and windowed programs process the "activating click" just like a normal one, they don't ignore it. They probably use the "legacy" messages, as they arrive correctly.
Should I mix raw input with the legacy mouse handling? Should I use INPUTSINK flag and ignore messages selectively? If so, how?
Since Microsoft recommends raw input for games, I hope there is a common solution to this problem that I just couldn't find so far.
Relevant snippet from code:
Registering the raw input device
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = 0; // adds HID mouse
Rid[0].hwndTarget = 0;
RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))
For convenience here's a full sample program that showcases my problem.
#include <windows.h>
#include <strsafe.h>
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_INPUT:
{
UINT dwSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize,
sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{
return 0;
}
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize)
OutputDebugString(TEXT("GetRawInputData does not return correct size !\n"));
RAWINPUT* raw = (RAWINPUT*)lpb;
size_t const cchDest = 1000;
TCHAR pszDest[cchDest];
if (raw->header.dwType == RIM_TYPEMOUSE && raw->data.mouse.ulButtons)
{
auto hResult = StringCchPrintf(pszDest, STRSAFE_MAX_CCH, TEXT("Mouse: usFlags=%04x ulButtons=%04x usButtonFlags=%04x usButtonData=%04x ulRawButtons=%04x lLastX=%04x lLastY=%04x ulExtraInformation=%04x\r\n"),
raw->data.mouse.usFlags,
raw->data.mouse.ulButtons,
raw->data.mouse.usButtonFlags,
raw->data.mouse.usButtonData,
raw->data.mouse.ulRawButtons,
raw->data.mouse.lLastX,
raw->data.mouse.lLastY,
raw->data.mouse.ulExtraInformation);
if (FAILED(hResult))
{
// TODO: write error handler
}
OutputDebugString(pszDest);
}
delete[] lpb;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = 0; // adds HID mouse
Rid[0].hwndTarget = 0;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
//registration failed. Call GetLastError for the cause of the error
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
UPDATE
For now I found a workaround by mixing legacy and raw input. In the WM_ACTIVATE message I check if the wParam is 2. If so it means that the window was activated using a mouse click.
In this case I actually process the next WM_LBUTTONDOWN, but only one time (and the other buttons as well).
This is a terrible hack so I'm still looking for a proper solution.
I have been trying to make a universal DLL to where I can inject it into a process & a window would pop up. I have all required functions such as WinMain (I named mine Initiate and called it manually), DllWindowProcedure, and DllMain. However, after compilation, there is one warning and no errors - I believe it is a logical error. If you spot any issues, or anything I can improve, please let me know!
Warning:
1>Main.cpp(43): warning C4060: switch statement contains no 'case' or 'default' labels
Code:
#include <Windows.h>
HINSTANCE InjectedModuleHandle;
LRESULT CALLBACK DllWindowProcedure(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI DllThreadProcedure(void * Data)
{
MessageBoxA(NULL, (LPCSTR)"Welcome to Flames v1.1!", (LPCSTR)"Startup Message", NULL);
MSG Messages;
WNDCLASSEX WindowClass;
WindowClass.hInstance = InjectedModuleHandle;
WindowClass.lpszClassName = L"DllWindowClass";
WindowClass.lpfnWndProc = DllWindowProcedure;
WindowClass.style = CS_DBLCLKS;
WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WindowClass.lpszMenuName = NULL;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hbrBackground = (HBRUSH)RGB(255, 255, 255);
RegisterClassEx(&WindowClass);
HWND Window = CreateWindowEx(0, L"DllWindowClass", L"Flames v1.1 - By XenoSaga3000", (WS_SYSMENU | WS_MINIMIZEBOX), 200, 200, 500, 450, FindWindow(NULL, L"ROBLOX"), CreateMenu(), InjectedModuleHandle, NULL);
ShowWindow(Window, SW_SHOWNORMAL);
while (GetMessage(&Messages, NULL, 0, 0))
{
TranslateMessage(&Messages);
DispatchMessage(&Messages);
};
return 1;
};
LRESULT CALLBACK DllWindowProcedure(HWND Window, UINT Message, WPARAM WParameter, LPARAM LParameter)
{
switch (Message)
{
case WM_COMMAND:
switch(WParameter)
{
//Check for commands here.
};
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(Window, Message, WParameter, LParameter);
};
return 0;
};
bool APIENTRY DllMain(HMODULE DllInstance, DWORD Reason, LPVOID Reserved)
{
if(Reason == DLL_PROCESS_ATTACH)
{
InjectedModuleHandle = DllInstance;
CreateThread(0, NULL, DllThreadProcedure, NULL, NULL, NULL);
};
return TRUE;
};
You can look at this blog for an example for your case.
One of the differences is that it uses DllMain's provided HINSTANCE as the instance handle for the window class. GetModuleHandle(NULL) returns the instance handle of the calling process instead of the DLL.
Note that Windows API calls like CreateThread from DllMain are not recommended, see here.
I am writing a fairly simple windows C++ application in Visual Studio Express 2013, and at startup have a problem where the window hangs for 5-10 seconds due to a DispatchMessage processing a WM_TIMER message. The problem is, I never created a timer, so I'm not sure why I'm getting the message nor why there is this long hang.
enum KRESULT
{
K_OK,
K_FALSE,
K_QUIT
};
HINSTANCE g_hInstance;
HWND g_hWnd;
char* g_winName;
char* g_name;
KRESULT MessagePump( MSG &msg )
{
if( !PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
// there weren't any messages
return K_FALSE;
}
else
{
if( msg.message == WM_QUIT )
return K_QUIT;
if( msg.message == WM_CLOSE )
return K_QUIT;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return K_OK;
}
void Run()
{
MSG msg;
bool done = false;
while( !done )
{
KRESULT msgRes = K_OK;
while( msgRes != K_FALSE )
{
msgRes = MessagePump( msg );
if( msgRes == K_QUIT )
{
done = true;
break;
}
done = !DoFrame();
}
}
}
bool DoFrame()
{
// do a bunch of stuff, disabled for debugging.
// returns false if Esc is pressed
return true;
}
LRESULT CALLBACK MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch( uMsg )
{
// Code managing WM_SIZE, WM_ENTERSIZEMOVE, etc that has to do with changing the size of the window.
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
bool bRunDefault = false;
switch( uMsg )
{
// check if the window is being destroyed.
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
// check if the window is being closed.
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
default:
{
bRunDefault = true;
break;
}
}
}
if( bRunDefault )
return MessageHandler(hWnd, uMsg, wParam, lParam);
return 0;
}
void InitWindows()
{
WNDCLASSEX wc;
DEVMODE dmScreenSettings;
int posX, posY;
// get the instance of this application
g_hInstance = GetModuleHandle(NULL);
// setup the windows class with some default settings
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_name;
wc.cbSize = sizeof(WNDCLASSEX);
// register the window class
RegisterClassEx(&wc)
// determine the resolution of the client desktop screen
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
int wWidth = 1366;
int wHeight = 768;
DWORD styleFlags;
// place it in the middle of the screen
posX = ( screenWidth - wWidth ) / 2;
posY = ( screenHeight - wHeight ) / 2;
styleFlags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
// create the window with the screen settings and get the handle
g_hWnd = CreateWindowEx( WS_EX_APPWINDOW, g_name, g_winName, styleFlags,
posX, posY, wWidth, wHeight, NULL, NULL,
g_hInstance, NULL );
// bring the window up on the screen and set it as main focus
ShowWindow( g_hWnd, SW_SHOW );
SetForegroundWindow( g_hWnd );
SetFocus( g_hWnd );
// mouse cursor options
ShowCursor( true );
}
Sorry about all the code, I wasn't sure how much of it was relevant. Most of these functions are encapsulated in an app class, and my WinMain function creates an instance of the class, calls InitWindows(), and Run(). The place where it hangs is in the MessagePump function, at DispatchMessage(). The WM_TIMER msg is passed twice, each with the following parameters:
hwnd = 0x00000000 <NULL>
message = 275 (WM_TIMER)
wParam = 18876 (this is random, but varies from around 18000 to 22000 or so)
lParam = 1975757148 (this is always the same)
Then, it is passed once with wParam = 1 and lParam = 0. Always in this order, and this group of three WM_TIMER messages are called every few frames.
The hanging only happens once, however, on the first time the message is passed at the beginning.
The hanging is the worst in debug mode of course, but in Release build, it happens as well.
I can add a handler to the MessagePump function, like so:
if( msg.message == WM_TIMER )
return K_OK;
This works to stop the hanging, but then it creates another problem where, when I move the window, my whole OS freezes for about 5-10 seconds.
I have been programming for a while (by no means an expert) but this baffles me. Any help is appreciated. Thanks!
Edit:
I tried building and running on two other machines. No hanging on both. After several restarts of the original machine, removing the startup processes that I felt I didn't need, the problem finally stopped, and has remained OK even after ~50 runs. I wish it was more satisfying to report which process was causing it, but I still haven't figured it out.
There was no process at the address 75C3A95C when looking at the modules window, strangely.
Something I noticed though: on all machines, the WM_TIMER messages are being passed. In addition, even with the default project you can create in VS2013 Exp, Win32Project1, these 3 WM_TIMER messages are being passed. Also, the Win32Project1 did hang as well, so it really had nothing to do with my code, but I guess... some strange interaction with a process running in my machine?
Thanks for everyone's help.
Perhaps another running program is sending yours the WM_TIMER messages, or perhaps a library you are linking in. Try running on on another, clean machine.
Can anyone tell me how to enable and disable USB port using C/C++.
I have already searched one way to do this..using windows registry but there are some problems with it.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR
change the value of start value to 3----for unblock
4----block
It does not show correct behavior on windows 7. eg-
when I change the value of start value to 4 it disable the usb ports but again for enabling we need to restart the system and one more thing after disabling all the ports are disabled but still we are able to use already plugged device.
Any other way to do it?
I have found out one more solution for this using devcon utility.
It provides various commands to enable and disable usb devices.
http://msdn.microsoft.com/en-us/library/windows/hardware/ff544746(v=vs.85).aspx#ddk_example_31_disable_devices_by_device_instance_id_tools
But it needs administrative privileges for running commands and I don't have source code for this.
So I want ask you all one thing..
I have heard about libusb-win32 library for writing prog for USB devices.
So does anybody have idea about it..
Any help will be highly appreciated..
Thank you all..
This is really answering comments on the question r.e. device detection.
Create a hidden window in your console application.
This example assumes you have a class called DevNotifier, with an HWND hidden_wnd_; member variable:
static TCHAR const s_window_class[] = _T("Device notification window");
static TCHAR const* const s_window_title = s_window_class;
LRESULT CALLBACK
DevNotifierWndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
) {
DevNotifier* dn = (DevNotifier*) ::GetWindowLongPtr(hWnd, GWLP_USERDATA);
switch (message)
{
case WM_DEVICECHANGE:
dn->onWM_DEVICECHANGE(
wParam,
lParam
);
break;
default:
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void
DevNotifier::createHiddenWindow()
{
HINSTANCE hinstance = ::GetModuleHandle(NULL);
if ((hinstance == 0) || (hinstance == INVALID_HANDLE_VALUE))
{
throw Exception("Failed to get application instance handle.");
}
// register window class
WNDCLASSEX wcex;
::memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = &DevNotifierWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hinstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = s_window_class;
wcex.hIconSm = 0;
(void) ::RegisterClassEx(&wcex);
// Create the window
hidden_wnd_ = ::CreateWindow(
s_window_class,
s_window_title,
WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hinstance,
NULL
);
if (!hidden_wnd_) {
throw Exception("Failed to create device notification window");
}
#ifdef _WIN64
::SetWindowLongPtr(static_cast<HWND>(hidden_wnd_), GWLP_USERDATA, (LONG_PTR)this);
#else
::SetWindowLongPtr(static_cast<HWND>(hidden_wnd_), GWLP_USERDATA, (LONG)this);
#endif
::ShowWindow(static_cast<HWND>(hidden_wnd_), SW_HIDE);
}
You can register for notifications on hidden_wnd_.
e.g.
DEV_BROADCAST_DEVICEINTERFACE filter;
ZeroMemory(&filter, sizeof(filter));
filter.dbcc_size = sizeof(filter);
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_classguid = /* SOME INTERFACE GUID */;
HDEVNOTIFY hdn = ::RegisterDeviceNotification(
hidden_wnd_,
&filter,
DEVICE_NOTIFY_WINDOW_HANDLE
);
You'll need to implement the function to deal with the WM_DEVICE_CHANGE messages:
bool
DevNotifier::onWM_DEVICECHANGE(WPARAM wparam, LPARAM lparam)
{
DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*)lparam;
// Note dbh will NOT always be valid, depending upon the value of wparam.
if (wparam == DBT_DEVNODES_CHANGED) {
// Do some stuff here
return true;
}
else if (wparam == DBT_DEVICEARRIVAL) {
DEV_BROADCAST_HDR* hdr = (DEV_BROADCAST_HDR*)lparam;
if (hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
DEV_BROADCAST_DEVICEINTERFACE* devinterface =
(DEV_BROADCAST_DEVICEINTERFACE*)hdr;
// Do some stuff here
}
}
else if (wparam == DBT_DEVICEREMOVEPENDING) {
}
else if (wparam == DBT_DEVICEREMOVECOMPLETE) {
HANDLE h = INVALID_HANDLE_VALUE;
DEV_BROADCAST_HDR* phdr = (DEV_BROADCAST_HDR*) lparam;
if (phdr->dbch_devicetype == DBT_DEVTYP_HANDLE) {
DEV_BROADCAST_HANDLE* pdbh = (DEV_BROADCAST_HANDLE*) lparam;
h = pdbh->dbch_handle;
}
else if (phdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
DEV_BROADCAST_DEVICEINTERFACE* devinterface =
(DEV_BROADCAST_DEVICEINTERFACE*)phdr;
// Do some stuff here
}
// Maybe do some stuff here too.
}
return false;
}
In your console application you are going to have to run a message pump to get windows messages to work. If you have got other stuff to do while the application is waiting for messages then you'll need to also handle that here.
while (GetMessage(&message, NULL, 0, 0) > 0) {
TranslateMessage(&message);
DispatchMessage(&message);
}
Recently I came about a strange difference between the two Win32 API calls "PostMessage" and "SendNotifyMessage" (at least noticed on Win7 64bit SP1):
An owned top-level window of another process seems not to receive messages broadcasted (HWND_BROADCAST) with "PostMessage" while it receives messages broadcasted with "SendNotifyMessage" in its WndProc.
The sent message have been registered with the help of a call to "RegisterWindowMessage".
Even using Spy++, I cannot see the message arriving when using "PostMessage". In addition, I want to mention, that if I send the message directly to the specific HWND with "PostMessage", it arrives as expected. So it looks like the windows-internal implementation of "PostMessage" just skips my window when iterating for execution of the broadcast.
Reading the respective MSDN documentation, I cannot see any statement about this difference and I am wondering if this is a bug in PostMessage or in SendNotifyMessage and if I can rely on SendNotifyMessage to continue to show this behavior in future versions of Windows.
So does someone have a plausible explanation why the both functions treat the broadcasts differently in this situation?
In addition, I would want to ask if there is any way to still use PostMessage to broadcast to an owned top-level window, because I would prefer to post the message because I would prefer to not skip the message queue (which is what SendNotifyMessage does).
In case you are curious why I want to reach a top-level owned window: in WPF, windows are hidden from the taskbar (Window.ShowInTaskbar property) by making them owned top-level windows with a hidden owner window.
Thanks a lot in advance for any ideas or comments on this topic.
Attachment: here a sample showing the behavior ... simply build it, and start it two times ... the second process should make a message show up in the first one.
Here is also a link to the complete solution including a build EXE: Link to the complete VS solution
#include <windows.h>
#include <stdio.h>
#include <string>
#include <vector>
HWND hwndMain = NULL;
HWND ownerHwnd = NULL;
std::vector<std::string> theOutput;
UINT MyRegisteredMessage1 = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc = NULL;
if (message == MyRegisteredMessage1 && wParam != (WPARAM) hwndMain)
{
if (lParam == (LPARAM) 1)
theOutput.push_back("Got a 'MyRegisteredMessage1' via PostMessage");
if (lParam == (LPARAM) 2)
theOutput.push_back("Got a 'MyRegisteredMessage1' via SendNotifyMessage");
InvalidateRect(hwndMain, NULL, TRUE);
}
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
for(size_t i = 0, pos = 0; i < theOutput.size(); ++i, pos += 20)
TextOutA(hdc, 0, pos, theOutput[i].c_str(), theOutput[i].size());
EndPaint (hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WndProcHidden(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
BOOL bRet;
WNDCLASSA wc;
UNREFERENCED_PARAMETER(lpszCmdLine);
if (!hPrevInstance)
{
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProcHidden;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);;
wc.lpszMenuName = "MainMenu";
wc.lpszClassName = "MyOwnerWindowClass";
if (!RegisterClassA(&wc))
return FALSE;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.lpszClassName = "MyOwnedWindowClass";
if (!RegisterClassA(&wc))
return FALSE;
}
ownerHwnd = CreateWindowA("MyOwnerWindowClass", "OwnerWindow",
WS_OVERLAPPEDWINDOW, 0, 0, 800, 400, (HWND) NULL,
(HMENU) NULL, hInstance, (LPVOID) NULL);
hwndMain = CreateWindowA("MyOwnedWindowClass", "OwnedWindow",
WS_OVERLAPPEDWINDOW, 0, 0, 800, 400, ownerHwnd,
(HMENU) NULL, hInstance, (LPVOID) NULL);
// only show the "real" window
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
MyRegisteredMessage1 = RegisterWindowMessageA("MyRegisteredMessage1");
char infoText[256];
_snprintf_s(infoText, 256,
"HWND = %X, registered message code for 'MyRegisteredMessage1' = %d",
hwndMain, MyRegisteredMessage1);
theOutput.push_back(infoText);
InvalidateRect(hwndMain, NULL, TRUE);
PostMessage(HWND_BROADCAST, MyRegisteredMessage1, (WPARAM) hwndMain, (LPARAM) 1);
Sleep(1000);
SendNotifyMessageA(HWND_BROADCAST, MyRegisteredMessage1, (WPARAM) hwndMain, (LPARAM) 2);
while( (bRet = ::GetMessage( &msg, NULL, 0, 0 )) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
You may need to register your message using RegisterWindowMessage() -- see the Remarks section of this MSDN article
Just adding this here for info..
I was able to get around this issue in c# by registering an IMessageFilter object on the Application level. PreFilterMessage on this object will receive the message and I can handle it from there.
public class FooMessageFilter : IMessageFilter
{
uint UM_FOO = 0;
public event EventHandler OnFoo;
public FooMessageFilter()
{
UM_FOO = Win32.RegisterWindowMessage("UM_FOO");
}
public bool PreFilterMessage(ref Message m)
{
if(m.Msg == UM_FOO)
{
if(OnFoo != null)
OnFoo(this, new EventArgs());
return true;
}
return false;
}
}
I then added this message filter to the Application context in my owned top-level form's constructor.
public partial class Form1 : Form
{
private FooMessageFilter fooFilter = new FooMessageFilter();
public Form1()
{
InitializeComponent();
// Register message filter
Application.AddMessageFilter(fooFilter);
// Subscribe to event
fooFilter.OnFoo += HandleFoo;
}
private void HandleFoo(object o, EventArgs e)
{
MessageBox.Show("Foo!");
}
}
From there it was just a matter of hooking up events in my top-level window to the message filter. This was necessary because of the need to adhere to current architecture, and the message originating from a third party process.
The documentation page(s) for PostMessage() mention that integrity level restrictions apply:
Starting with Windows Vista, message posting is subject to UIPI. The thread of a process can post messages only to message queues of threads in processes of lesser or equal integrity level.
There is no mention of such restrictions on SendNotifyMessage(). Since you don't check the return value of either, you could be running into that, and you wouldn't know it.