2013-05-11 61 views
3

大家好日子,针对从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 

正如我所说的,我完全没了主意这里...我已经试过所有我能想到的。任何有用的评论将非常感谢!

问候,

杰拉德

+0

当它失败时,您是否在事件日志中获得条目? – 2013-05-11 15:24:11

回答

3

之前#include任何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 

这一切都explained on MSDN here,如果你想看到其他选择。

+0

感谢您的回复Roger,但正如您可以从我的问题中的代码中看到的那样,我已经定义了WINVER,_WIN32_WINNT等等。 – Jehjoa 2013-05-11 15:18:28

+0

@Jehjoa - doh!对不起,应该看得更仔细 - 大脑褪色! – 2013-05-11 15:19:58

相关问题