Changeset 787 in dev for branches/egtra
- Timestamp:
- Jan 30, 2011, 2:16:08 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp
r785 r787 1 1 #include "stdafx.h" 2 2 3 #define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName)) 4 5 namespace 6 { 7 8 OSVERSIONINFO GetVersionEx2() 3 static OSVERSIONINFO GetVersionEx2() 9 4 { 10 5 OSVERSIONINFO vi = {sizeof vi}; … … 15 10 static OSVERSIONINFO const vi = GetVersionEx2(); 16 11 17 HMODULE GetKernelModule() 12 13 14 static HMODULE hmodKernel; 15 16 static HMODULE GetKernelModule() 18 17 { 19 static HMODULE hmod = GetModuleHandle(TEXT("kernel32")); 20 return hmod; 18 if (hmodKernel == nullptr) 19 { 20 hmodKernel = GetModuleHandle(TEXT("kernel32")); 21 } 22 return hmodKernel; 21 23 } 22 24 23 } 25 #define KERNEL32 (GetKernelModule()) 26 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 24 30 25 31 extern "C" … … 27 33 BOOL WINAPI IsDebuggerPresent_Helper() 28 34 { 29 static auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsDebuggerPresent); 30 if (IsDebuggerPresent_Real) 35 DECLARE_REAL_HOLDER(IsDebuggerPresent); 36 static bool initialized; 37 if (!initialized) 38 { 39 auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(KERNEL32, IsDebuggerPresent); 40 initialized = true; 41 } 42 43 if (IsDebuggerPresent_Real != nullptr) 31 44 { 32 45 return IsDebuggerPresent_Real(); … … 40 53 void* WINAPI EncodePointer_Helper(void* p) 41 54 { 42 static auto EncodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), EncodePointer); 43 if (EncodePointer_Real) 55 DECLARE_REAL_HOLDER(EncodePointer); 56 static bool initialized; 57 if (!initialized) 58 { 59 auto EncodePointer_Real = GET_PROC_ADDRESS(KERNEL32, EncodePointer); 60 initialized = true; 61 } 62 63 if (EncodePointer_Real != nullptr) 44 64 { 45 65 return EncodePointer_Real(p); … … 53 73 void* WINAPI DecodePointer_Helper(void* p) 54 74 { 55 static auto DecodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), DecodePointer); 56 if (DecodePointer_Real) 75 DECLARE_REAL_HOLDER(DecodePointer); 76 static bool initialized; 77 if (!initialized) 78 { 79 auto DecodePointer_Real = GET_PROC_ADDRESS(KERNEL32, DecodePointer); 80 initialized = true; 81 } 82 83 if (DecodePointer_Real != nullptr) 57 84 { 58 85 return DecodePointer_Real(p); … … 66 93 BOOL WINAPI IsProcessorFeaturePresent_Helper(DWORD feture) 67 94 { 68 static auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsProcessorFeaturePresent); 69 if (IsProcessorFeaturePresent_Real) 95 DECLARE_REAL_HOLDER(IsProcessorFeaturePresent); 96 static bool initialized; 97 if (!initialized) 98 { 99 IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(KERNEL32, IsProcessorFeaturePresent); 100 initialized = true; 101 } 102 103 if (IsProcessorFeaturePresent_Real != nullptr) 70 104 { 71 105 return IsProcessorFeaturePresent_Real(feture); … … 80 114 BOOL WINAPI HeapSetInformation_Helper(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength) 81 115 { 82 static auto HeapSetInformation_Real = GET_PROC_ADDRESS(GetKernelModule(), HeapSetInformation); 83 if (HeapSetInformation_Real) 116 DECLARE_REAL_HOLDER(HeapSetInformation); 117 static bool initialized; 118 if (!initialized) 119 { 120 HeapSetInformation_Real = GET_PROC_ADDRESS(KERNEL32, HeapSetInformation); 121 initialized = true; 122 } 123 124 if (HeapSetInformation_Real != nullptr) 84 125 { 85 126 return HeapSetInformation_Real(hHeap, hic, information, informationLength); … … 91 132 } 92 133 93 namespace94 {95 typedef decltype(InitializeCriticalSectionAndSpinCount)* PInitializeCriticalSectionAndSpinCount;96 97 // 関数内静的変数とSEHは両立できないようなので別の関数へ分離98 BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper2(PInitializeCriticalSectionAndSpinCount pfn, LPCRITICAL_SECTION lpcs, DWORD spinCount)99 {100 __try101 {102 if (pfn != nullptr)103 {104 BOOL ret = pfn(lpcs, spinCount);105 return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS106 ? TRUE107 : ret;108 }109 else110 {111 ::InitializeCriticalSection(lpcs);112 return TRUE;113 }114 }115 __except(EXCEPTION_EXECUTE_HANDLER)116 {117 }118 ::SetLastError(ERROR_OUTOFMEMORY);119 return FALSE;120 }121 }122 123 134 BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper(LPCRITICAL_SECTION lpcs, DWORD spinCount) 124 135 { 125 static auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(GetKernelModule(), InitializeCriticalSectionAndSpinCount); 126 return InitializeCriticalSectionAndSpinCount_Helper2(InitializeCriticalSectionAndSpinCount_Real, lpcs, spinCount); 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 } 143 144 __try 145 { 146 if (InitializeCriticalSectionAndSpinCount_Real != nullptr) 147 { 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; 154 } 155 else 156 { 157 ::InitializeCriticalSection(lpcs); 158 return TRUE; 159 } 160 } 161 __except(EXCEPTION_EXECUTE_HANDLER) 162 { 163 } 164 return FALSE; 127 165 } 128 166 … … 134 172 PSLIST_ENTRY WINAPI InterlockedPushEntrySList_Helper(PSLIST_HEADER head, PSLIST_ENTRY entry) 135 173 { 136 static auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPushEntrySList); 174 DECLARE_REAL_HOLDER(InterlockedPushEntrySList); 175 if (InterlockedPushEntrySList_Real == nullptr) 176 { 177 InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPushEntrySList); 178 } 137 179 assert(InterlockedPushEntrySList_Real != nullptr); 138 180 return InterlockedPushEntrySList_Real(head, entry); … … 141 183 PSLIST_ENTRY WINAPI InterlockedPopEntrySList_Helper(PSLIST_HEADER head) 142 184 { 143 static auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPopEntrySList); 185 DECLARE_REAL_HOLDER(InterlockedPopEntrySList); 186 if (InterlockedPopEntrySList_Real == nullptr) 187 { 188 InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPopEntrySList); 189 } 144 190 assert(InterlockedPopEntrySList_Real != nullptr); 145 191 return InterlockedPopEntrySList_Real(head);
Note:
See TracChangeset
for help on using the changeset viewer.