Traps

I am trying to convert a mouse hook like this.........

Code Snippet

MouseHookProcedure = new HookProc(Form1.MouseHookProc);

hHook = SetWindowsHookEx(WH_MOUSE,
MouseHookProcedure,
(IntPtr)0,
AppDomain.GetCurrentThreadId());

To a low level mouse hook like this........

Code Snippet

MouseHookProcedure = new HookProc(Form1.LowLevelMouseProc);

hHook = SetWindowsHookEx(WH_MOUSE_LL,

MouseHookProcedure,

AppDomain.GetCurrentThreadId(),0);

I get the following errors

Code Snippet

Warning 1 'System.AppDomain.GetCurrentThreadId()' is obsolete: 'AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the ManagedThreadId property on Thread. http://go.microsoft.com/fwlink/ linkid=14202'

Code Snippet
Error 2 The best overloaded method match for 'MouseKeyHook2.Form1.SetWindowsHookEx(int, MouseKeyHook2.Form1.HookProc, System.IntPtr, int)' has some invalid arguments

Please help.

Thanks in advance!



Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

I've tried using GetModuleHandle, and it still doesnt work.

Code Snippet

MouseHookProcedure = new HookProc(Form1.LowLevelMouseProc);

hHook = SetWindowsHookEx(WH_MOUSE_LL,

MouseHookProcedure,

GetModuleHandle(curModule.ModuleName), 0);

I get this error

Code Snippet

Error 1 The name 'GetModuleHandle' does not exist in the current context

and

Code Snippet
Error 2 The name 'curModule' does not exist in the current context

This should be an easy one for you guys. What am I doing wrong Here are my includes, all the code (and includes) are in a Form1.cs file

Code Snippet

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Ok I found some source code that does exactly what I'm looking for and in studying it, I found their hook statement.

//install hook

hMouseHook = SetWindowsHookEx(

WH_MOUSE_LL,

MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

if (hKeyboardHook == 0)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//do cleanup

Stop(false, true, false);

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

Their entire hook is defined as a class in its own .cs file When I insert that file into my own project, add the appropriate namespace for the class, and create an instance of it, it fails to hook. The SetWindowsHookEx returns a 0 and the line

Code Snippet
throw new Win32Exception(errorCode);

gets a popup window that breaks execution and throws me into source code window with a pop up box, title "Win32Exception was unhandled", below that in the body of the window it says, "The parameter is incorrect" (errorCode = 87 if that means anything to anyone). This part of the code might be a bug in the original source. But I'm confused as to why the hook fails in my program, but works in the program from which I pulled the .cs file from

Please help me.




Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Jung Yi

Hello,

try to use this...

try to find which one is correct threadid which should be hHandle pointer

Code Snippet

MsgBox(Threading.Thread.CurrentThread.ManagedThreadId.ToString)
MsgBox(AppDomain.GetCurrentThreadId.ToString)
MsgBox(GetCurrentThreadId.ToString) <-- this is correct one called from Win32 API


Code Snippet

[VB]

Declare Function GetCurrentThreadId Lib "kernel32" () As Long

[C#]

[DllImport("Kernel32.Dll")]
public static extern System.UInt32 GetCurrentThreadId();

[For Local Hook]

hHook = SetWindowsHookEx(WH_MOUSE,
MouseHookProcedure,
null (or 0),
GetCurrentThreadId());

[For Global Hook]

hHook = SetWindowsHookEx(WH_MOUSE,
MouseHookProcedure,
Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32,
0);






Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

I tried your suggestion by placing the messagebox right after the hook statement, and each thing you suggested came up with an error.

MessageBox.Show(Threading.Thread.CurrentThread.ManagedThreadId.ToString);

Code Snippet
Error 1 The name 'Threading' does not exist in the current context

MessageBox.Show(AppDomain.GetCurrentThreadId.ToString);

Code Snippet
Error 1 'System.AppDomain.GetCurrentThreadId()' is a 'method', which is not valid in the given context

MessageBox.Show(Threading.Thread.CurrentThread.ManagedThreadId.ToString);

Code Snippet

Error 1 The name 'Threading' does not exist in the current context

I even tried your hook statement

Code Snippet

hMouseHook = SetWindowsHookEx(WH_MOUSE,

MouseHookProcedure,

Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32,

0);

and I get the error

Code Snippet
Error 1 The name 'Reflection' does not exist in the current context

My problem cant be that difficult. I'm a total noob to c# and I'm sure there's a simple explanation as to why I get these errors. Maybe I'll go buy a book later today.





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Traps wrote:

Ok I found some source code that does exactly what I'm looking for and in studying it, I found their hook statement.

//install hook

hMouseHook = SetWindowsHookEx(

WH_MOUSE_LL,

MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

if (hKeyboardHook == 0)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//do cleanup

Stop(false, true, false);

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

Their entire hook is defined as a class in its own .cs file When I insert that file into my own project, add the appropriate namespace for the class, and create an instance of it, it fails to hook. The SetWindowsHookEx returns a 0 and the line

Code Snippet
throw new Win32Exception(errorCode);

gets a popup window that breaks execution and throws me into source code window with a pop up box, title "Win32Exception was unhandled", below that in the body of the window it says, "The parameter is incorrect" (errorCode = 87 if that means anything to anyone). This part of the code might be a bug in the original source. But I'm confused as to why the hook fails in my program, but works in the program from which I pulled the .cs file from

Bump, still cant figure this out.
Traps wrote:

Ok I found some source code that does exactly what I'm looking for and in studying it, I found their hook statement.

//install hook

hMouseHook = SetWindowsHookEx(

WH_MOUSE_LL,

MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

if (hKeyboardHook == 0)

{

//Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.

int errorCode = Marshal.GetLastWin32Error();

//do cleanup

Stop(false, true, false);

//Initializes and throws a new instance of the Win32Exception class with the specified error.

throw new Win32Exception(errorCode);

}

Their entire hook is defined as a class in its own .cs file When I insert that file into my own project, add the appropriate namespace for the class, and create an instance of it, it fails to hook. The SetWindowsHookEx returns a 0 and the line

Code Snippet
throw new Win32Exception(errorCode);

gets a popup window that breaks execution and throws me into source code window with a pop up box, title "Win32Exception was unhandled", below that in the body of the window it says, "The parameter is incorrect" (errorCode = 87 if that means anything to anyone). This part of the code might be a bug in the original source. But I'm confused as to why the hook fails in my program, but works in the program from which I pulled the .cs file from

Please help me.

bump. I still cant get this to work. Any suggestions





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

This is the definition of SetWindowsHookEx

Code Snippet
HHOOK SetWindowsHookEx(

int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);

idHook
[in] Specifies the type of hook procedure to be installed. This
parameter can be one of the following values.
lpfn
[in] Pointer to the hook procedure. If the dwThreadId parameter
is zero or specifies the identifier of a thread created by a
different process, the lpfn parameter must point to a hook procedure
in a DLL. Otherwise, lpfn can point to a hook procedure in the code
associated with the current process.
hMod
[in] Handle to the DLL containing the hook procedure pointed to by
the lpfn parameter. The hMod parameter must be set to NULL if the
dwThreadId parameter specifies a thread created by the current
process and if the hook procedure is within the code associated with
the current process.
dwThreadId
[in] Specifies the identifier of the thread with which the hook
procedure is to be associated. If this parameter is zero, the hook
procedure is associated with all existing threads running in the same
desktop as the calling thread.

This is my code for the hook

Code Snippet

// Create an instance of HookProc.

MouseHookProcedure = new HookProc(MouseHookProc);

//install hook

hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

idHook = WH_MOUSE_LL

lpfn = MouseHookProcedure

hMod = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0])

dwThreadid = 0

I've also tried this function

Code Snippet

hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,MouseHookProcedure,

(IntPtr)0 ,

0);

Result is the same hMouseHook = 0. Keep in mind this hook, the MouseHookProcedure, unhook, etc, is in a class module. This class module works fine in the program example I found on the net. However, if I copy this class to my own new windows project, it doesnt work. I cant for the life of me figure out why. It makes no sense.

What in the world is different from how the class runs in my program compared to the original program. The class is contained in the same file!!!





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Heres the error I get when I throw a win32 exception

System.ComponentModel.Win32Exception was unhandled
Message="The parameter is incorrect"
Source="MyMouseKeyHook"
ErrorCode=-2147467259
NativeErrorCode=87





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Ok, I've had it. Now I'm getting mad. There is nothing wrong with the hook or the hook class. It has to lay somewhere else within the projects elements like the assembly or something. I have no clue, I dont know enough about c# yet. I've replaced the assembly of my program with the working program, and I get a different win 32 exception(go figure).

Is it possible to change the framework that your project makes reference too Like perhaps the working project is referencing NET 1.1, whereas mine is using NET 2.0, is it possible to change this somewhere in your project I dont know I'm just brainstorming. Please if you have any thoughts post them. If anything it'll let me know people are thinking about this issue.





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

nobugz

The .NET framework cannot support most of the hooks you can set with SetWindowsHookEx(). They require the hook code to be present in an unmanaged DLL that can be injected into a process. However, the low-level mouse and keyboard hooks work. Check this thread for source code.





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Yep, from what I understand you cant inject into another process but your own. Thats why the these two global hooks work....something like that anyway....(still learning).... I have code that works. I have a project that I downloaded from somewhere it it sucessfully hooks fine, both mouse and keyboard. However when I copy/paste the class that does this, or include the .cs file into my own project, the hook fails and returns a 0. I'll check out your link anyway.

BTW, I found yet another class that has both mouse and keyboard LL hooks, and copy/pasted it into the project I already had(the one that successfully hooks), and that new class I inserted hooks as well......Its something to do with the other elements of the project, like I suggested earlier!!!! What other elements of this project is allowing these hooks to work

Edit to add, Your code looks like visual basic. I have already written a successfull mouse/keyboard macro program(records and playsback) in vb6, I am trying to convert it to c#

BTW..... CAN I PM YOU THE CLASS AND SEE IF YOU CAN MAKE IT WORK IN YOUR OWN NEW WINDOWS APPLICATION PROJECT It'll take just a couple minutes to throw a start and stop button, create an instance of the class, and invoke the appropriate start and stop methods.





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

I FIGURED IT OUT!!!!!!!!!





Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Sal Blomo

So why don't you tell us After getting all this help.




Re: Visual C# Express Edition Whats wrong with my SetWindowsHookEx statement?

Traps

Oh, sorry. I posted the answer in another thread. Its simple

Disable visual studio runtime host in debug properties for project.