Index: branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp
===================================================================
--- branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp	(revision 785)
+++ branches/egtra/ab5.0/abdev/OldWindowsHelperImpl.cpp	(revision 787)
@@ -1,10 +1,5 @@
 #include "stdafx.h"
 
-#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
-
-namespace
-{
-
-OSVERSIONINFO GetVersionEx2()
+static OSVERSIONINFO GetVersionEx2()
 {
 	OSVERSIONINFO vi = {sizeof vi};
@@ -15,11 +10,22 @@
 static OSVERSIONINFO const vi = GetVersionEx2();
 
-HMODULE GetKernelModule()
+
+
+static HMODULE hmodKernel;
+
+static HMODULE GetKernelModule()
 {
-	static HMODULE hmod = GetModuleHandle(TEXT("kernel32"));
-	return hmod;
+	if (hmodKernel == nullptr)
+	{
+		hmodKernel = GetModuleHandle(TEXT("kernel32"));
+	}
+	return hmodKernel;
 }
 
-}
+#define KERNEL32 (GetKernelModule())
+
+#define GET_PROC_ADDRESS(hmod, functionName) reinterpret_cast<decltype(functionName)*>(GetProcAddress((hmod), #functionName))
+
+#define DECLARE_REAL_HOLDER(name) static decltype(name)* name ## _Real
 
 extern "C"
@@ -27,6 +33,13 @@
 	BOOL WINAPI IsDebuggerPresent_Helper()
 	{
-		static auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsDebuggerPresent);
-		if (IsDebuggerPresent_Real)
+		DECLARE_REAL_HOLDER(IsDebuggerPresent);
+		static bool initialized;
+		if (!initialized)
+		{
+			auto IsDebuggerPresent_Real = GET_PROC_ADDRESS(KERNEL32, IsDebuggerPresent);
+			initialized = true;
+		}
+
+		if (IsDebuggerPresent_Real != nullptr)
 		{
 			return IsDebuggerPresent_Real();
@@ -40,6 +53,13 @@
 	void* WINAPI EncodePointer_Helper(void* p)
 	{
-		static auto EncodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), EncodePointer);
-		if (EncodePointer_Real)
+		DECLARE_REAL_HOLDER(EncodePointer);
+		static bool initialized;
+		if (!initialized)
+		{
+			auto EncodePointer_Real = GET_PROC_ADDRESS(KERNEL32, EncodePointer);
+			initialized = true;
+		}
+
+		if (EncodePointer_Real != nullptr)
 		{
 			return EncodePointer_Real(p);
@@ -53,6 +73,13 @@
 	void* WINAPI DecodePointer_Helper(void* p)
 	{
-		static auto DecodePointer_Real = GET_PROC_ADDRESS(GetKernelModule(), DecodePointer);
-		if (DecodePointer_Real)
+		DECLARE_REAL_HOLDER(DecodePointer);
+		static bool initialized;
+		if (!initialized)
+		{
+			auto DecodePointer_Real = GET_PROC_ADDRESS(KERNEL32, DecodePointer);
+			initialized = true;
+		}
+
+		if (DecodePointer_Real != nullptr)
 		{
 			return DecodePointer_Real(p);
@@ -66,6 +93,13 @@
 	BOOL WINAPI IsProcessorFeaturePresent_Helper(DWORD feture)
 	{
-		static auto IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(GetKernelModule(), IsProcessorFeaturePresent);
-		if (IsProcessorFeaturePresent_Real)
+		DECLARE_REAL_HOLDER(IsProcessorFeaturePresent);
+		static bool initialized;
+		if (!initialized)
+		{
+			IsProcessorFeaturePresent_Real = GET_PROC_ADDRESS(KERNEL32, IsProcessorFeaturePresent);
+			initialized = true;
+		}
+
+		if (IsProcessorFeaturePresent_Real != nullptr)
 		{
 			return IsProcessorFeaturePresent_Real(feture);
@@ -80,6 +114,13 @@
 	BOOL WINAPI HeapSetInformation_Helper(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
 	{
-		static auto HeapSetInformation_Real = GET_PROC_ADDRESS(GetKernelModule(), HeapSetInformation);
-		if (HeapSetInformation_Real)
+		DECLARE_REAL_HOLDER(HeapSetInformation);
+		static bool initialized;
+		if (!initialized)
+		{
+			HeapSetInformation_Real = GET_PROC_ADDRESS(KERNEL32, HeapSetInformation);
+			initialized = true;
+		}
+
+		if (HeapSetInformation_Real != nullptr)
 		{
 			return HeapSetInformation_Real(hHeap, hic, information, informationLength);
@@ -91,38 +132,35 @@
 	}
 
-	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)
 	{
-		static auto InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(GetKernelModule(), InitializeCriticalSectionAndSpinCount);
-		return InitializeCriticalSectionAndSpinCount_Helper2(InitializeCriticalSectionAndSpinCount_Real, lpcs, spinCount);
+		DECLARE_REAL_HOLDER(InitializeCriticalSectionAndSpinCount);
+		static bool initialized;
+		if (!initialized)
+		{
+			InitializeCriticalSectionAndSpinCount_Real = GET_PROC_ADDRESS(KERNEL32, InitializeCriticalSectionAndSpinCount);
+			initialized = true;
+		}
+
+		__try
+		{
+			if (InitializeCriticalSectionAndSpinCount_Real != nullptr)
+			{
+				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;
 	}
 
@@ -134,5 +172,9 @@
 	PSLIST_ENTRY WINAPI InterlockedPushEntrySList_Helper(PSLIST_HEADER head, PSLIST_ENTRY entry)
 	{
-		static auto InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPushEntrySList);
+		DECLARE_REAL_HOLDER(InterlockedPushEntrySList);
+		if (InterlockedPushEntrySList_Real == nullptr)
+		{
+			 InterlockedPushEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPushEntrySList);
+		}
 		assert(InterlockedPushEntrySList_Real != nullptr);
 		return InterlockedPushEntrySList_Real(head, entry);
@@ -141,5 +183,9 @@
 	PSLIST_ENTRY WINAPI InterlockedPopEntrySList_Helper(PSLIST_HEADER head)
 	{
-		static auto InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(GetKernelModule(), InterlockedPopEntrySList);
+		DECLARE_REAL_HOLDER(InterlockedPopEntrySList);
+		if (InterlockedPopEntrySList_Real == nullptr)
+		{
+			 InterlockedPopEntrySList_Real = GET_PROC_ADDRESS(KERNEL32, InterlockedPopEntrySList);
+		}
 		assert(InterlockedPopEntrySList_Real != nullptr);
 		return InterlockedPopEntrySList_Real(head);
