C 全域性滑鼠鍵盤Hook

2022-03-28 14:37:28 字數 4256 閱讀 6298

原文出自:

using

system;

using

system.collections.generic;

using

system.reflection;

using

system.runtime.interopservices;

using

system.text;

using

system.windows.forms;

namespace

dciengine.framework.snap

//裝置鉤子的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int

threadid);

//卸下鉤子的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

bool unhookwindowshookex(int

idhook);

//下乙個鉤掛的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

int callnexthookex(int idhook, int

ncode, int32 wparam, intptr lparam);

[dllimport(

"user32 ")]

public

static

extern

int toascii(int uvirtkey, int uscancode, byte lpbkeystate, byte lpwtranskey, int

fustate);

[dllimport(

"user32 ")]

public

static

extern

int getkeyboardstate(byte

pbkeystate);

public

delegate

int hookproc(int

ncode, int32 wparam, intptr lparam);

//////

墨認的建構函式構造當前類的例項並自動的執行起來.

///

public

keybordhook()

//析構函式.

~keybordhook()

public

void

start()}}

public

void

stop()

//如果卸下鉤子失敗

if (!(retkeyboard)) throw

new exception("

unhookwindowshookex failed. ");

}private

int keyboardhookproc(int

ncode, int32 wparam, intptr lparam)

//引發onkeypressevent

if (onkeypressevent != null && wparam ==wm_keydown)

}//引發onkeyupevent

if (onkeyupevent != null && (wparam == wm_keyup || wparam ==wm_syskeyup))

}return

callnexthookex(hkeyboardhook, ncode, wparam, lparam);}}

//////

這個類可以讓你得到乙個在執行中程式的所有滑鼠事件

///並且引發乙個帶mouseeventargs引數的.net滑鼠事件以便你很容易使用這些資訊

///

public

class

mousehook

//宣告滑鼠鉤子的封送結構型別

[structlayout(layoutkind.sequential)]

public

class

mousehookstruct

//裝置鉤子的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int

threadid);

//卸下鉤子的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

bool unhookwindowshookex(int

idhook);

//下乙個鉤掛的函式

[dllimport("

user32.dll

", charset = charset.auto, callingconvention =callingconvention.stdcall)]

public

static

extern

int callnexthookex(int idhook, int

ncode, int32 wparam, intptr lparam);

public

delegate

int hookproc(int

ncode, int32 wparam, intptr lparam);

//////

墨認的建構函式構造當前類的例項.

///

public

mousehook()

//析構函式.

~mousehook()

public

void

start()}}

public

void

stop()

//如果卸下鉤子失敗

if (!(retmouse)) throw

new exception("

unhookwindowshookex failed. ");

}private

int mousehookproc(int

ncode, int32 wparam, intptr lparam)

//從**函式中得到滑鼠的資訊

mousehookstruct mymousehookstruct = (mousehookstruct)marshal.ptrtostructure(lparam, typeof

(mousehookstruct));

mouseeventargs e = new mouseeventargs(button, clickcount, mymousehookstruct.pt.x, mymousehookstruct.pt.y, 0

); onmouseactivity(

this

, e);

}return

callnexthookex(hmousehook, ncode, wparam, lparam);}}

}

在vs中直接執行,hook將會失敗。

必須生成release然後直接執行exe才能生效。。

如果失敗的話,可以把**寒函式設定成靜態的

C 全域性滑鼠鍵盤Hook 備查

using system using system.collections.generic using system.reflection using system.runtime.interopservices using system.text using system.windows.form...

如何在C 中使用全域性滑鼠 鍵盤Hook

c dllimport user32.dll charset charset.auto,callingconvention callingconvention.stdcall,setlasterror true private static extern int setwindowshookex i...

通用滑鼠鍵盤

該程式通過並行機制處理滑鼠和鍵盤的訊息,為了簡化程式裁減了一些 該程式可用為基礎在該程式上新增 可用於文字編輯,遊戲和其他滑鼠鍵盤通用的程式.include include include include include union regs regs unsigned size void far ...