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. Only in Release config, SetWindowsHookEx throws error 80004005

Only in Release config, SetWindowsHookEx throws error 80004005

Scheduled Pinned Locked Moved C#
helpcomdebuggingtutorialquestion
2 Posts 2 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.
  • L Offline
    L Offline
    LimitedAtonement
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • L LimitedAtonement

      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

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

      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...

      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