OSDN Git Service

8812dd73b718cc51939497e308027fb8632494b0
[dokopop/dokopop.git] / DCHook64 / dchk64 / dchk64.cpp
1 // dchk64.cpp : \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83G\83\93\83g\83\8a \83|\83C\83\93\83g\82ð\92è\8b`\82µ\82Ü\82·\81B
2 //
3
4 #include "stdafx.h"
5 #include "dchk64.h"
6 #include "../../DCHookTest/HookLoader.h"
7 #include "../DCHook64.h"
8
9 #define MAX_LOADSTRING 100
10
11 // \83O\83\8d\81[\83o\83\8b\95Ï\90\94:
12 HINSTANCE hInst;                                                                // \8c»\8dÝ\82Ì\83C\83\93\83^\81[\83t\83F\83C\83X
13 TCHAR szTitle[MAX_LOADSTRING];                                  // \83^\83C\83g\83\8b \83o\81[\82Ì\83e\83L\83X\83g
14 TCHAR szWindowClass[MAX_LOADSTRING];                    // \83\81\83C\83\93 \83E\83B\83\93\83h\83\83N\83\89\83X\96¼
15
16 const char *StrDCHOOKDLL = "DKPUHK64.dll";
17 THookLoader HookDll(StrDCHOOKDLL);
18
19 HWND hWnd = NULL;
20 HWND hwndParent = NULL;
21 HANDLE hMutex = NULL;
22 bool WindowsNT = true;
23
24 #ifdef _DEBUG
25 #define DBW     dbw
26 #else
27 #define DBW
28 #endif
29 void dbw( const char *format, ... );
30
31 // \82±\82Ì\83R\81[\83\83\82\83W\83\85\81[\83\8b\82É\8aÜ\82Ü\82ê\82é\8aÖ\90\94\82Ì\90é\8c¾\82ð\93]\91\97\82µ\82Ü\82·:
32 ATOM MyRegisterClass(HINSTANCE hInstance);
33 BOOL InitInstance(HINSTANCE, LPTSTR lpCmdLine, int);
34 void Cleanup();
35 LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
36
37 int atox( const TCHAR *str, TCHAR **next );
38
39 int APIENTRY _tWinMain(HINSTANCE hInstance,
40                      HINSTANCE hPrevInstance,
41                      LPTSTR    lpCmdLine,
42                      int       nCmdShow)
43 {
44         UNREFERENCED_PARAMETER(hPrevInstance);
45         //UNREFERENCED_PARAMETER(lpCmdLine);
46
47         MSG msg;
48         HACCEL hAccelTable;
49
50         // \83O\83\8d\81[\83o\83\8b\95\8e\9a\97ñ\82ð\8f\89\8aú\89»\82µ\82Ä\82¢\82Ü\82·\81B
51         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
52         LoadString(hInstance, IDC_DCHK64, szWindowClass, MAX_LOADSTRING);
53         MyRegisterClass(hInstance);
54
55         // \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\8f\89\8aú\89»\82ð\8eÀ\8ds\82µ\82Ü\82·:
56         if (!InitInstance (hInstance, lpCmdLine, nCmdShow))
57         {
58                 return FALSE;
59         }
60
61         hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DCHK64));
62
63         // \83\81\83C\83\93 \83\81\83b\83Z\81[\83\83\8b\81[\83v:
64         while (GetMessage(&msg, NULL, 0, 0))
65         {
66                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
67                 {
68                         TranslateMessage(&msg);
69                         DispatchMessage(&msg);
70                 }
71         }
72
73         Cleanup();
74
75         return (int) msg.wParam;
76 }
77
78
79
80 //
81 //  \8aÖ\90\94: MyRegisterClass()
82 //
83 //  \96Ú\93I: \83E\83B\83\93\83h\83\83N\83\89\83X\82ð\93o\98^\82µ\82Ü\82·\81B
84 //
85 //  \83R\83\81\83\93\83g:
86 //
87 //    \82±\82Ì\8aÖ\90\94\82¨\82æ\82Ñ\8eg\82¢\95û\82Í\81A'RegisterClassEx' \8aÖ\90\94\82ª\92Ç\89Á\82³\82ê\82½
88 //    Windows 95 \82æ\82è\91O\82Ì Win32 \83V\83X\83e\83\80\82Æ\8cÝ\8a·\82³\82¹\82é\8fê\8d\87\82É\82Ì\82Ý\95K\97v\82Å\82·\81B
89 //    \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82ª\81A\8aÖ\98A\95t\82¯\82ç\82ê\82½
90 //    \90³\82µ\82¢\8c`\8e®\82Ì\8f¬\82³\82¢\83A\83C\83R\83\93\82ð\8eæ\93¾\82Å\82«\82é\82æ\82¤\82É\82·\82é\82É\82Í\81A
91 //    \82±\82Ì\8aÖ\90\94\82ð\8cÄ\82Ñ\8fo\82µ\82Ä\82­\82¾\82³\82¢\81B
92 //
93 ATOM MyRegisterClass(HINSTANCE hInstance)
94 {
95         WNDCLASSEX wcex;
96
97         wcex.cbSize = sizeof(WNDCLASSEX);
98
99         wcex.style                      = CS_HREDRAW | CS_VREDRAW;
100         wcex.lpfnWndProc        = WndProc;
101         wcex.cbClsExtra         = 0;
102         wcex.cbWndExtra         = 0;
103         wcex.hInstance          = hInstance;
104         wcex.hIcon                      = NULL; //LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PDHK64));
105 #ifdef _DEBUG
106         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
107         wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);
108 #else
109         wcex.hCursor            = NULL; //LoadCursor(NULL, IDC_ARROW);
110         wcex.hbrBackground      = NULL; //(HBRUSH)(COLOR_WINDOW+1);
111 #endif
112         wcex.lpszMenuName       = NULL; //MAKEINTRESOURCE(IDC_PDHK64);
113         wcex.lpszClassName      = szWindowClass;
114         wcex.hIconSm            = NULL; //LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
115
116         return RegisterClassEx(&wcex);
117 }
118
119 //
120 //   \8aÖ\90\94: InitInstance(HINSTANCE, LPTSTR, int)
121 //
122 //   \96Ú\93I: \83C\83\93\83X\83^\83\93\83\83n\83\93\83h\83\8b\82ð\95Û\91\82µ\82Ä\81A\83\81\83C\83\93 \83E\83B\83\93\83h\83E\82ð\8dì\90¬\82µ\82Ü\82·\81B
123 //
124 //   \83R\83\81\83\93\83g:
125 //
126 //        \82±\82Ì\8aÖ\90\94\82Å\81A\83O\83\8d\81[\83o\83\8b\95Ï\90\94\82Å\83C\83\93\83X\83^\83\93\83\83n\83\93\83h\83\8b\82ð\95Û\91\82µ\81A
127 //        \83\81\83C\83\93 \83v\83\8d\83O\83\89\83\80 \83E\83B\83\93\83h\83E\82ð\8dì\90¬\82¨\82æ\82Ñ\95\\8e¦\82µ\82Ü\82·\81B
128 //
129 BOOL InitInstance(HINSTANCE hInstance, LPTSTR lpCmdLine, int nCmdShow)
130 {
131    hInst = hInstance; // \83O\83\8d\81[\83o\83\8b\95Ï\90\94\82É\83C\83\93\83X\83^\83\93\83X\8f\88\97\9d\82ð\8ai\94[\82µ\82Ü\82·\81B
132
133         TCHAR ModuleName[_MAX_PATH];
134         GetModuleFileName(NULL, ModuleName, sizeof(ModuleName)/sizeof(TCHAR));
135         hMutex = CreateMutex(NULL, FALSE, ModuleName);
136         if (GetLastError()==ERROR_ALREADY_EXISTS){
137                 return FALSE;   // dup launched
138         }
139
140 #ifdef _DEBUG
141         const int width = 800;
142         const int height = 400;
143 #else
144         const int width = 0;
145         const int height = 0;
146 #endif
147
148    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
149       CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, hInstance, NULL);
150
151    if (!hWnd)
152    {
153       return FALSE;
154    }
155
156         hwndParent = (HWND)(LONG_PTR)atox(lpCmdLine, NULL);
157         //DBW("hwndParent=%d", (int)(LONG_PTR)hwndParent);
158         if (hwndParent){
159                 PostMessage(hwndParent, UM_SHORTCUT, SCINX_NOTIFY64, (LPARAM)hWnd);
160                 //DBW("Posted");
161         }
162
163 #ifdef _DEBUG
164    ShowWindow(hWnd, nCmdShow);
165    UpdateWindow(hWnd);
166 #endif
167
168    return TRUE;
169 }
170
171 void Cleanup()
172 {
173         if (hMutex){
174                 CloseHandle(hMutex);
175                 hMutex = NULL;
176         }
177 }
178
179 //
180 //  \8aÖ\90\94: WndProc(HWND, UINT, WPARAM, LPARAM)
181 //
182 //  \96Ú\93I:  \83\81\83C\83\93 \83E\83B\83\93\83h\83E\82Ì\83\81\83b\83Z\81[\83W\82ð\8f\88\97\9d\82µ\82Ü\82·\81B
183 //
184 //  WM_COMMAND  - \83A\83v\83\8a\83P\81[\83V\83\87\83\93 \83\81\83j\83\85\81[\82Ì\8f\88\97\9d
185 //  WM_PAINT    - \83\81\83C\83\93 \83E\83B\83\93\83h\83E\82Ì\95`\89æ
186 //  WM_DESTROY  - \92\86\8e~\83\81\83b\83Z\81[\83W\82ð\95\\8e¦\82µ\82Ä\96ß\82é
187 //
188 //
189 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
190 {
191         //int wmId, wmEvent;
192         PAINTSTRUCT ps;
193         HDC hdc;
194
195         switch (message)
196         {
197         case WM_CREATE:
198                 HookDll.Load(hWnd);
199 #ifdef _DEBUG
200                 {
201                 if (!HookDll.Init(hWnd)){
202                         DBW("Hook.Init error");
203                 }
204 #if 0
205                 HookDll.Config(-1, KA_POPUP, KF_CONTROL);
206                 TDCHConfig cfg;
207                 memset(&cfg, 0, sizeof(cfg));
208                 cfg.UseAMODI = 0;
209                 cfg.MoveSend = 0;
210                 cfg.ScaleX = 0;
211                 cfg.ScaleY = 0;
212                 HookDll.Config2(&cfg);
213 #endif
214                 }
215 #endif
216                 break;
217 #if 0
218         case WM_COMMAND:
219                 wmId    = LOWORD(wParam);
220                 wmEvent = HIWORD(wParam);
221                 // \91I\91ð\82³\82ê\82½\83\81\83j\83\85\81[\82Ì\89ð\90Í:
222                 switch (wmId)
223                 {
224                 case IDM_ABOUT:
225                         DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
226                         break;
227                 case IDM_EXIT:
228                         DestroyWindow(hWnd);
229                         break;
230                 default:
231                         return DefWindowProc(hWnd, message, wParam, lParam);
232                 }
233                 break;
234 #endif
235         case WM_PAINT:
236                 hdc = BeginPaint(hWnd, &ps);
237                 EndPaint(hWnd, &ps);
238                 break;
239         case WM_DESTROY:
240                 PostQuitMessage(0);
241                 break;
242
243         case WM_COPYDATA:
244                 {
245                 COPYDATASTRUCT *cds = (COPYDATASTRUCT*)lParam;
246                 if ( !cds ) break;
247                 switch (cds->dwData){
248                         case HDC_Init:
249                                 {
250                                 HDCInitParam &p = *(HDCInitParam*)cds->lpData;
251                                 return HookDll.Init(p.hwnd);
252                                 }
253                         case HDC_Uninit:
254                                 HookDll.Uninit();
255                                 break;
256                         case HDC_Config:
257                                 {
258                                 HDCConfigParam &p = *(HDCConfigParam*)cds->lpData;
259                                 return HookDll.Config(p.clickonly, p.keyaction, p.keyflag);
260                                 }
261                         case HDC_Config2:
262                                 return HookDll.Config2((TDCHConfig*)cds->lpData);
263                         case HDC_Capture:
264                                 return HookDll.Capture();
265                         case HDC_CaptureAsync:
266                                 return HookDll.CaptureAsync();
267                         case HDC_CaptureAsyncWait:
268                                 return HookDll.CaptureAsyncWait();
269                         case HDC_Debug:
270                                 return HookDll.Debug();
271                 }
272                 }
273                 break;
274         default:
275                 return DefWindowProc(hWnd, message, wParam, lParam);
276         }
277         return 0;
278 }
279
280 int atox( const TCHAR *str, TCHAR **next )
281 {
282         unsigned val = 0;
283         while ( 1 )
284         {
285                 TCHAR c = *str;
286                 if ( c >= '0' && c <= '9' ){
287                         val = (val<<4) + (c - '0');
288                 } else if ( c >= 'A' && c <= 'F' ){
289                         val = (val<<4) + ( c - 'A' + 10 );
290                 } else if ( c >= 'a' && c <= 'f' ){
291                         val = (val<<4) + ( c - 'a' + 10 );
292                 } else {
293                         break;
294                 }
295                 str++;
296         }
297         if ( next ) *next = (TCHAR*)str;
298         return val;
299 }
300
301 static HWND hWin = NULL;
302 static const TCHAR *clsnameW = _T("TDbgMsgForm");
303 static const TCHAR *winnameW = _T("Debug Messenger");
304
305 void dbw( const char *format, ... )
306 {
307         if ( !hWin ){
308                 hWin = FindWindow( clsnameW, winnameW );
309                 if ( !hWin ) return;
310         }
311         va_list ap;
312         va_start( ap, format );
313         char buf[ 2048 ];
314         wvsprintfA( buf, format, ap );
315         COPYDATASTRUCT cds;
316         cds.dwData = 1; // Indicate String
317         cds.cbData = (DWORD)strlen(buf)+1;
318         cds.lpData = buf;
319         SendMessageA( hWin, WM_COPYDATA, NULL, (LPARAM)&cds );
320         va_end( ap );
321 }
322
323 static const char *MyWinTitle = "<Processing...>";
324 HANDLE WinExecEx( const char *cmd, int show, const char *dir, const char *title )
325 {
326         STARTUPINFO sui;
327         memset( &sui, 0, sizeof(STARTUPINFO) );
328         sui.cb = sizeof(STARTUPINFO);
329         sui.dwFlags = STARTF_USESHOWWINDOW;
330         sui.wShowWindow = (WORD)show;
331         sui.lpTitle = (LPTSTR)(title ? title : MyWinTitle);
332         PROCESS_INFORMATION pi;
333         if ( !CreateProcess( NULL, (LPTSTR)cmd, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, dir, &sui, &pi ) )
334                 return NULL;
335         return pi.hProcess;
336 }