从Visual Studio 2012针对Windows XP

3
大家好,
我最近升级到了Visual Studio 2012,喜欢上了改进后的C++11支持和黑暗主题。然而,我正在尝试编写一个在Windows XP及更高版本上运行的程序,但是遇到了一些奇怪的问题。
第一次在XP虚拟机上尝试运行我的程序时,出现了“这不是有效的Win32程序”的错误消息。通过一些搜索,我发现需要应用Update 1才能将目标定位到Windows XP。我搜索了一下,但找到的是Update 2。我应用了它,并将平台工具集设置为v110_xp,然后重新编译了我的程序并尝试再次运行它。这次没有出现任何错误消息,但是在尝试启动程序时,我听到了XP的错误声音(当使用MB_ICONERROR调用MessageBox时听到的声音),然后什么也没有发生。在Windows XP的事件查看器中也没有提到任何信息。
我认为可能是Update 2弄乱了其他东西,所以我完全卸载了VS2012,包括所有留下来的MS SQL垃圾,重新安装它,并只应用了Update 1。再次使用XP工具集编译我的代码,但是仍然出现同样的问题。在尝试启动我的程序时,会发出错误声音,但没有消息。
进一步搜索发现,我需要将PSAPI_VERSION定义为1以定位到进程API的Windows 7之前的版本,所以我这样做了,但问题仍然存在。
我开始认为我的代码有问题,所以我编写了最基本的“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

就像我所说的那样,我完全没有任何想法……我已经尝试了我能想到的一切。非常感谢任何有帮助的评论!

祝好,

杰拉德


当它失败时,您是否在事件日志中获得了条目? - Roger Rowland
1个回答

3

在包含任何Windows头文件之前,请确保您有以下#define

#ifndef WINVER
#define WINVER 0x0501
#endif

#ifndef _WIN32_WINNT   // Specifies that the minimum required platform is Windows XP.
#define _WIN32_WINNT 0x0501
#endif

如果您想查看其他替代方案,可以在MSDN这里找到所有相关信息。


感谢您的回复Roger,但是从我问题中的代码可以看出,我已经定义了WINVER、_WIN32_WINNT等内容。 - Jehjoa
@Jehjoa - 哎呦!抱歉,本应该仔细看一下的,脑子短路了! - Roger Rowland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接