微軟最后一個可以原生編譯Win2000應(yīng)用程序IDE是Visual Studio 2008,后續(xù)版本只能支持到WinXP。那么有什么辦法讓高版本的VS編譯出可以在Win2000上運行的程序呢?
首先我們要了解為什么編譯出的程序無法在Win2000上運行蹬屹,首先一個原因是EXE可執(zhí)行文件NTHEADER中的可選頭有一個操作系統(tǒng)版本和子系統(tǒng)版本,這個如果是支持XP,則值為5.1蟋滴,如果是2000染厅,則必須是5.0。
另外一個原因就是Win2000上kernel32.dll津函,缺失了一些關(guān)鍵的API糟秘,例如EncodePointer、DecodePointer等等球散。
有一個方案是安裝多個版本的VS尿赚,然后配置Daffodil.ENU.msi這個補丁來讓高版本的VS能夠使用低版本的Toolset。但是這有兩個個問題蕉堰,首先如果讓高版本的VS識別VC2008的工具集就必須安裝VS2010凌净,這樣就導(dǎo)致你的機器至少有3個版本的VS。其次就是雖然你可以使用VC2008的工具集了屋讶,但是你的編譯器也是VC2008冰寻,并不能享受高版本VC的特性,例如C99皿渗,VC++11等等斩芭。而且據(jù)我所知VC2013是第一個完整支持C99的版本,VC2015是第一個完整支持VC++11的版本乐疆。
此文給出老外的一個解決辦法划乖,這里只做整理,原理很簡單挤土,就是通過鏈接時符號替換琴庵。
首先創(chuàng)建一個最簡單的控制臺工程。只打印出hello world即可仰美。
添加impapi.asm文件迷殿,內(nèi)容如下
.586
.MODEL FLAT, STDCALL
EXTERN STDCALL i__GetModuleHandleExW@12:PROC
EXTERN STDCALL i__SetFilePointerEx@20:PROC
EXTERN STDCALL i__InitializeSListHead@4:PROC
EXTERN STDCALL i__EncodePointer@4:PROC
EXTERN STDCALL i__DecodePointer@4:PROC
EXTERN STDCALL i__HeapSetInformation@16:PROC
.DATA
__imp__InitializeSListHead@4 DWORD i__InitializeSListHead@4
__imp__InterlockedPushEntrySList@8 DWORD RtlInterlockedPushEntrySList
__imp__GetModuleHandleExW@12 DWORD i__GetModuleHandleExW@12
__imp__EncodePointer@4 DWORD i__EncodePointer@4
__imp__DecodePointer@4 DWORD i__DecodePointer@4
__imp__HeapSetInformation@16 DWORD i__HeapSetInformation@16
__imp__SetFilePointerEx@20 DWORD i__SetFilePointerEx@20
EXTERNDEF STDCALL __imp__InitializeSListHead@4:DWORD
EXTERNDEF STDCALL __imp__InterlockedPushEntrySList@8:DWORD
EXTERNDEF STDCALL __imp__GetModuleHandleExW@12:DWORD
EXTERNDEF STDCALL __imp__EncodePointer@4:DWORD
EXTERNDEF STDCALL __imp__DecodePointer@4:DWORD
EXTERNDEF STDCALL __imp__HeapSetInformation@16:DWORD
EXTERNDEF STDCALL __imp__SetFilePointerEx@20:DWORD
.CODE
InterlockedPushEntrySList PROC
push ebx
push ebp
mov ebp, ecx
mov ebx, edx
mov edx, [ebp+4]
mov eax, [ebp+0]
Epsh:
mov [ebx], eax
lea ecx, [edx+10001h]
lock cmpxchg8b qword ptr [ebp+0]
jnz Epsh
pop ebp
pop ebx
retn
InterlockedPushEntrySList ENDP
RtlInterlockedPushEntrySList PROC
mov edi, edi
push ebp
mov ebp, esp
mov edx, [ebp+0Ch] ; ListEntry
mov ecx, [ebp+08h] ; ListHead
call InterlockedPushEntrySList
pop ebp
retn 8
RtlInterlockedPushEntrySList ENDP
END
添加extapi.c,內(nèi)容如下
#include <windows.h>
typedef BOOL (WINAPI *PFN_GET_MODULE_HANDLE_EX_W)(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule);
typedef BOOL (WINAPI *PFN_SET_FILE_POINTER_EX)(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod);
typedef VOID (WINAPI *PFN_INITIALIZE_SLIST_HEAD)(PSLIST_HEADER ListHead);
typedef PVOID (WINAPI *PFN_ENCODE_POINTER)(PVOID Pointer);
typedef PVOID (WINAPI *PFN_DECODE_POINTER)(PVOID Pointer);
typedef BOOL (WINAPI *PFN_HEAP_SET_INFORMATION)(HANDLE HeapHandle, HEAP_INFORMATION_CLASS HeapInformationClass, PVOID HeapInformation, SIZE_T HeapInformationLength);
PFN_GET_MODULE_HANDLE_EX_W pfnGetModuleHandleExW;
PFN_SET_FILE_POINTER_EX pfnSetFilePointerEx;
PFN_INITIALIZE_SLIST_HEAD pfnInitializeSListHead;
PFN_ENCODE_POINTER pfnEncodePointer;
PFN_DECODE_POINTER pfnDecodePointer;
PFN_HEAP_SET_INFORMATION pfnHeapSetInformation;
BOOL HasAlreadyTryGetFunction;
static VOID TryGetFunction(VOID)
{
HANDLE hKernel32 = GetModuleHandleA("kernel32");
if(HasAlreadyTryGetFunction)
return;
pfnGetModuleHandleExW = (PFN_GET_MODULE_HANDLE_EX_W)GetProcAddress(hKernel32, "GetModuleHandleExW");
pfnSetFilePointerEx = (PFN_SET_FILE_POINTER_EX)GetProcAddress(hKernel32, "SetFilePointerEx");
pfnInitializeSListHead = (PFN_INITIALIZE_SLIST_HEAD)GetProcAddress(hKernel32, "InitializeSListHead");
pfnEncodePointer = (PFN_ENCODE_POINTER)GetProcAddress(hKernel32, "EncodePointer");
pfnDecodePointer = (PFN_DECODE_POINTER)GetProcAddress(hKernel32, "DecodePointer");
pfnHeapSetInformation = (PFN_HEAP_SET_INFORMATION)GetProcAddress(hKernel32, "HeapSetInformation");
HasAlreadyTryGetFunction = TRUE;
}
BOOL WINAPI i__GetModuleHandleExW(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule)
{
TryGetFunction();
if(pfnGetModuleHandleExW)
{
return pfnGetModuleHandleExW(dwFlags, lpModuleName, phModule);
}
//NOT FULLY IMPLEMENTED
//BasepGetModuleHandleExParameterValidation
if( phModule == NULL ||
(dwFlags & ~(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) ||
(dwFlags & GET_MODULE_HANDLE_EX_FLAG_PIN && dwFlags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT) ||
(lpModuleName == NULL && dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) )
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
*phModule = NULL;
//BasepGetModuleHandleExW
if(dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
{
MEMORY_BASIC_INFORMATION mbi;
SIZE_T size;
size = VirtualQuery(lpModuleName, &mbi, sizeof(mbi));
if(size >= sizeof(PVOID) * 2)
{
*phModule = (HANDLE)mbi.AllocationBase;
}
}
else
{
*phModule = GetModuleHandleW(lpModuleName);
}
return TRUE;
}
BOOL WINAPI i__SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
{
LARGE_INTEGER liDistanceToMoveTmp;
DWORD dwRet;
TryGetFunction();
if(pfnSetFilePointerEx)
{
return pfnSetFilePointerEx(hFile, liDistanceToMove, lpNewFilePointer, dwMoveMethod);
}
liDistanceToMoveTmp = liDistanceToMove;
dwRet = SetFilePointer(hFile, liDistanceToMoveTmp.LowPart, &liDistanceToMoveTmp.HighPart, dwMoveMethod);
if(dwRet != INVALID_SET_FILE_POINTER)
{
lpNewFilePointer->LowPart = dwRet;
lpNewFilePointer->HighPart = liDistanceToMoveTmp.HighPart;
return TRUE;
}
return FALSE;
}
VOID WINAPI i__InitializeSListHead(PSLIST_HEADER ListHead)
{
ListHead->Alignment = 0;
}
PVOID WINAPI i__EncodePointer(PVOID Pointer)
{
TryGetFunction();
if(pfnEncodePointer)
{
return pfnEncodePointer(Pointer);
}
//NOT IMPLEMENTED
return Pointer;
}
PVOID WINAPI i__DecodePointer(PVOID Pointer)
{
TryGetFunction();
if(pfnDecodePointer)
{
return pfnDecodePointer(Pointer);
}
//NOT IMPLEMENTED
return Pointer;
}
BOOL WINAPI i__HeapSetInformation(HANDLE HeapHandle, HEAP_INFORMATION_CLASS HeapInformationClass,
PVOID HeapInformation, SIZE_T HeapInformationLength)
{
TryGetFunction();
if(pfnHeapSetInformation)
return pfnHeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength);
//NOT IMPLEMENTED
return TRUE;
}
解決方案資源管理器咖杂,在項目上右鍵菜單選擇生成自定義庆寺,勾選masm。
在extapi1.asm右鍵屬性诉字,配置常規(guī)項類型為 Microsoft Macro Assembler懦尝。
最后需要用PE編輯工具編輯SubSystem版本號為5.0
嘗試在win2000上運行通過。
上述代碼參考來源
https://stackoverflow.com/questions/19516796/visual-studio-2012-win32-project-targeting-windows-2000