0

Hello I was wondering what is wrong with this PINVOKE declaration? I am not finding the mistake.

This is the Code in C++

BOOL HOOKDLL_API WINAPI SetHook(int HookType, BOOL bInstall,
                                    DWORD dwThreadId = 0,
                                    HWND hWndCaller = NULL);

This is my declaration:

[
        DllImport("CppHookDll.dll", CharSet = CharSet.Auto,
        //EntryPoint="?SetHook@@YGHHHKPAUHWND__@@@Z",
        EntryPoint = "SetKeyboardHook",
        ExactSpelling = true,CallingConvention=CallingConvention.StdCall)
    ]

    public static extern bool SetHook(int HookType, bool bInstall, [MarshalAs(UnmanagedType.U4)] UInt32 dwThreadId, IntPtr hWndCaller);

Thankt in advance.

  • Hmm. What C++ compiler? 64-bit issues? Is `int` there 32 bits? Is bool 1 byte? – Chris Jul 25 '14 at 09:08
  • C# `bool` is marshalled as a 4 byte Windows `BOOL`. – David Heffernan Jul 25 '14 at 09:45
  • Are you sure the function uses the stdcall convention? I think cdecl is the default for functions declared as extern "C" (although it was a long time ago since I worked with this so I may very well be wrong) – erikkallen Jul 25 '14 at 10:00
  • @erikkallen `WINAPI` is a macro that expands to `__stdcall` http://stackoverflow.com/questions/2348442/what-does-winapi-in-main-function-mean – David Heffernan Jul 25 '14 at 10:24
  • Wrong function, do keep the mangled name. A "SetKeyboardHook" function would not have the *HookType* argument, it already knows the hook type :) – Hans Passant Jul 25 '14 at 11:15

1 Answers1

1

It looks like you are just importing the wrong function. The function in the unmanaged code is named SetHook, but you are importing a function named SetKeyboardHook. Presumably the function SetKeyboardHook has a different signature which would explain the stack imbalance warning.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490