Only in Release config, SetWindowsHookEx throws error 80004005
-
Dear Sirs, I'm having a problem with SetWindowsHookEx. In Debug mode, no problem, in release, it throws. Here's the code:
namespace barcode
{
public static class program
{
static void Main(string[] args)
{
_keyboard_delegate = Keyboard_Msg_Pump;
_keyboard_hook_handle = SetWindowsHookEx(
WH_KEYBOARD_LL,
_keyboard_delegate,
System.Runtime.InteropServices.Marshal.GetHINSTANCE(
System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]),
0);
if (_keyboard_hook_handle == 0)
{//this is failure...
int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(errorCode);
}
}private static int Keyboard\_Msg\_Pump(int nCode, System.Int32 wParam, System.IntPtr lParam) { return 12; } private static int \_keyboard\_hook\_handle; private static HOOKPROC \_keyboard\_delegate; private delegate int HOOKPROC(int nCode, int wParam, System.IntPtr lParam); private const int WH\_KEYBOARD\_LL = 13; \[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall, SetLastError=true)\] private static extern int SetWindowsHookEx( int idHook, HOOKPROC lpfn, System.IntPtr hMod, int dwThreadId); }
}
If you get to line sixteen (right after the if statement in the Main proc), you have an error. Try stepping through this example and tell me if it works for you. If I'm in debug mode, it works for me, but if I'm in release (even with optimize code off, which I thought would make it just like DEBUG), it fails with inconsistent errors, sometimes it says "Operation completed successfully" sometimes it says "Access denied" and other things. Let me know if you can run it in Release, and how. If not, tell me what error you get. JUST NOW, as I was writing this, I decided that it might be the setting, "Enable the VSHost" process. If it's off, it works. If it's on, no good. Does the VSHost run under lower authority than the user?? Let me know what you think.
In Christ, Aaron Laws http://ProCure.com
-
Dear Sirs, I'm having a problem with SetWindowsHookEx. In Debug mode, no problem, in release, it throws. Here's the code:
namespace barcode
{
public static class program
{
static void Main(string[] args)
{
_keyboard_delegate = Keyboard_Msg_Pump;
_keyboard_hook_handle = SetWindowsHookEx(
WH_KEYBOARD_LL,
_keyboard_delegate,
System.Runtime.InteropServices.Marshal.GetHINSTANCE(
System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]),
0);
if (_keyboard_hook_handle == 0)
{//this is failure...
int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(errorCode);
}
}private static int Keyboard\_Msg\_Pump(int nCode, System.Int32 wParam, System.IntPtr lParam) { return 12; } private static int \_keyboard\_hook\_handle; private static HOOKPROC \_keyboard\_delegate; private delegate int HOOKPROC(int nCode, int wParam, System.IntPtr lParam); private const int WH\_KEYBOARD\_LL = 13; \[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall, SetLastError=true)\] private static extern int SetWindowsHookEx( int idHook, HOOKPROC lpfn, System.IntPtr hMod, int dwThreadId); }
}
If you get to line sixteen (right after the if statement in the Main proc), you have an error. Try stepping through this example and tell me if it works for you. If I'm in debug mode, it works for me, but if I'm in release (even with optimize code off, which I thought would make it just like DEBUG), it fails with inconsistent errors, sometimes it says "Operation completed successfully" sometimes it says "Access denied" and other things. Let me know if you can run it in Release, and how. If not, tell me what error you get. JUST NOW, as I was writing this, I decided that it might be the setting, "Enable the VSHost" process. If it's off, it works. If it's on, no good. Does the VSHost run under lower authority than the user?? Let me know what you think.
In Christ, Aaron Laws http://ProCure.com
Yep, in order for the keyboard hook to work, you have to turn off the VS Hosting process. I don't know why - you just do. I also don't have any documentation on this. I'm just speaking from my own experience.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...