Why my WndProc doesn't receive Shell Hook Messages if the app is set as Default Windows Desktop Shell?
-
13-10-2019 - |
Question
uMsgNotify = WinApi.RegisterWindowMessage("SHELLHOOK");
WinApi.RegisterShellHookWindow(this.Handle);
in my Form constructor
and this in my overrided WndProc:
protected override void WndProc(ref System.Windows.Forms.Message m)
{
IntPtr handle;
if (m.Msg == uMsgNotify)
{
switch (m.WParam.ToInt32())
{
case WinApi.HSHELL_WINDOWCREATED:
handle = m.LParam;
string windowName = GetWindowName(handle);
MessageBox.Show(windowName+" "+handle.ToString());
break;
case WinApi.HSHELL_WINDOWDESTROYED:
handle = m.LParam;
MessageBox.Show(handle.ToString());
break;
}
}
base.WndProc(ref m);
}
So this Win Form Works fine when default windows shell is explorer.exe. I can get all events All created and destroyed windows and their names. When i set my app.exe as defaul windows Shell. It doesn't receive messages.
Do I have to use dll function injection, separate dll file for hooking? I was trying to get simplier and easier solution.
Any ideas?
Thanks
Solution
The WH_SHELL
documentation explicitly states:
Note that custom shell applications do not receive
WH_SHELL
messages. Therefore, any application that registers itself as the default shell must call theSystemParametersInfo
function before it (or any other application) can receiveWH_SHELL
messages. This function must be called withSPI_SETMINIMIZEDMETRICS
and aMINIMIZEDMETRICS
structure. Set the iArrange member of this structure to ARW_HIDE.