OSDN Git Service

ドキュメント修正
[dokopop/dokopop.git] / DCHookTest / HookLoader.cpp
1 #include <windows.h>
2 #pragma hdrstop
3 #include "stdafx.h"
4 #include "HookLoader.h"
5 #include "../DCHook64/DCHook64.h"
6 #include "Util.h"
7
8 #define HOOK64_SUBDIR   0               // x64\8aÖ\98A\83t\83@\83C\83\8b\82Ísub directory x64\82Ö
9
10 HWND hwnd64;
11
12 THookLoader::THookLoader(const char *dllname)
13         :DllName(dllname)
14 {
15         hDll = NULL;
16         DCHInit = NULL;
17         DCHConfig = NULL;
18         DCHConfig2 = NULL;
19         DCHCapture = NULL;
20         DCHWaitForCaptureResult = NULL;
21         DCHUninit = NULL;
22 #ifdef _DEBUG
23         DCDebug = NULL;
24 #endif
25
26         hThread = NULL;
27         dwThreadId = 0;
28 }
29 THookLoader::~THookLoader()
30 {
31         Uninit();
32 }
33 bool THookLoader::Load(HWND hwnd)
34 {
35         if (hDll)
36                 return true;
37
38         hDll = LoadLibrary( DllName );
39         if ( !hDll )
40         {
41                 GetLastError();
42 #ifndef _DEBUG
43                 DBW("Cannot load DLL:%s",DllName);
44 #endif
45                 // DKPPHK.DLL \82ª\82È\82¯\82ê\82ΠDCHOOK.DLL\82ðload
46                 DllName = "DCHOOK.DLL";
47
48                 hDll = LoadLibrary( DllName );
49                 if ( !hDll ){
50                         DBW("Cannot load DLL:%s",DllName);
51                         return false;
52                 }
53         }
54
55         DCHInit = (FNDCHInit)GetProcAddress( hDll, "Init" );
56         DCHUninit = (FNDCHUninit)GetProcAddress( hDll, "Uninit" );
57         DCHConfig = (FNDCHConfig)GetProcAddress( hDll, "Config" );
58         DCHConfig2 = (FNDCHConfig2)GetProcAddress( hDll, "Config2" );
59         DCHCapture = (FNDCHCapture)GetProcAddress( hDll, "Capture" );
60         DCHWaitForCaptureResult = (FNDCHWaitForCaptureResult)GetProcAddress( hDll, "WaitForCaptureResult" );
61 #ifdef _DEBUG
62         DCDebug = (FNDebug)GetProcAddress( hDll, "Debug" );
63 #endif
64
65         if (!DCHInit || !DCHUninit || !DCHConfig || !DCHConfig2 || !DCHCapture || !DCHWaitForCaptureResult){
66                 FreeLibrary(hDll);
67                 hDll = NULL;
68                 return false;
69         }
70
71 #if 0
72 #ifdef USE_UNICODE
73         const char *pVxdPath = NULL;
74 #else
75         AnsiString VxdPath;
76         if (!WindowsNT){
77                 // for Windows9x
78                 VxdPath = ExtractFilePath( Application->ExeName );
79                 char path[256];
80                 GetShortPathName( VxdPath.c_str(), path, sizeof(path) );
81                 VxdPath = path;
82                 VxdPath += "HK95D.VXD";
83         }
84         const char *pVxdPath = VxdPath.data();
85 #endif
86
87         if ( !DCHInit( hwnd, DllName, WindowsNT, pVxdPath ) ){
88                 FreeLibrary( hDll );
89                 hDll = NULL;
90                 DBW("Cannot initialized DLL");
91                 return false;
92         }
93 #endif
94
95         return true;
96 }
97
98 void THookLoader::Unload()
99 {
100         if (!hDll)
101                 return;
102         DCHUninit();
103         FreeLibrary(hDll);
104         hDll = NULL;
105         DCHUninit = NULL;
106 }
107
108 bool THookLoader::Init(HWND hwnd)
109 {
110         DBW("Init");
111         return DCHInit(hwnd, NULL, NULL, NULL);
112 }
113 void THookLoader::Uninit()
114 {
115         if (DCHUninit) DCHUninit();
116 }
117
118 int THookLoader::Capture()
119 {
120         if (!hDll)
121                 return 0;
122         return DCHCapture(NULL, NULL, true, false);
123 }
124 int THookLoader::CaptureAsync()
125 {
126         if (!hDll)
127                 return 0;
128         int ret = DCHCapture(NULL, NULL, true, true);
129         int result = DCHWaitForCaptureResult(false, 0);
130         DBW("result:%d", result);
131         if (result != 0){
132                 // wait data\82 \82è
133                 OpenThread();
134         }
135         return ret;
136 }
137 int THookLoader::CaptureAsyncWait()
138 {
139         return 0;
140 }
141
142 void THookLoader::OpenThread()
143 {
144         DBW("OpenThread: %08X", hThread);
145         if (hThread) return;
146
147         ThreadEnd = false;
148         hThread = CreateThread(NULL, 0, cbWaitCaptureThread, this, CREATE_SUSPENDED, &dwThreadId);
149         ResumeThread(hThread);
150 }
151
152 DWORD WINAPI THookLoader::cbWaitCaptureThread(LPVOID This)
153 {
154         ((THookLoader*)This)->WaitCaptureThread();
155         return 0;
156 }
157
158 void THookLoader::WaitCaptureThread()
159 {
160         DCHWaitForCaptureResult(true, 3000);
161         hThread = NULL;
162 }
163
164 static const char *MyWinTitle = "<Processing...in dchook>";
165 inline HANDLE __WinExec( const char *cmd, int show, const char *dir=NULL )
166 {
167         return WinExecEx(cmd, show, dir, MyWinTitle);
168 }
169
170 //#ifdef _DEBUG
171 #if 0
172 const char *StrDCHK64EXE = "..\\dchk64\\x64\\Debug\\dchk64.exe";        // for debug
173 #else
174 #if HOOK64_SUBDIR
175 const char *StrDCHK64EXE = "x64\\DKPUHK64.exe";
176 #else
177 const char *StrDCHK64EXE = "DKPUHK64.exe";
178 #endif
179 #endif
180
181 THookLoader64::THookLoader64(HWND hwnd_recv)
182         :hwndRecv(hwnd_recv)
183 {
184         hwndTarget = NULL;
185         hProc = NULL;
186         LoadPending = false;
187 }
188 THookLoader64::~THookLoader64()
189 {
190         if (hProc)
191                 CloseHandle(hProc);
192         Unload();
193 }
194 bool THookLoader64::Load(HWND hwnd)
195 {
196 #if 1   // Find already launched hook executer.
197         HWND hwndFound = FindWindow("dchk64", "DCHK64");
198 //      HWND hwndFound = FindApp("dchk64", "DCHK64", "");
199         if (hwndFound){
200                 DBW("x64:Found %08X already launched exe", hwndFound);
201                 hwndTarget = hwndFound;
202                 hwnd64 = hwndTarget;
203                 return true;
204         }
205 #endif
206 #if 1
207         char param[280];
208         wsprintf(param, "%s %08X", StrDCHK64EXE, hwndRecv);
209         hProc = __WinExec(param, SW_HIDE, 0);   // Launch dchk64.exe
210         if (!hProc){
211                 DBW("Launch failed: %s", param);
212                 return false;
213         }
214         LoadPending = true;
215         //WaitForInputIdle(hProc, 3000);
216 #endif
217         return true;
218 }
219 void THookLoader64::Unload()
220 {
221         if (!hProc)
222                 return;
223         Uninit();
224         TerminateProcess(hProc, 0);
225         CloseHandle(hProc);
226         hProc = NULL;
227         LoadPending = false;
228 }
229 bool THookLoader64::Init(HWND hwnd)
230 {
231         DBW("x64:Init hwndTarget=%08X", hwndTarget);
232         if (!hwndTarget){
233                 DBW("Init failed");
234                 return false;
235         }
236         DBW("x64:Init");
237         HDCInitParam p = {hwnd};
238         return Send(HDC_Init, &p, sizeof(p)) ? true : false;
239 }
240 void THookLoader64::Uninit( )
241 {
242         if (!hwndTarget)
243                 return ;
244         Send(HDC_Uninit, 0);
245         LoadPending = false;
246 }
247 int THookLoader64::Config( int clickonly, int keyaction, int keyflag )
248 {
249         if (!hwndTarget)
250                 return false;
251         HDCConfigParam p = {clickonly, keyaction, keyflag };
252         return Send(HDC_Config, &p, sizeof(p));
253 }
254 int THookLoader64::Config2( struct TDCHConfig *cfg )
255 {
256         if (!hwndTarget)
257                 return false;
258         return Send(HDC_Config2, cfg, sizeof(*cfg));
259 }
260 int THookLoader64::Capture()
261 {
262         if (!hwndTarget)
263                 return false;
264         return Send(HDC_Capture, 0);
265 }
266 int THookLoader64::CaptureAsync()
267 {
268         if (!hwndTarget)
269                 return false;
270         return Send(HDC_CaptureAsync, 0);
271 }
272 int THookLoader64::CaptureAsyncWait()
273 {
274         if (!hwndTarget)
275                 return false;
276         return Send(HDC_CaptureAsyncWait, 0);
277 }
278 int THookLoader64::Debug()
279 {
280         if (!hwndTarget)
281                 return false;
282         return Send(HDC_Debug, 0);
283 }
284
285 int THookLoader64::Send(int msg, void *data, int len)
286 {
287         COPYDATASTRUCT cds;
288         memset(&cds, 0, sizeof(cds));
289         cds.dwData = msg;
290         cds.lpData = data;
291         cds.cbData = len;
292         return (int)SendMessage(hwndTarget, WM_COPYDATA, 0, (LPARAM)&cds);
293 }
294