Index: branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp
===================================================================
--- branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp	(revision 784)
+++ branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp	(revision 785)
@@ -1,5 +1,10 @@
 #include "stdafx.h"
 
-static OSVERSIONINFO GetVersionEx2()
+#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
+
+namespace
+{
+
+OSVERSIONINFO GetVersionEx2()
 {
 	OSVERSIONINFO vi = {sizeof vi};
@@ -10,7 +15,11 @@
 static OSVERSIONINFO const vi = GetVersionEx2();
 
-#define KERNEL32 (GetModuleHandle(TEXT("kernel32")))
+HMODULE GetKernelModule()
+{
+	static HMODULE hmod = GetModuleHandle(TEXT("kernel32"));
+	return hmod;
+}
 
-#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
+}
 
 extern "C"
@@ -18,5 +27,6 @@
 	BOOL WINAPI IsDebuggerPresent_Helper()
 	{
-		if (auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(KERNEL32, IsDebuggerPresent))
+		static auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsDebuggerPresent);
+		if (IsDebuggerPresent_Real)
 		{
 			return IsDebuggerPresent_Real();
@@ -30,5 +40,6 @@
 	void* WINAPI EncodePointer_Helper(void* p)
 	{
-		if (auto EncodePointer_Real = GET_PROC_ADDRESS(KERNEL32, EncodePointer))
+		static auto EncodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), EncodePointer);
+		if (EncodePointer_Real)
 		{
 			return EncodePointer_Real(p);
@@ -42,5 +53,6 @@
 	void* WINAPI DecodePointer_Helper(void* p)
 	{
-		if (auto DecodePointer_Real = GET_PROC_ADDRESS(KERNEL32, DecodePointer))
+		static auto DecodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), DecodePointer);
+		if (DecodePointer_Real)
 		{
 			return DecodePointer_Real(p);
@@ -54,5 +66,6 @@
 	BOOL WINAPI IsProcessorFeaturePresent_Helper(DWORD feture)
 	{
-		if (auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(KERNEL32, IsProcessorFeaturePresent))
+		static auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsProcessorFeaturePresent);
+		if (IsProcessorFeaturePresent_Real)
 		{
 			return IsProcessorFeaturePresent_Real(feture);
@@ -67,5 +80,6 @@
 	BOOL WINAPI HeapSetInformation_Helper(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
 	{
-		if (auto HeapSetInformation_Real = GET_PROC_ADDRESS(KERNEL32, HeapSetInformation))
+		static auto HeapSetInformation_Real = GET_PROC_ADDRESS(GetKernelModule(), HeapSetInformation);
+		if (HeapSetInformation_Real)
 		{
 			return HeapSetInformation_Real(hHeap, hic, information, informationLength);
@@ -77,27 +91,38 @@
 	}
 
+	namespace
+	{
+		typedef decltype(InitializeCriticalSectionAndSpinCount)* PInitializeCriticalSectionAndSpinCount;
+
+		// 関数内静的変数とSEHは両立できないようなので別の関数へ分離
+		BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper2(PInitializeCriticalSectionAndSpinCount pfn, LPCRITICAL_SECTION lpcs, DWORD spinCount)
+		{
+			__try
+			{
+				if (pfn != nullptr)
+				{
+					BOOL ret = pfn(lpcs, spinCount);
+					return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
+						? TRUE
+						: ret;
+				}
+				else
+				{
+					::InitializeCriticalSection(lpcs);
+					return TRUE;
+				}
+			}
+			__except(EXCEPTION_EXECUTE_HANDLER)
+			{
+			}
+			::SetLastError(ERROR_OUTOFMEMORY);
+			return FALSE;
+		}
+	}
+
 	BOOL WINAPI InitializeCriticalSectionAndSpinCount_Helper(LPCRITICAL_SECTION lpcs, DWORD spinCount)
 	{
-		__try
-		{
-			if (auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(KERNEL32, InitializeCriticalSectionAndSpinCount))
-			{
-				OSVERSIONINFO vi = {sizeof vi};
-				::GetVersionEx(&vi);
-				BOOL ret = InitializeCriticalSectionAndSpinCount_Real(lpcs, spinCount);
-				return vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
-					? TRUE
-					: ret;
-			}
-			else
-			{
-				::InitializeCriticalSection(lpcs);
-				return TRUE;
-			}
-		}
-		__except(EXCEPTION_EXECUTE_HANDLER)
-		{
-		}
-		return FALSE;
+		static auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(GetKernelModule(), InitializeCriticalSectionAndSpinCount);
+		return InitializeCriticalSectionAndSpinCount_Helper2(InitializeCriticalSectionAndSpinCount_Real, lpcs, spinCount);
 	}
 
@@ -109,5 +134,5 @@
 	PSLIST_ENTRY WINAPI InterlockedPushEntrySList_Helper(PSLIST_HEADER head, PSLIST_ENTRY entry)
 	{
-		auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPushEntrySList);
+		static auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPushEntrySList);
 		assert(InterlockedPushEntrySList_Real != nullptr);
 		return InterlockedPushEntrySList_Real(head, entry);
@@ -116,5 +141,5 @@
 	PSLIST_ENTRY WINAPI InterlockedPopEntrySList_Helper(PSLIST_HEADER head)
 	{
-		auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPopEntrySList);
+		static auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPopEntrySList);
 		assert(InterlockedPopEntrySList_Real != nullptr);
 		return InterlockedPopEntrySList_Real(head);
