[?] ERROR_HOOK_NEEDS_HMOD during installing system hook

G

Gambero

Hi, i want to do system hook WH_SYSMSGFILTER,
my code is:

IntPtr hMod =
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
//install hook
hSysMsgHook = Api.User32.SetWindowsHookEx(WH_SYSMSGFILTER,
SysMsgHookProcedure, hMod, 0);
//If SetWindowsHookex fails.
if (hSysMsgHook == 0)
{
int errorCode = Marshal.GetLastWin32Error();
//do cleanup
Stop(false, false, true, false);
//Initializes and throws a new instance of the Win32Exception class with
the specified error.
throw new Win32Exception(errorCode);
}


hMod is valid but the api return code is ERROR_HOOK_NEEDS_HMOD!
other types of hook like WH_KEYBOARD_LL or WH_MOUSE_LL works fine!
my machine is windows vista
what's wrong?
Thanks
 
M

Mattias Sjögren

Hi, i want to do system hook WH_SYSMSGFILTER,

You can't implement global system hooks in managed code (with the
exception of the _LL types). Choose another language that compiles to
native code and that doesn't require any runtime library to run.


Mattias
 
G

Gambero

You can't implement global system hooks in managed code (with the
exception of the _LL types). Choose another language that compiles to
native code and that doesn't require any runtime library to run.

but i don't use managed code! i use windows api througth pinvoke:

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.StdCall, SetLastError = true)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr
hMod, int dwThreadId);

you are sure that i can't do that? this works fine for keyboard and mouse...
what is the problem for WH_SYSMSGFILTER?
do you have any documentation that report this limitation?
thanks!
 
W

Willy Denoyette [MVP]

Gambero said:
You can't implement global system hooks in managed code (with the
exception of the _LL types). Choose another language that compiles to
native code and that doesn't require any runtime library to run.

but i don't use managed code! i use windows api througth pinvoke:

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.StdCall, SetLastError = true)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr hMod, int dwThreadId);

you are sure that i can't do that? this works fine for keyboard and
mouse... what is the problem for WH_SYSMSGFILTER?
do you have any documentation that report this limitation?
thanks!


Mattias is right, your hook procedure is managed code and such procedure is
not exported as a native function, which is an absolute requirement to be
called globally.

See: "Global hooks are not supported in the .NET Framework" at the bottom of
the following KB article for more detailed info:
http://support.microsoft.com/kb/318804


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top