Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,

i would like implement keys hook in my application but i still have 1 error:

Erreur   1  error C3352: 'int IHMV2::FrmPrincipal::captureKey(int,System::IntPtr,System::IntPtr)' : la fonction spécifiée est incompatible avec le type délégué 'int (int,System::IntPtr,System::IntPtr)' 


I don't find the solution, can you help me?

Thanks,

Rémy

code:

C++
//System level functions to be used for hook and unhook keyboard input  

    delegate int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

      LowLevelKeyboardProc^ objKeyboardProcess;


    [DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]

    static int SetWindowsHookEx(int id, LowLevelKeyboardProc^ callback, IntPtr hMod, int dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]

    static bool UnhookWindowsHookEx(IntPtr hook);

    [DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]

    static int CallNextHookEx(int hook, int nCode, IntPtr wp, IntPtr lp);

    [DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError = true)]

    static IntPtr GetModuleHandle(String^ name);

    [DllImport("user32.dll", CharSet = CharSet::Auto)]

    static short GetAsyncKeyState(Keys key);

    //Declaring Global objects     

      private: int ptrHook;

    


      public: int captureKey(int nCode, IntPtr wParam, IntPtr lParam)

      {

        if (nCode >= 0)

        {

            KBDLLHOOKSTRUCT^ objKeyInfo = (KBDLLHOOKSTRUCT)Marshal::PtrToStructure(lParam, KBDLLHOOKSTRUCT::typeid);

               

            // Disabling Windows keys 


            if (objKeyInfo->key == Keys::RWin || objKeyInfo->key == Keys::LWin || objKeyInfo->key == Keys::Tab && HasAltModifier(objKeyInfo->flags) || objKeyInfo->key == Keys::Escape && (ModifierKeys & Keys::Control) == Keys::Control)     

            {

                return 1; // if 0 is returned then All the above keys will be enabled

            }

        }

        return CallNextHookEx(ptrHook, nCode, wParam, lParam);

      }


    bool HasAltModifier(int flags)

    {

        return (flags & 0x20) == 0x20;

    }


      public:

      [StructLayout(LayoutKind::Sequential)]

      value struct KBDLLHOOKSTRUCT

    {

            public: Keys key;

            public: int scanCode;

            public: int flags;

            public: int time;

            public: IntPtr extra;

    };




private: System::Void FrmPrincipal_Load(System::Object^  sender, System::EventArgs^  e) {


                  ProcessModule^ objCurrentModule = Process::GetCurrentProcess()->MainModule;

                  objKeyboardProcess = gcnew LowLevelKeyboardProc(&FrmPrincipal::captureKey); // Error is here

                  ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule->ModuleName), 0);

                     }
Posted

1 solution

You cannot use class instance functions to set Win32 callbacks, they must be static.
 
Share this answer
 
Comments
Remyb95 6-Jun-13 5:43am    
thanks for you answer. You mean i cannot call my fonction this way?
gcnew Delegate(&FrmPrincipal::MyFonction)

What should i do?
Richard MacCutchan 6-Jun-13 5:54am    
No, that is not what I said; I said that your function must be static. Class instance functions contain a hidden first parameter of this, which is a pointer to the object that is being acted on. That means that the function signature does not match the definition of the function that you are trying to call.
Remyb95 6-Jun-13 6:52am    
thank you, i change my functions captureKey and HasAltModifier to static and it's works.

solved!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900