SetWindowsHookEx fails on WIN98
-
Hi peoeple, i originly was working with VS.Net 2003 and i use WINXP. when i tried to run my package on win 98 (installed the Framework 1.1) , it caused an Exception. do i need to copy dll file along with my package? my dll definitions:
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,IntPtr hInstance, int threadId);
my use in the code is :KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,KeyboardHookProcedure,Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0);
Please Help. Thanks alot, Ran. -- modified at 17:43 Tuesday 24th January, 2006 -
Hi peoeple, i originly was working with VS.Net 2003 and i use WINXP. when i tried to run my package on win 98 (installed the Framework 1.1) , it caused an Exception. do i need to copy dll file along with my package? my dll definitions:
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,IntPtr hInstance, int threadId);
my use in the code is :KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,KeyboardHookProcedure,Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0);
Please Help. Thanks alot, Ran. -- modified at 17:43 Tuesday 24th January, 20061. You should use
Assembly.ManifestModule
(at least with assemblies containing a single module) instead of the first entry inAssembly.GetModules()
. 2. You should declare P/Invoke methods as private and expose them through wrapped classes. P/Invoke is dangerous and you should make sure that you handle all inputs from potential unknown and malicious callers. This leads into my next point. 3. You should declare the P/Invoke methods withSetLastError=true
. If an error occurs, callMarshal.GetLastWin32Error
and let us know what the actual error is. Without that, all one can know is an error occured (which you already know). Wrapper this method would allow you to do that and throw aWin32Exception
(or any exception - this is just an example) with the last error as reported by Windows (so the exception is more actionable). There are several good articles here on Code Project that may help shed some light on what might be wrong. Just search for "SetWindowsHookEx". This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog] -
1. You should use
Assembly.ManifestModule
(at least with assemblies containing a single module) instead of the first entry inAssembly.GetModules()
. 2. You should declare P/Invoke methods as private and expose them through wrapped classes. P/Invoke is dangerous and you should make sure that you handle all inputs from potential unknown and malicious callers. This leads into my next point. 3. You should declare the P/Invoke methods withSetLastError=true
. If an error occurs, callMarshal.GetLastWin32Error
and let us know what the actual error is. Without that, all one can know is an error occured (which you already know). Wrapper this method would allow you to do that and throw aWin32Exception
(or any exception - this is just an example) with the last error as reported by Windows (so the exception is more actionable). There are several good articles here on Code Project that may help shed some light on what might be wrong. Just search for "SetWindowsHookEx". This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]HI, I figured out (after reading here) what was the problem in the VS2005 and so i changed the message a bit. now its working on the VS Express but not on the 98. i have read a bit about the SetWindowsHookEx function and it says i must do something there to be able to work on 98. can someone please explain what is that: http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp[^] Thanks again. R.Z
-
HI, I figured out (after reading here) what was the problem in the VS2005 and so i changed the message a bit. now its working on the VS Express but not on the 98. i have read a bit about the SetWindowsHookEx function and it says i must do something there to be able to work on 98. can someone please explain what is that: http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp[^] Thanks again. R.Z
All that says at the bottom is that to use the Unicode version of the function (NT supports Unicode and ANSI, while Win9x only supports ANSI) you have to use a shim named unicows.dll. You needn't worry about this since you're using
CharSet.Auto
. You need to figure out what the error is, though. See my paragraph about usingMarshal.GetLastWin32Error()
. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog] -
1. You should use
Assembly.ManifestModule
(at least with assemblies containing a single module) instead of the first entry inAssembly.GetModules()
. 2. You should declare P/Invoke methods as private and expose them through wrapped classes. P/Invoke is dangerous and you should make sure that you handle all inputs from potential unknown and malicious callers. This leads into my next point. 3. You should declare the P/Invoke methods withSetLastError=true
. If an error occurs, callMarshal.GetLastWin32Error
and let us know what the actual error is. Without that, all one can know is an error occured (which you already know). Wrapper this method would allow you to do that and throw aWin32Exception
(or any exception - this is just an example) with the last error as reported by Windows (so the exception is more actionable). There are several good articles here on Code Project that may help shed some light on what might be wrong. Just search for "SetWindowsHookEx". This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog] -
sorry to bother you again but i realy dont understand what you mean. i was looking for a method named ManifestModule and i didnt fint it. and i also dont know what is P/Invoke :~ Can you help me? Ran. R.Z
Assembly.ManifestModule
is new in .NET 2.0, so if you're not programming against .NET 2.0 you won't have it. P/Invoke is the act of calling native functions in .NET. It's what you call defining methods attributed withDllImportAttribute
like you're doing. Please add code to callMarshal.GetLastWin32Error
and report what the error is on Win9x. The fact that 0 is returned simply means an error occured but there are many errors that could have occured and without knowing which one it's impossible to help you further. I would also suggest looking for articles here on Code Project that useSetWindowsHookEx
. Sample code works on both 9x and NT at least for the ones I have tested (some time back when I was an editor). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog] -
Assembly.ManifestModule
is new in .NET 2.0, so if you're not programming against .NET 2.0 you won't have it. P/Invoke is the act of calling native functions in .NET. It's what you call defining methods attributed withDllImportAttribute
like you're doing. Please add code to callMarshal.GetLastWin32Error
and report what the error is on Win9x. The fact that 0 is returned simply means an error occured but there are many errors that could have occured and without knowing which one it's impossible to help you further. I would also suggest looking for articles here on Code Project that useSetWindowsHookEx
. Sample code works on both 9x and NT at least for the ones I have tested (some time back when I was an editor). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]well i've changed the public to private like you said. now i put this line before throwing the exception: MessageBox.Show(Marshal.GetLastWin32Error().ToString()); that will show me the error number i guess. and wrote : SetLastError=true run it on the win98 and it gives me the number 127. what that means? did i win anything ? :laugh: R.Z
-
well i've changed the public to private like you said. now i put this line before throwing the exception: MessageBox.Show(Marshal.GetLastWin32Error().ToString()); that will show me the error number i guess. and wrote : SetLastError=true run it on the win98 and it gives me the number 127. what that means? did i win anything ? :laugh: R.Z
If you lookup error 127 in winerror.h (available with VC++ that may be installed with your VS install, and online on http://msdn.microsoft.com[^]) you'll see that the error is
ERROR_PROC_NOT_FOUND
, which translates to the English text, "The specified procedure could not be found." I would recommend taking a look at http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/[^]. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog] -
If you lookup error 127 in winerror.h (available with VC++ that may be installed with your VS install, and online on http://msdn.microsoft.com[^]) you'll see that the error is
ERROR_PROC_NOT_FOUND
, which translates to the English text, "The specified procedure could not be found." I would recommend taking a look at http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/[^]. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]well, its generaly nice but it doesn't help regarding win98. i have allready succesfuly using Hooking but i just want to make sure the client can run it too. in Microsoft they say: WH_KEYBOARD Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure. WH_KEYBOARD_LL Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure. is it posible that the WH_KEYBOARD_LL is only on XP and not win98? R.Z
-
well, its generaly nice but it doesn't help regarding win98. i have allready succesfuly using Hooking but i just want to make sure the client can run it too. in Microsoft they say: WH_KEYBOARD Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure. WH_KEYBOARD_LL Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure. is it posible that the WH_KEYBOARD_LL is only on XP and not win98? R.Z
Yeah, the WH_KEYBOARD_LL hook is only available in NT4.0 SP3 and above. Windows 9x is not supported. Hey Heath! How's it going in Redmond? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Yeah, the WH_KEYBOARD_LL hook is only available in NT4.0 SP3 and above. Windows 9x is not supported. Hey Heath! How's it going in Redmond? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Hi Dave, so how can i get KeyUp/Pressed/Down , MouseClick events on using .net on 98? Thanks, Ran. R.Z
You can use
WH_KEYBOARD
andWH_MOUSE
. These will give you the scan codes for keys pressed and mouse messages like move, click, etc., respectively. The article I linked shows an example and other articles on this site have further examples. I encourage you to use the other features of this web site. There's thousands of articles at your disposable with a rating system in place so you know which ones are good. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]