Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SetWindowsHookEx fails on WIN98

SetWindowsHookEx fails on WIN98

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ranzask
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • R ranzask

      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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      1. You should use Assembly.ManifestModule (at least with assemblies containing a single module) instead of the first entry in Assembly.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 with SetLastError=true. If an error occurs, call Marshal.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 a Win32Exception (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]

      R 2 Replies Last reply
      0
      • H Heath Stewart

        1. You should use Assembly.ManifestModule (at least with assemblies containing a single module) instead of the first entry in Assembly.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 with SetLastError=true. If an error occurs, call Marshal.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 a Win32Exception (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]

        R Offline
        R Offline
        ranzask
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • R ranzask

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 using Marshal.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 Reply Last reply
          0
          • H Heath Stewart

            1. You should use Assembly.ManifestModule (at least with assemblies containing a single module) instead of the first entry in Assembly.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 with SetLastError=true. If an error occurs, call Marshal.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 a Win32Exception (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]

            R Offline
            R Offline
            ranzask
            wrote on last edited by
            #5

            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

            H 1 Reply Last reply
            0
            • R ranzask

              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

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              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 with DllImportAttribute like you're doing. Please add code to call Marshal.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 use SetWindowsHookEx. 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]

              R 1 Reply Last reply
              0
              • H Heath Stewart

                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 with DllImportAttribute like you're doing. Please add code to call Marshal.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 use SetWindowsHookEx. 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]

                R Offline
                R Offline
                ranzask
                wrote on last edited by
                #7

                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

                H 1 Reply Last reply
                0
                • R ranzask

                  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

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  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]

                  R 1 Reply Last reply
                  0
                  • H Heath Stewart

                    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]

                    R Offline
                    R Offline
                    ranzask
                    wrote on last edited by
                    #9

                    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

                    D 1 Reply Last reply
                    0
                    • R ranzask

                      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

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      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

                      R 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        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

                        R Offline
                        R Offline
                        ranzask
                        wrote on last edited by
                        #11

                        Hi Dave, so how can i get KeyUp/Pressed/Down , MouseClick events on using .net on 98? Thanks, Ran. R.Z

                        H 1 Reply Last reply
                        0
                        • R ranzask

                          Hi Dave, so how can i get KeyUp/Pressed/Down , MouseClick events on using .net on 98? Thanks, Ran. R.Z

                          H Offline
                          H Offline
                          Heath Stewart
                          wrote on last edited by
                          #12

                          You can use WH_KEYBOARD and WH_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]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups