大家好日子,針對從Visual Studio 2012的Windows XP
我最近升級到Visual Studio 2012,我很喜歡改進的C++ 11支持和黑暗的主題。但是,我試圖編寫一個在Windows XP和更高版本上運行的程序,並且遇到了一些奇怪的問題。
我第一次嘗試在XP虛擬機上運行我的程序時,我收到了「這不是有效的Win32程序」錯誤消息。一些谷歌搜索顯示,我需要將更新1應用到VS以便能夠定位到Windows XP。我搜索了,但發現更新2。我申請了該工具,並將平臺工具集設置爲v110_xp,然後重新編譯了我的程序並嘗試再次運行它。這次我沒有收到任何錯誤信息,但在嘗試啓動程序時,我聽到XP的錯誤聲音(與MB_ICONERROR調用MessageBox時聽到的聲音相同),然後就沒有其他事情發生。在Windows XP的事件查看器中也沒有提及任何事情。我想也許更新2搞砸了其他的東西,所以我完全卸載了VS2012,包括它留下的所有MS SQL垃圾,重新安裝了它,並且只應用了更新1.再次將我的代碼與XP工具集編譯在一起,但是相同事情發生。嘗試啓動我的程序時出現錯誤,但沒有消息。
還有一些Google搜索顯示我必須將PSAPI_VERSION定義爲1才能定位Windows 7之前的Process API版本,所以我這樣做了,但問題仍然存在。
我開始認爲我的代碼有問題,所以我做了最基本的Hello World程序,但它仍然有同樣的問題。所以我現在沒有想法了。
這裏是我用來編譯Hello World程序的代碼:
main.cpp中:
#include "winapi.h"
int WINAPI wWinMain(HINSTANCE inst, HINSTANCE prev, wchar_t *cmdline, int show)
{
MessageBox(HWND_DESKTOP, L"Let's hope this works in Windows XP...", L"Testing 1... 2... 3...", MB_ICONERROR);
return 0;
}
winapi.h:
#ifndef WINAPI_H_INCLUDED
#define WINAPI_H_INCLUDED
#ifdef _WIN32
// WINDOWS DEFINES /////////////////////////////////////////////////////////////
// If this is not a console program, let the linker include a manifest to
// enable visual styles.
#ifndef _CONSOLE
# pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif // _CONSOLE
// Shorten compile time by only including the most basic Windows definitions.
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOSYSMETRICS // SM_*
#define NOICONS // IDI_*
#define NOKEYSTATES // MK_*
#define NOSYSCOMMANDS // SC_*
#define NORASTEROPS // Binary and Tertiary raster ops
#define OEMRESOURCE // OEM Resource values (OCR_NORMAL and related constants)
#define NOATOM // Atom Manager routines
#define NOCLIPBOARD // Clipboard routines
#define NODRAWTEXT // DrawText() and DT_*
#define NOKERNEL // All KERNEL defines and routines
#define NONLS // All NLS defines and routines
#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // typedef METAFILEPICT
#define NOMINMAX // Macros min(a,b) and max(a,b)
#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
#define NOSOUND // Sound driver routines
#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
#define NOWH // SetWindowsHook and WH_*
#define NOCOMM // COMM driver routines
#define NOKANJI // Kanji support stuff.
#define NOHELP // Help engine interface.
#define NOPROFILER // Profiler interface.
#define NODEFERWINDOWPOS // DeferWindowPos routines
#define NOMCX // Modem Configuration Extensions
// Enable strict typechecking on Windows types like HANDLE, HWND and HDC.
#define STRICT
// Enable targetting of pre-Win7 Process API functions when compiling on
// VS2012 or higher.
#if _MSC_VER >= 1700
# define PSAPI_VERSION 1
#endif // _MSC_VER
// Specify the minimum versions of Windows and Internet Explorer supported
// by this code.
#define NTDDI_VERSION NTDDI_WIN2K
#define _WIN32_WINNT _WIN32_WINNT_WIN2K
#define WINVER _WIN32_WINNT_WIN2K
#define _WIN32_IE _WIN32_IE_IE50
// WINDOWS INCLUDES ////////////////////////////////////////////////////////////
#include <Windows.h>
#else // _WIN32
// OTHER OS ////////////////////////////////////////////////////////////////////
#error This software has been written with Visual C++ in mind.
////////////////////////////////////////////////////////////////////////////////
#endif // _WIN32
#endif // WINAPI_H_INCLUDED
正如我已經說過了,我通過定位v110_xp工具集編譯此代碼,並使用項目設置窗口靜態鏈接CRT與/ MD。我將它編譯爲x86代碼,並且在我的Windows 7 x64機器上運行良好。在生成的可執行文件上運行dumpbin,確認它是編譯爲操作系統版本5.01和Windows GUI子系統的32位代碼。
爲了使這一儘可能完整,這裏是可執行的進口:
D:\Projects\xptest\Release>dumpbin /imports xptest.exe
Microsoft (R) COFF/PE Dumper Version 11.00.51106.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file xptest.exe
File Type: EXECUTABLE IMAGE
Section contains the following imports:
USER32.dll
4070FC Import Address Table
40B19C Import Name Table
0 time date stamp
0 Index of first forwarder reference
215 MessageBoxW
KERNEL32.dll
407000 Import Address Table
40B0A0 Import Name Table
0 time date stamp
0 Index of first forwarder reference
218 GetModuleHandleW
8F CreateFileW
187 GetCommandLineW
300 IsDebuggerPresent
304 IsProcessorFeaturePresent
202 GetLastError
473 SetLastError
2EF InterlockedIncrement
2EB InterlockedDecrement
1C5 GetCurrentThreadId
EA EncodePointer
CA DecodePointer
119 ExitProcess
217 GetModuleHandleExW
245 GetProcAddress
367 MultiByteToWideChar
264 GetStdHandle
525 WriteFile
214 GetModuleFileNameW
24A GetProcessHeap
1F3 GetFileType
2E3 InitializeCriticalSectionAndSpinCount
D1 DeleteCriticalSection
263 GetStartupInfoW
3A7 QueryPerformanceCounter
1C1 GetCurrentProcessId
279 GetSystemTimeAsFileTime
1DA GetEnvironmentStringsW
161 FreeEnvironmentStringsW
4D3 UnhandledExceptionFilter
4A5 SetUnhandledExceptionFilter
1C0 GetCurrentProcess
4C0 TerminateProcess
4C5 TlsAlloc
4C7 TlsGetValue
4C8 TlsSetValue
4C6 TlsFree
EE EnterCriticalSection
339 LeaveCriticalSection
2CF HeapFree
4B2 Sleep
30A IsValidCodePage
168 GetACP
237 GetOEMCP
172 GetCPInfo
33E LoadLibraryExW
38A OutputDebugStringW
33F LoadLibraryW
418 RtlUnwind
2CB HeapAlloc
2D2 HeapReAlloc
511 WideCharToMultiByte
269 GetStringTypeW
2D4 HeapSize
32D LCMapStringW
157 FlushFileBuffers
19A GetConsoleCP
1AC GetConsoleMode
487 SetStdHandle
467 SetFilePointerEx
524 WriteConsoleW
52 CloseHandle
Summary
3000 .data
5000 .rdata
3000 .reloc
1000 .rsrc
6000 .text
正如我所說的,我完全沒了主意這裏...我已經試過所有我能想到的。任何有用的評論將非常感謝!
問候,
傑拉德
當它失敗時,您是否在事件日誌中獲得條目? – 2013-05-11 15:24:11