Reputation: 876
I use RegisterWindowMessage("SHELLHOOK") to register for window create and destroy events. My code is written in C#. The code works perfectly when I'm debugging the code. But when I run the program without debugging, I'm not getting WndProc messages like I used to get while debugging. Does Windows block the hook?
class ShellHook:NativeWindow
{
public ShellHook(IntPtr hWnd)
{
if (Win32.RegisterShellHookWindow(this.Handle))
{
WM_ShellHook = Win32.RegisterWindowMessage("SHELLHOOK");
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ShellHook)
{
switch((ShellEvents)m.WParam)
{
//m.lparam
case ShellEvents.HSHELL_WINDOWCREATED:
if (windowCreatedEvent != null)
{
windowCreatedEvent(m.LParam);
}
break;
case ShellEvents.HSHELL_WINDOWDESTROYED:
if (windowDestroyedEvent != null)
{
windowDestroyedEvent(m.LParam);
}
break;
}
}
base.WndProc(ref m);
}
}
This is how I start my wpf app.
public partial class App : Application
{
MainWindow mainWindowView;
public App()
{
Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
mainWindowView = new MainWindow();
MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
mainWindowView.DataContext = mainWindowViewModel;
mainWindowView.ShowDialog();
}
}
My MainWindowViewModel Constructor is as follows:
public MainWindowViewModel()
{
EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
ShellHook shellHookObject = new ShellHook(f.Handle);
shellHookObject.windowCreatedEvent += shellHookObject_windowCreatedEvent;
shellHookObject.windowDestroyedEvent += shellHookObject_windowDestroyedEvent;
}
Upvotes: 1
Views: 2326
Reputation: 876
I changed the NativeWindow to Form and it looks like its working now. Guys please let me know your thoughts.
Upvotes: 1