微软最后一个可以原生编译Win2000应用程序IDE是Visual Studio 2008,后续版本只能支持到WinXP。那么有什么办法让高版本的VS编译出可以在Win2000上运行的程序呢?
首先我们要了解为什么编译出的程序无法在Win2000上运行,首先一个原因是EXE可执行文件NTHEADER中的可选头有一个操作系统版本和子系统版本,这个如果是支持XP,则值为5.1,如果是2000,则必须是5.0。
另外一个原因就是Win2000上kernel32.dll,缺失了一些关键的API,例如EncodePointer、DecodePointer等等。
有一个方案是安装多个版本的VS,然后配置Daffodil.ENU.msi这个补丁来让高版本的VS能够使用低版本的Toolset。但是这有两个个问题,首先如果让高版本的VS识别VC2008的工具集就必须安装VS2010,这样就导致你的机器至少有3个版本的VS。其次就是虽然你可以使用VC2008的工具集了,但是你的编译器也是VC2008,并不能享受高版本VC的特性,例如C99,VC++11等等。而且据我所知VC2013是第一个完整支持C99的版本,VC2015是第一个完整支持VC++11的版本。
此文给出老外的一个解决办法,这里只做整理,原理很简单,就是通过链接时符号替换。
首先创建一个最简单的控制台工程。只打印出hello world即可。
添加impapi.asm文件,内容如下
.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,内容如下
#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右键属性,配置常规项类型为 Microsoft Macro Assembler。
最后需要用PE编辑工具编辑SubSystem版本号为5.0
尝试在win2000上运行通过。
上述代码参考来源
https://stackoverflow.com/questions/19516796/visual-studio-2012-win32-project-targeting-windows-2000