新手请教,c++键盘钩子手动按按键可以注入,怎么实现自动注入啊?
展开全部
1 // 说明:头文件KeyRedir.h
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 #ifndef __KEYHOOK_H_H
5 #define __KEYHOOK_H_H
6
7 #ifdef MY_HOOK_API
8 #else
9 #define MY_HOOK_API _declspec(dllimport)
10 #endif
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 MY_HOOK_API BOOL __stdcall InstallHook();
17 MY_HOOK_API BOOL __stdcall UnInstallHook();
18
19 #ifdef __cplusplus
20 }
21 #endif
22
23 #endif
1 // 说明:Def文件KeyRedir.def
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 LIBRARY "KeyRedir"
5
6 EXPORTS
7 InstallHook
8 UnInstallHook
1 // 说明:源文件KeyRedir.cpp
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 #include <windows.h>
5
6 #pragma warning(disable:4996)
7
8 #define MY_HOOK_API _declspec(dllexport)
9 #include "KeyRedir.h"
10
11 HINSTANCE g_hInst = NULL;
12 static HHOOK g_hHook = NULL;
13
14 BYTE g_bVK[4] = {VK_LEFT, VK_DOWN, VK_RIGHT, VK_UP};
15
16 LRESULT CALLBACK LowLevelKeyboardProc(
17 int code,
18 WPARAM wParam,
19 LPARAM lParam)
20 {
21 if(code == HC_ACTION)
22 {
23 PKBDLLHOOKSTRUCT pStruct = (PKBDLLHOOKSTRUCT)lParam;
24 DWORD dwIndex = 0;
25 switch (pStruct->vkCode)
26 {
27 case L'A':
28 case L'a':
29 dwIndex = 0;
30 break;
31
32 case L'S':
33 case L's':
34 dwIndex = 1;
35 break;
36
37 case L'D':
38 case L'd':
39 dwIndex = 2;
40 break;
41
42 case L'W':
43 case L'w':
44 dwIndex = 3;
45 break;
46
47 default:
48 return CallNextHookEx(g_hHook, code, wParam, lParam);
49 }
50 if (wParam == WM_KEYDOWN)
51 {
52 keybd_event(g_bVK[dwIndex], MapVirtualKey(g_bVK[dwIndex], 0), 0, 0);
53 }
54 else if (wParam == WM_KEYUP)
55 {
56 keybd_event(g_bVK[dwIndex], MapVirtualKey(g_bVK[dwIndex], 0), KEYEVENTF_KEYUP, 0);
57 }
58 return TRUE;
59 }
60 return CallNextHookEx(g_hHook, code, wParam, lParam);
61 }
62
63 BOOL __stdcall InstallHook()
64 {
65 g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
66 LowLevelKeyboardProc,
67 g_hInst,
68 NULL);
69 return g_hHook == NULL ? FALSE : TRUE;
70 }
71
72 BOOL __stdcall UnInstallHook()
73 {
74 return UnhookWindowsHookEx(g_hHook);
75 }
76
77 BOOL WINAPI DllMain(IN HINSTANCE hDllHandle,
78 IN DWORD nReason,
79 IN LPVOID Reserved)
80 {
81 g_hInst = hDllHandle;
82 return TRUE;
83 }
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 #ifndef __KEYHOOK_H_H
5 #define __KEYHOOK_H_H
6
7 #ifdef MY_HOOK_API
8 #else
9 #define MY_HOOK_API _declspec(dllimport)
10 #endif
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 MY_HOOK_API BOOL __stdcall InstallHook();
17 MY_HOOK_API BOOL __stdcall UnInstallHook();
18
19 #ifdef __cplusplus
20 }
21 #endif
22
23 #endif
1 // 说明:Def文件KeyRedir.def
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 LIBRARY "KeyRedir"
5
6 EXPORTS
7 InstallHook
8 UnInstallHook
1 // 说明:源文件KeyRedir.cpp
2 // 作者:代码疯子
3 // Blog: http://www.programlife.net/
4 #include <windows.h>
5
6 #pragma warning(disable:4996)
7
8 #define MY_HOOK_API _declspec(dllexport)
9 #include "KeyRedir.h"
10
11 HINSTANCE g_hInst = NULL;
12 static HHOOK g_hHook = NULL;
13
14 BYTE g_bVK[4] = {VK_LEFT, VK_DOWN, VK_RIGHT, VK_UP};
15
16 LRESULT CALLBACK LowLevelKeyboardProc(
17 int code,
18 WPARAM wParam,
19 LPARAM lParam)
20 {
21 if(code == HC_ACTION)
22 {
23 PKBDLLHOOKSTRUCT pStruct = (PKBDLLHOOKSTRUCT)lParam;
24 DWORD dwIndex = 0;
25 switch (pStruct->vkCode)
26 {
27 case L'A':
28 case L'a':
29 dwIndex = 0;
30 break;
31
32 case L'S':
33 case L's':
34 dwIndex = 1;
35 break;
36
37 case L'D':
38 case L'd':
39 dwIndex = 2;
40 break;
41
42 case L'W':
43 case L'w':
44 dwIndex = 3;
45 break;
46
47 default:
48 return CallNextHookEx(g_hHook, code, wParam, lParam);
49 }
50 if (wParam == WM_KEYDOWN)
51 {
52 keybd_event(g_bVK[dwIndex], MapVirtualKey(g_bVK[dwIndex], 0), 0, 0);
53 }
54 else if (wParam == WM_KEYUP)
55 {
56 keybd_event(g_bVK[dwIndex], MapVirtualKey(g_bVK[dwIndex], 0), KEYEVENTF_KEYUP, 0);
57 }
58 return TRUE;
59 }
60 return CallNextHookEx(g_hHook, code, wParam, lParam);
61 }
62
63 BOOL __stdcall InstallHook()
64 {
65 g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
66 LowLevelKeyboardProc,
67 g_hInst,
68 NULL);
69 return g_hHook == NULL ? FALSE : TRUE;
70 }
71
72 BOOL __stdcall UnInstallHook()
73 {
74 return UnhookWindowsHookEx(g_hHook);
75 }
76
77 BOOL WINAPI DllMain(IN HINSTANCE hDllHandle,
78 IN DWORD nReason,
79 IN LPVOID Reserved)
80 {
81 g_hInst = hDllHandle;
82 return TRUE;
83 }
追问
我想问的是SetWindowsHookEx在目标进程挂了键盘钩子后,不用按键就能直接加载DLL,用sendmessage,postmessage都失败,用的是::SetWindowsHookExA(WH_KEYBOARD,&Gameproc,::GetModuleHandleA("hlddz.dll"),tid);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询