318804 How to: Set the window hook in Visual C # .NET (from MKBA)

xiaoxiao2021-03-06  75

This task content

Summary

Set the mouse hook does not support global hook references in the .NET framework.

SUMMARY This article uses a mouse hook as an example, describes how to set a hook specific to a certain thread and a hook specific to a hook process. You can use hook to monitor specific types of events. You can associate these events as a call thread with a specific thread or all threads in the same desktop.

Back to top

Set the mouse hook To set the hook, call from the user32.dll file

Setwindowshookex function. This function can install a hook process defined by an application to the hook chain associated with this hook.

To set a mouse hook and monitor your mouse event, follow these steps:

Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click the Visual C # item under the project type, then click Windows Applications under Templates. Type ThreadspecificMousehook in the Name box. The Form1 is added to the project by default. Add the following code rows to other USING statements in the Form1.cs file. Using system.Runtime.InteropServices;

Add the following code in the Form1 class: Public Delegate Int HookProc (int Ncode, INTPTR WPARAM, INTPTR LPARAM);

// Declare Hook Handle As Int.

Static int hHHOOK = 0;

// Declare mouse hook constant.

// for other hook types, you can obtain these values ​​from winuser.h in Microsoft SDK.

Public const INT wh_mouse = 7;

Private system.windows.Forms.Button button1;

// Declare MousehookProcedure As HookProc Type.

HookProc MousehookProcedure;

// Declare Wrapper Managed Point Class.

[Structlayout (layoutkind.sequential)]

Public Class Point

{

Public int x;

Public int y;

}

// Declare Wrapper Managed MousehookStruct Class.

[Structlayout (layoutkind.sequential)]

Public Class MousehookStruct

{

PUBLIC POINT PT;

Public int hwnd;

Public int whittestcode;

Public int dwextrainfo;

}

// Import for setWindowshookex function.

// Use this function to install thread-specific hook.

[DLLIMPORT ("User32.dll", charset = charset.auto,

Callingconvention = calingconvention.stdcall)]

Public Static Extern Int setWindowshookex (int IDHOK, HOOKPROC LPFN,

INTPTR HINSTANCE, INT THREADID;

// Import for unhookwindowshookex.

// Call this function to uninstall the hook.

[DLLIMPORT ("User32.dll", Charset = Charset.auto, CallingConvention = CALLINGCONVENTION.STDCALL)]

Public Static Extern Bool UnHookWindowsHookex (Int IDHOOK);

// Import for callnexthookex.

// use this function to pass the hook information to next hook procedure in chain.

[DLLIMPORT ("User32.dll", charset = charset.auto,

Callingconvention = calingconvention.stdcall)]

Public Static Extern Int CallNexthookex (int IDHOK, INT NCODE,

INTPTR WPARAM, INTPTR LPARAM;

Add the Button control to the form, then add the following code to the Button1_Click process: private void button1_click (Object Sender, System.EventArgs E)

{

IF (hHOOK == 0)

{

// Create an instance of hookproc.

MousehookProcedure = New HookProc (Form1.MouseHookProc);

HHOOK = SETWINDOWSHOKEX (Wh_Mouse,

MousehookProcedure,

(INTPTR) 0,

Appdomain.getCurrentThreadId ());

// if setwindowshookex fails.

IF (hHOOK == 0)

{

MessageBox.show ("SetWindowsHookex Failed";

Return;

}

Button1.text = "unhook windows hook";

}

Else

{

Bool Ret = UnHookWindowsHookex (HHOOK);

// if unhookwindowshookex fails.

IF (RET == FALSE)

{

MessageBox.Show ("UnhookWindowsHookex Failed";

Return;

}

hHOOK = 0;

Button1.text = "SET Windows Hook";

this.Text = "Mouse Hook";

}

}

Add the following code to the MouseHookProc function in the Form1 class: Public Static Int MousehookProc (int Ncode, INTPTR WPARAM, INTPTR LPARAM)

{

// Marshall The Data from Callback.

Mousehooking = (MousehookStruct) Marshal.PTRTOStructure (lParam, Typeof (MousehookStruct);

IF (ncode <0)

{

Return CallNexthookex (HHOOK, NCODE, WPARAM, LPARAM);

}

Else

{

// Create a string variable with shows current mouse. Coordinates

String strcaption = "x =" mymousehooktruct.pt.x.tostring ("d")

"y ="

MyMouseHookStruct.pt.y.tostring ("d");

// NEED TO GET The Active Form Because It is a static function.

Form Tempform = form.activeform;

// set the caption of the form.

TempForm.Text = STRCAPTION;

Return CallNexthookex (HHOOK, NCODE, WPARAM, LPARAM);

}

}

Press the F5 key to run this project, then click the button on the form to set this hook. When the pointer moves on the form, the mouse coordinate will appear on the form header bar. Click this button again to delete this hook.

Back to top

The global hooks do not support global hooks in the .NET framework you cannot implement global hooks in the Microsoft .NET framework. To install global hooks, the hook must have a native dynamic link library (DLL) export to insert it itself into another process that needs to be tuned into a valid and consistent function. This requires a DLL export, and the .NET framework does not support this. The hosted code does not allow function pointers with a unified value, because these functions are dynamically built a proxy.

Back to top

For more information on the window hook, see the MSDN documentation below:

About hookshtp: //msdn.microsoft.com/library/default.asp? Url = / library / en-us / winui / hooks_9rg3.asp

Back to top

The information in this article applies to:

Microsoft .NET Framework SDK 1.0 Microsoft Visual C # .NET (2002)

Recent Updated: 2004-3-10 (3.0) Keywords: KbhowTomaster KB318804 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108896.html

New Post(0)