source: dev/branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp@ 790

Last change on this file since 790 was 787, checked in by イグトランス (egtra), 14 years ago

旧Windows対応箇所で、動的な初期化を持つ静的変数の使用が9xでエラーを起こしていたため、それを回避する形に修正。

File size: 4.8 KB
RevLine 
[778]1#include "stdafx.h"
2
[787]3static OSVERSIONINFO GetVersionEx2()
[778]4{
5 OSVERSIONINFO vi = {sizeof vi};
6 GetVersionEx(&vi);
7 return vi;
8}
9
10static OSVERSIONINFO const vi = GetVersionEx2();
11
[787]12
13
14static HMODULE hmodKernel;
15
16static HMODULE GetKernelModule()
[785]17{
[787]18 if (hmodKernel == nullptr)
19 {
20 hmodKernel = GetModuleHandle(TEXT("kernel32"));
21 }
22 return hmodKernel;
[785]23}
[778]24
[787]25#define KERNEL32 (GetKernelModule())
[778]26
[787]27#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
28
29#define DECLARE_REAL_HOLDER(name) static decltype(name)* name ## _Real
30
[778]31extern "C"
32{
33 BOOL WINAPI IsDebuggerPresent_Helper()
34 {
[787]35 DECLARE_REAL_HOLDER(IsDebuggerPresent);
36 static bool initialized;
37 if (!initialized)
[778]38 {
[787]39 auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(KERNEL32, IsDebuggerPresent);
40 initialized = true;
41 }
42
43 if (IsDebuggerPresent_Real != nullptr)
44 {
[778]45 return IsDebuggerPresent_Real();
46 }
47 else
48 {
49 return FALSE;
50 }
51 }
52
53 void* WINAPI EncodePointer_Helper(void* p)
54 {
[787]55 DECLARE_REAL_HOLDER(EncodePointer);
56 static bool initialized;
57 if (!initialized)
[778]58 {
[787]59 auto EncodePointer_Real = GET_PROC_ADDRESS(KERNEL32, EncodePointer);
60 initialized = true;
61 }
62
63 if (EncodePointer_Real != nullptr)
64 {
[778]65 return EncodePointer_Real(p);
66 }
67 else
68 {
69 return reinterpret_cast<void*>(reinterpret_cast<DWORD>(p) ^ GetCurrentProcessId());
70 }
71 }
72
73 void* WINAPI DecodePointer_Helper(void* p)
74 {
[787]75 DECLARE_REAL_HOLDER(DecodePointer);
76 static bool initialized;
77 if (!initialized)
[778]78 {
[787]79 auto DecodePointer_Real = GET_PROC_ADDRESS(KERNEL32, DecodePointer);
80 initialized = true;
81 }
82
83 if (DecodePointer_Real != nullptr)
84 {
[778]85 return DecodePointer_Real(p);
86 }
87 else
88 {
89 return reinterpret_cast<void*>(reinterpret_cast<DWORD>(p) ^ GetCurrentProcessId());
90 }
91 }
92
93 BOOL WINAPI IsProcessorFeaturePresent_Helper(DWORD feture)
94 {
[787]95 DECLARE_REAL_HOLDER(IsProcessorFeaturePresent);
96 static bool initialized;
97 if (!initialized)
[778]98 {
[787]99 IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(KERNEL32, IsProcessorFeaturePresent);
100 initialized = true;
101 }
102
103 if (IsProcessorFeaturePresent_Real != nullptr)
104 {
[778]105 return IsProcessorFeaturePresent_Real(feture);
106 }
107 else
108 {
109 assert(feture == PF_NX_ENABLED); // ATLから呼ばれる
110 return FALSE;
111 }
112 }
113
114 BOOL WINAPI HeapSetInformation_Helper(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
115 {
[787]116 DECLARE_REAL_HOLDER(HeapSetInformation);
117 static bool initialized;
118 if (!initialized)
[778]119 {
[787]120 HeapSetInformation_Real = GET_PROC_ADDRESS(KERNEL32, HeapSetInformation);
121 initialized = true;
122 }
123
124 if (HeapSetInformation_Real != nullptr)
125 {
[778]126 return HeapSetInformation_Real(hHeap, hic, information, informationLength);
127 }
128 else
129 {
130 return TRUE;
131 }
132 }
133
[787]134 BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper(LPCRITICAL_SECTION lpcs, DWORD spinCount)
[778]135 {
[787]136 DECLARE_REAL_HOLDER(InitializeCriticalSectionAndSpinCount);
137 static bool initialized;
138 if (!initialized)
139 {
140 InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(KERNEL32, InitializeCriticalSectionAndSpinCount);
141 initialized = true;
142 }
[785]143
[787]144 __try
[778]145 {
[787]146 if (InitializeCriticalSectionAndSpinCount_Real != nullptr)
[778]147 {
[787]148 OSVERSIONINFO vi = {sizeof vi};
149 ::GetVersionEx(&vi);
150 BOOL ret = InitializeCriticalSectionAndSpinCount_Real(lpcs, spinCount);
151 return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
152 ? TRUE
153 : ret;
[778]154 }
[787]155 else
[778]156 {
[787]157 ::InitializeCriticalSection(lpcs);
158 return TRUE;
[778]159 }
160 }
[787]161 __except(EXCEPTION_EXECUTE_HANDLER)
162 {
163 }
164 return FALSE;
[778]165 }
166
167 // Interlocked(Push|Pop)EntrySListは、NXが有効な場合のみ呼ばれる。
168 // Windows XP以前はNX対応していないためInterlocked(Push|Pop)EntrySListが呼ばれることはない。
169 // そのため、GET_PROC_ADDRESSがnullptrを返すことはないと仮定している。
170 // なお、Interlocked(Push|Pop)EntrySListもWindows XPから搭載されている。
171
172 PSLIST_ENTRY WINAPI InterlockedPushEntrySList_Helper(PSLIST_HEADER head, PSLIST_ENTRY entry)
173 {
[787]174 DECLARE_REAL_HOLDER(InterlockedPushEntrySList);
175 if (InterlockedPushEntrySList_Real == nullptr)
176 {
177 InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPushEntrySList);
178 }
[778]179 assert(InterlockedPushEntrySList_Real != nullptr);
180 return InterlockedPushEntrySList_Real(head, entry);
181 }
182
183 PSLIST_ENTRY WINAPI InterlockedPopEntrySList_Helper(PSLIST_HEADER head)
184 {
[787]185 DECLARE_REAL_HOLDER(InterlockedPopEntrySList);
186 if (InterlockedPopEntrySList_Real == nullptr)
187 {
188 InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPopEntrySList);
189 }
[778]190 assert(InterlockedPopEntrySList_Real != nullptr);
191 return InterlockedPopEntrySList_Real(head);
192 }
193}
Note: See TracBrowser for help on using the repository browser.