1 |
// VS2005でビルドされたバイナリが Windows95 でも起動できるようにするために、 |
// VS2005でビルドされたバイナリが Windows95 でも起動できるようにするために、 |
2 |
// IsDebuggerPresent()のシンボル定義を追加する。 |
// IsDebuggerPresent()のシンボル定義を追加する。 |
3 |
// |
// |
4 |
// cf.http://jet2.u-abel.net/program/tips/forceimp.htm |
// cf.http://jet2.u-abel.net/program/tips/forceimp.htm |
5 |
|
|
6 |
// 装飾された名前のアドレスを作るための仮定義 |
// 装飾された名前のアドレスを作るための仮定義 |
7 |
// (これだけでインポートを横取りしている) |
// (これだけでインポートを横取りしている) |
8 |
#ifdef __cplusplus |
#ifdef __cplusplus |
9 |
extern "C" { |
extern "C" { |
10 |
#endif |
#endif |
11 |
int WINAPI _imp__IsDebuggerPresent() |
int WINAPI _imp__IsDebuggerPresent() |
12 |
{ return PtrToInt((void*) &_imp__IsDebuggerPresent); } |
{ return PtrToInt((void*) &_imp__IsDebuggerPresent); } |
13 |
#ifdef __cplusplus |
#ifdef __cplusplus |
14 |
} |
} |
15 |
#endif |
#endif |
16 |
|
|
17 |
// 実際に横取り処理を行う関数 |
// 実際に横取り処理を行う関数 |
18 |
#ifdef __cplusplus |
#ifdef __cplusplus |
19 |
extern "C" { |
extern "C" { |
20 |
#endif |
#endif |
21 |
BOOL WINAPI Cover_IsDebuggerPresent() |
BOOL WINAPI Cover_IsDebuggerPresent() |
22 |
{ return FALSE; } |
{ return FALSE; } |
23 |
#ifdef __cplusplus |
#ifdef __cplusplus |
24 |
} |
} |
25 |
#endif |
#endif |
26 |
|
|
27 |
// 関数が実際に呼び出されたときに備えて |
// 関数が実際に呼び出されたときに備えて |
28 |
// 横取り処理関数を呼び出させるための下準備 |
// 横取り処理関数を呼び出させるための下準備 |
29 |
#ifdef __cplusplus |
#ifdef __cplusplus |
30 |
extern "C" { |
extern "C" { |
31 |
#endif |
#endif |
32 |
void __stdcall DoCover_IsDebuggerPresent() |
void __stdcall DoCover_IsDebuggerPresent() |
33 |
{ |
{ |
34 |
DWORD dw; |
DWORD dw; |
35 |
DWORD_PTR FAR* lpdw; |
DWORD_PTR FAR* lpdw; |
36 |
OSVERSIONINFO osvi; |
OSVERSIONINFO osvi; |
37 |
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
38 |
// Windows95 でなければここでおわり |
// Windows95 でなければここでおわり |
39 |
GetVersionEx(&osvi); |
GetVersionEx(&osvi); |
40 |
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT || |
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT || |
41 |
// VER_PLATFORM_WIN32_WINDOWS なら、あとは Minor だけで判定できる |
// VER_PLATFORM_WIN32_WINDOWS なら、あとは Minor だけで判定できる |
42 |
// osvi.dwMajorVersion > 4 || |
// osvi.dwMajorVersion > 4 || |
43 |
osvi.dwMinorVersion > 0) { |
osvi.dwMinorVersion > 0) { |
44 |
return; |
return; |
45 |
} |
} |
46 |
// 横取り関数を設定するアドレスを取得 |
// 横取り関数を設定するアドレスを取得 |
47 |
lpdw = (DWORD_PTR FAR*) &_imp__IsDebuggerPresent; |
lpdw = (DWORD_PTR FAR*) &_imp__IsDebuggerPresent; |
48 |
// このアドレスを書き込めるように設定 |
// このアドレスを書き込めるように設定 |
49 |
// (同じプログラム内なので障害なく行える) |
// (同じプログラム内なので障害なく行える) |
50 |
VirtualProtect(lpdw, sizeof(DWORD_PTR), PAGE_READWRITE, &dw); |
VirtualProtect(lpdw, sizeof(DWORD_PTR), PAGE_READWRITE, &dw); |
51 |
// 横取り関数を設定 |
// 横取り関数を設定 |
52 |
*lpdw = (DWORD_PTR)(FARPROC) Cover_IsDebuggerPresent; |
*lpdw = (DWORD_PTR)(FARPROC) Cover_IsDebuggerPresent; |
53 |
// 読み書きの状態を元に戻す |
// 読み書きの状態を元に戻す |
54 |
VirtualProtect(lpdw, sizeof(DWORD_PTR), dw, NULL); |
VirtualProtect(lpdw, sizeof(DWORD_PTR), dw, NULL); |
55 |
} |
} |
56 |
#ifdef __cplusplus |
#ifdef __cplusplus |
57 |
} |
} |
58 |
#endif |
#endif |
59 |
|
|
60 |
// アプリケーションが初期化される前に下準備を呼び出す |
// アプリケーションが初期化される前に下準備を呼び出す |
61 |
// ※ かなり早くに初期化したいときは、このコードを |
// ※ かなり早くに初期化したいときは、このコードを |
62 |
// ファイルの末尾に書いて「#pragma init_seg(lib)」を、 |
// ファイルの末尾に書いて「#pragma init_seg(lib)」を、 |
63 |
// この変数宣言の手前に書きます。 |
// この変数宣言の手前に書きます。 |
64 |
// 初期化を急ぐ必要が無い場合は WinMain 内から |
// 初期化を急ぐ必要が無い場合は WinMain 内から |
65 |
// DoCover_IsDebuggerPresent を呼び出して構いません。 |
// DoCover_IsDebuggerPresent を呼び出して構いません。 |
66 |
/* C言語では以下のコードは、コンパイルエラーとなるので、WinMain, DllMain から呼ぶ。*/ |
/* C言語では以下のコードは、コンパイルエラーとなるので、WinMain, DllMain から呼ぶ。*/ |
67 |
#ifdef __cplusplus |
#ifdef __cplusplus |
68 |
extern "C" { |
extern "C" { |
69 |
int s_DoCover_IsDebuggerPresent |
int s_DoCover_IsDebuggerPresent |
70 |
= (int) (DoCover_IsDebuggerPresent(), 0); |
= (int) (DoCover_IsDebuggerPresent(), 0); |
71 |
} |
} |
72 |
#endif |
#endif |