How to get Visual C++ 2012 Update 2 statically linked applications to run on Windows XP

EDIT (June 26, 2013) – Microsoft has released the final version of Visual Studio 2012 Update 3 that completely fixes this issue.  For more information on Update 3, please click here.

EDIT (May 11, 2013) – Microsoft has released an RC of Visual Studio 2012 Update 3 that fixes this issue which is “go live”, meaning if you can’t wait for the final Update 3 and need a Microsoft supported solution immediately, you can use it with production releases.  For more info look here and to download look here. For a solution to use with Update 2, see below.

Due to a regression in Visual C++ 2012 Update 2, applications using ATL will no longer be able to run on XP, even if using built-in XP targeting support.  This is due to a new dependency on InitializeCriticalSectionEx.  Running an app on XP that was built with Visual C++ 2012 Update 2 will give you the following error message:

Entry Point Not Found

The procedure entry point InitializeCriticalSectionEx could not be located in the dynamic link library KERNEL32.dll

To fix this, I’ve published a new version of the xpsupport tool, namely XPSupport for VC11 Update 2 Version 1.02, on my skydrive.  It simply wraps the InitializeCriticalSectionEx function with an XP compatible replacement, using techniques I’ve used in previous blog entries.

To use this solution, simply add build customizations (masm) to your project by right clicking on the project in solution explorer and choosing “build customizations” and check masm (targets, props) on, then add the three files (xpatl.cpp, xpatlwrap.asm, and xpatlwrap64.asm) contained in the zip file named: xpsupportvc11upd2v102.zip found here:

https://skydrive.live.com/redir?resid=1B361BF333E9AB82!153&authkey=!AEAOQJ4-LCqWQiw

Build your app, and now your app will run under XP.

EDIT: despite the title of this blog post, I received reports that this works also for dynamically linked apps (i.e. linking to MFC and the CRT dynamically)

More info about the cause found here:

http://social.msdn.microsoft.com/Forums/en-US/visualstudiogeneral/thread/f0477c9f-8a2c-4e6b-9f5a-cd2469e216c4

credits to VSBs and others for reporting and researching this issue.

changelog:

1.00 2013-04-14 Initial Release

1.01 2013-04-15 added x64 support

1.02 2013-04-17 cleaned up Vista check

Visual Studio 2012 Update 1 released: real, official XP targeting support

I’m happy to announce that the Visual C++ 2012 XP targeting solution is finally available as part of Visual Studio 2012 Update 1.  As you know from previous posts, I tried to fill in the gap by providing wrapper functions (XPSupport wrappers) until the official Microsoft solution was ready.    No need to use those any more, you can definitely remove those files from your project if you’ve been using them up to now.

Thanks to those at Microsoft, especially Steve Teixeira and Herb Sutter for believing that this was a worthwhile thing to provide users.  It definitely will increase adoption of Visual C++ 2012, and C++11 in general, because it’s one less thing to have to worry about when making the decision to adopt the new toolset provided by Visual C++ 2012.    Thanks also, to all those people that provided feedback through blog comments, connect bug comments, uservoice comments and various other ways that they expressed their opinion that this was an absolute adoption blocker for them.

One of the things you may notice is that you have to choose a new “Platform Toolset” if you want XP support, i.e. it’s not just a simple recompile, you actually have to change project settings.  The reason this was done was that the Developer division cannot dictate the OS support level of the Platform SDK.  Since the Platform SDK 8.0 dropped support for XP, developer division decided to ship the 7.1 SDK and redirect the include paths to that instead of using the existing 8.0 SDK.   You only get the 7.1 SDK include paths if you choose the appropriate XP targeting platform toolset, that is, vc110_xp.

Just to be clear, this doesn’t mean that by choosing the 7.1 SDK you’re all of a sudden using inferior (older) versions of the C++ compiler and runtime libraries.  On the contrary, you are now using a newer version of the CRT, MFC etc, that have been enhanced to remove any Vista+ specific API calls (i.e API calls not available on XP), along with the exact same C++ compilers and linkers that the normal 8.0 SDK toolset provides.

And another clarification: with Update 1 there is only one set of CRT/MFC DLLs and libs.  There’s no separate redistributable for XP vs other operating systems.  The single vcredist_x86.exe/vcredist_x64.exe files have been updated to work on ALL platforms.  This simplifies things from a distribution perspective.  No worry about having the incorrect redist installed on the incorrect OS.

Note: currently there is nothing stopping you from using the 8.0 SDK with your project (i.e leave your platform toolset setting alone) and still run the resultant binaries on XP.  The difference is, that it’s not officially supported and could break at any time (e.g. if Windows team delivers an update to 8.0 SDK that causes incompatibilities with XP – this hasn’t happened yet but be forewarned).  If you decide to take this approach, be sure to update the subsystem version as described in my earlier blog post.  In summary, in the IDE you can just go to project properites, then linker settings – system, and set minimum required version to 5.01.

In conclusion, happy XP targeting with VC2012 + Update 1!

Visual Studio 2012 Express for Windows Desktop released

Visual Studio 2012 Express for Windows Desktop has been released.  This means that Win32 apps can now be developed using 2012 without buying a Proefessional license.  Previously, only WinRT apps were supported and Express 2012 was only supported on Windows 8.  This version can be installed on Windows 7 as well.  Of course, testing the XPSupport 1.10 wrappers was the first thing I tried.  And I’m happy to report that they work fine.

VC2012 RTM released – linker minimum required version setting now supports XP

VC2012 RTM has been released, and my xpsupport version 1.10 (post Release Preview version) files available at

https://skydrive.live.com/?cid=1b361bf333e9ab82

are holding up well.  The main changes between Release Preview and RTM are that new calls to Vista only thread pooling functions have been added to the CRT (so I had to wrap them in 1.10), and the way that the ConCRT libraries are initialized had to be changed slightly to carefully set the internal cached OS version back to XP after hacking it temporarily to Vista.   So if you’ve already upgraded to 1.10, no changes are necessary if you just upgraded to RTM, otherwise upgrade your xpsupport version now!

And now some good news: in previous VC2012 betas/candidates, the minimum version you could set in your binary was 6.0 (Vista) which meant you had to use EDITBIN to change the version of your executable to hack it back to 5.1.

Now, The RTM linker supports setting SUBSYSTEM version to 5.1!  In fact you no longer need to fumble around with command line options. In the IDE you can just go to project properites, then linker settings – system, and set minimum required version to 5.01.  And once you do that it conveniently also sets OSVERSION for you to 5.1.

This was probably put in early to prepare for the eventual full support for XP targeting later this fall.  Of course, with this setting (5.1) specified in the linker you still cannot run an app created by VC2012 RTM in XP due to missing ordinals, but it saves a few steps when performing my VC2012 XP targeting instructions at

http://tedwvc.wordpress.com/2012/03/11/how-to-get-visual-c-2012-vc-11-beta-statically-linked-crt-and-mfc-applications-to-run-on-windows-xp/

Now the countdown begins for the eventual release of full XP targeting support by Microsoft, and these wrappers can then be finally put to rest.

How to get Visual C++ 2012 (VC 11 Beta) statically linked CRT and MFC applications to run on Windows XP

EDIT (April 14, 2013): If you’re having trouble with Microsoft’s XP support after installing Visual Studio 2012 Update 2 then please ignore this blog post! Read the new blog post instead.

EDIT (November 26, 2012): Microsoft has made their final release of Visual Studio 2012 Update 1 available which includes official XP targeting support!

EDIT (October 14, 2012): Microsoft has released their CTP of the XP targeting support, negating any further need for these wrappers.  Please use the official Microsoft solution from now on.

EDIT (June 20, 2012): Microsoft has announced XP targeting support will be added to Visual C++ 2012 in a post-RTM update.  Read the details here.  In the meantime you can use the below as a temporary workaround until the update has been released.

Source files and sample projects for this article located at: https://skydrive.live.com/?cid=1b361bf333e9ab82

Files last updated: 2012-07-10

XPSupport wrappers version 1.10

One of the major limitations of Visual C++ 2012 is that it does not create applications that can run on Windows XP.  As most of you already know, there is a connect item at Microsoft on this.  See: http://connect.microsoft.com/VisualStudio/feedback/details/690617

Note that the connect item was closed as “by design” and it was stated that only Vista SP2 and above is supported.  So this leaves out XP and Server 2003.

As you can see from the comments, many users simply will not adopt VC 11 because of its lack of XP targeting support.  Whether the Visual Studio 11 tools run on XP is a whole other story.  I don’t need the IDE to run on XP. What I need is the ability to use VC 11 (and all of its powerful new language features) AND still be able to target XP as a platform to deploy my apps to.

If you’re a long time reader of my blog, you know that I have written another article in the past on how to target Windows 2000 when using Visual Studio 2010.   In that article I explained that there is a simple trick that we can use to “wrap” missing API functions.  This involves making a small assembly language forwarding directive that calls our own C++ wrapper function.  If we use this trick we can substitute our own versions of any API symbol that is linked into our app, without causing any multiple symbol errors in the linker.  This means we do not have to use /FORCE:MULTIPLE.

To summarize, if we run dependency walker (available from www.dependencywalker.com) on a built executable that uses CRT and/or MFC 11 on Windows XP SP3, we’ll see a bunch of “red” icons beside the functions that are missing on XP (mostly in kernel32.dll). Our goal is to get rid of all of this red before it will run correctly on XP.

An aside: If it were truly a technical limitation to drop XP, then it would be impossible for us to “wrap” every instance of these functions without touching the original CRT and/or MFC static lib source code.  I’m about to show you a simple, elegant way to give your VC 11 apps the gift of XP support, while allowing the exact same binary to run on higher versions as well (e.g Vista, 7, Server 2008, etc)

There is another new roadblock that Microsoft has put in our way.  Previous to VC 11, we could override both the minimum operating system and subsystem version numbers using a linker command line option.  However in VC 11, they only allow specification of 6.0 (Vista) as a minimum.  That means there is NO way to write the necessary operating and subsystem versions to the built binary without a separate post-build tool.

Turns out that writing the operating system and subsystem version numbers can be acheived using EDITBIN.EXE (a tool that is included with VC11)  This means we can use this as post-build step just after linking occurs.

All that needs to be done is to run editbin.exe on the built executable itself, e.g.

EDITBIN.EXE testapp.exe /SUBSYSTEM:WINDOWS,5.01 /OSVERSION:5.1

It still throws a warning that the subsystem supplied is invalid but writes it anyway (unlike linking which refuses to write it when it’s invalid).

Note: if you’re trying these instructions with a console application, use

EDITBIN.EXE testapp.exe /SUBSYSTEM:CONSOLE,5.01 /OSVERSION:5.1

instead.

Notice that in the title of this blog post, I say “statically linked”.  There is good reason for that.  Microsoft no longer provides makefiles for their DLLs (since VC2010).  The last version to provide makefiles was VC2008.  That is unfortunate, as I could have provided a way to make dynamically linked apps run on XP as well.  But for now, we’ll have to do with a solution for statically linked apps only. I suggest you put pressure on Microsoft using the avenue of your choice to have them make available steps or makefiles that will allow us to rebuild the CRT and MFC DLLs.  If they agree, then I can provide a follow-up to this blog entry.

So, without further ado, here they are, two walkthroughs on how to create either a CRT or MFC statically linked app in Visual Studio 2012 that runs on Windows XP (resulting executable tested on a Windows XP SP3 machine that has been updated using Microsoft (Windows) Update with the various security fixes. This has not been tested on Windows XP x64 edition, and I don’t expect to be adding support for it as it’s a relatively underused operating system.

Simplified 4 step summary of solution:

(1) Add assembly language support to your existing project

(2) Add 4 files that I wrote that “wrap” the missing CRT and MFC functions (downloadable from my skydrive).  You only need 2 of these files if your app is not using MFC.

(3) Add a post-build event that runs EDITBIN.EXE to set the operating system and subsystem versions of your built app.

(4) Build your app, copy to XP and run it

That’s it. No complicated source code changes.  You simply need to add some wrapper code to your app and then use a tool to change a binary after it’s built.

In all cases, the wrapper code defers to the OS versions of the APIs (using GetProcAddress) when running on operating systems higher than XP/2003, and on XP/2003 uses either specific wrapper code (when needed) or appropriate return codes that allow the calling functions to continue.

Here’s a walkthrough that describes each of the above steps in detail (I’ve provided both CRT and MFC versions of the walkthrough, choose one or the other depending on whether your app uses MFC or not).

Grab the 3 zip files here first: https://skydrive.live.com/?cid=1b361bf333e9ab82

The first two zip files testapp.zip and testmfcapp.zip are complete examples of CRT and MFC apps respectively.  Both of them contain the custom build step with editbin.exe that I mentioned and the changes mentioned in the walkthrough already applied.   The third, xpsupport.zip contain the 4 files required (only need 2 files if CRT project) to add to an existing project that you may have (or use them with newly created projects when going through the walkthroughs below)

IMPORTANT NOTE: if you use these wrappers with your existing projects, you must change both your WINVER and _WIN32_WINNT to 0x0600 in your stdafx.h files.  Otherwise a lot of Vista and later specific structures and defines that I use will not compile correctly.  I may fix this in a future release.

CRT (Win32 or console app) version of walkthrough:

1) Launch Visual Studio 11 Beta (could be named Visual Studio 2012 if this article being read after the RTM was released)

2) Create a Win32 app (Win32 Project), or console app, call it testapp, keep all the defaults and click finish (note: if you decide to create a console app in this step, please add an include for “windows.h” to your generated project’s stdafx.h file)

3) Right click on the project in solution explorer (not the solution, but the project – usually the second line from the top in the solution explorer view) and choose “Build Customizations…” menu item.

4) Click the checkbox (on) beside “MASM (.targets,.props), and hit OK.

5) Right click on the project and choose “Properties” menu item to bring up project properties

6) choose “release” configuration

7) change this project to statically link to the CRT (change to Release version of project, then using project properties – configuration properties – C/C++ – code generation: change runtime library to multithreaded (instead of multithreaded DLL),

8) choose “debug” then do the same in Debug as in step 7 (except change to multi-threaded Debug)

9) Choose “all configurations”

10) Choose Configuration Properties – Build Events – Post Build Event

11) If your app is a Win32 app add the following two steps to the “Command Line” section of the Post Build Event:

ping 127.0.0.1 -n 3 -w 1000

editbin.exe "$(TargetPath)" /SUBSYSTEM:WINDOWS,5.01 /OSVERSION:5.1

otherwise, if your app is a console app, then add the following two steps instead  to the “Command Line” section of the Post Build Event:

ping 127.0.0.1 -n 3 -w 1000

editbin.exe "$(TargetPath)" /SUBSYSTEM:CONSOLE,5.01 /OSVERSION:5.1

12) Hit OK

13) Get the xpsupport.zip file from my skydrive and extract 2 of the 4 files (xpcrt.cpp, xpcrtwrap.asm ) to the folder where your source code is ( stdafx.cpp, etc), e.g. at

c:\users\<user>\Documents\Visual Studio 11\Projects\testapp\testapp

14) Right click on Source Files folder, and choose Add – Existing Item

15) add the 2 files that you copied in step 13: xpcrt.cpp and  xpcrtwrap.asm to your project

16) Build your application.  The 2 files you added in step 15 should have compiled correctly, and the version should have been set by editbin.exe.

17) head over to a Windows XP SP3 machine (or VM) and copy your debug/release folder over there, and try running the app. If all steps above were performed correctly, you now have your first Win32 CRT 2012 app running on Windows XP SP3.

MFC version of walkthrough:

1) Launch Visual Studio 11 Beta (or Visual Studio 2012 if this article being read after the RTM was released)

2) Create an MFC app, call it testmfcapp, keep all the defaults except choose to use MFC in a static library and hit Finish

3) Right click on the project in solution explorer (not the solution, but the project – usually the second line from the top in the solution explorer view) and choose “Build Customizations…” menu item.

4) Click the checkbox (on) beside “MASM (.targets,.props), and hit OK.

5) Right click on the project and choose “Properties” menu item to bring up project properties

6) Choose “all configurations”

7) Choose Configuration Properties – Build Events – Post Build Event

8) Add the following two steps to the “Command Line” section of the Post Build Event:

ping 127.0.0.1 -n 3 -w 1000

editbin.exe "$(TargetPath)" /SUBSYSTEM:WINDOWS,5.01 /OSVERSION:5.1

9) Hit OK

10) Get the xpsupport.zip file from my skydrive and extract the 4 files (xpcrt.cpp, xpcrtwrap.asm, xpmfc.cpp, and xpmfcwrap.asm ) to the folder where your source code is ( stdafx.cpp, etc), e.g. at

c:\users\<user>\Documents\Visual Studio 11\Projects\testmfcapp\testmfcapp

11) Right click on Source Files folder, and choose Add – Existing Item

12) add all 4 files that you copied in step 10: xpcrt.cpp, xpcrtwrap.asm, xpmfc.cpp, and xpmfcwrap.asm to your project

13) Build your application.  The 4 files you added in step 12 should have compiled correctly, and the version should have been set by editbin.exe.

14) head over to a Windows XP SP3 machine (or VM) and copy your debug/release folder over there, and try running the app. If all steps above were performed correctly, you now have your first MFC 2012 app running on Windows XP SP3.

Technical details of solution:

I’ve added comments to the code that describe the various wrapper functions that I had to write.  Several in particular I’d like to bring to your attention:

(1) Wrapper for GetTickCount64 uses some undocumented but well established Windows structures – please read over the definition of that function in the source code (xpcrt.cpp) for further comments about this.

(2) Wrapper for Locale name based APIs uses a DLL (nlsdl.dll) that ships with Internet Explorer 7 and above, or is available from Microsoft as a redistributable (see code comments for location and discussion of an alternative that does not require use of nlsdl.dll).  So basically if you can guarantee that IE7 or later or the nlsdl redist is installed on the XP SP3 machine, you can use this solution without any modifications whatsoever.

Troubleshooting:

If you get the error: “Not a valid win32 application”, then the editbin.exe step failed during your build.  Please check to make sure the two steps mentioned (the ping and the editbin.exe are separate lines in the command line – click “edit…” when adding them)

If you get any errors that entry points cannot be found, then you’ve probably forgotten a step above or have called a Vista function that I haven’t wrapped.  It’s also possible that you inadvertently added the asm files to your project before you did the “Build Customizations” step.  Build customizations must be done before ANY assembly files are added, or they won’t be compiled.

Comments/questions welcome.  And keep voting on the Microsoft connect item (link above) :)

XPSupport wrappers – Update History

1.00 2012-03-11 Initial Release

1.01 2012-03-13 added msvcp110 stuff (InitializeCriticalSectionEx, CreateSymbolicLink(A/W)

1.02 2012-03-15 added DwmExtendFrameIntoClientArea

1.03 2012-03-15 added fix for ConCRT runtime resource manager initialization (so std::thread can now be used)

1.04 2012-03-16 added PSGetPropertyDescriptionListFromString

1.05 2012-03-29 added wrapper for EnumSystemLocalesEx

website update: 2012-03-30 updated steps to use editbin.exe to set versions instead of a custom executable (to avoid needing a special EXE to set the version)

1.06 2012-05-05 added wrapper for GetLogicalProcessorInformation (for XP SP2 support – thanks Michael Chourdakis for this implementation)

1.07 2012-05-18 added wrapper for InitOnceExecuteOnce

1.08 2012-05-26 added x64 support – thanks to Antony Vennard (https://vennard.org.uk) for testing and correcting several errors in my initial test x64 release

1.09 2012-05-27 fixed non-Unicode (MBCS) builds and updated instructions to clarify usage with console apps (thanks to Latency and Antony for suggesting and helping with these fixes)

1.10 2012-07-10 added support for more recent builds of Visual Studio 2012 (CRT uses several API functions related to thread pooling) and updated source code to include a standard MIT license

New feature for MFC vNext: small statically linked dialog based and console apps

If you’ve read my blog in the past, you’ve noticed that I’ve written several articles relating to reducing the size of statically linked MFC apps. Ever since the feature pack classes were added to MFC (in 2008 SP1) there have been various regressions that caused even the simplest of MFC applications to be super-large when building a release version.

In vNext, Microsoft has attempted to address this issue by adding a new define: _AFX_NO_MFC_CONTROLS_IN_DIALOGS that basically says: I know that I will not have any of the “feature pack” controls in my app, so don’t link any code into the app that I’m not going to need.

From my testing so far, it only appears to work in console apps and MFC Dialog based apps. If you use it with a single document interface app, it will fail with a linker error.

The sizing results are dramatic, and I suggest you give the define a try if you are using MFC vNext and have a statically linked app.

Apps created with CRT and MFC vNext (11) cannot be used on Windows XP SP3

I filed a bug relating to no longer being able to target XP when creating a Visual C++ vNext app. I got a reply from a member of the Microsoft libraries team stating this is by design and only Vista and above or Windows Server 2008 SP2 and above can be targeted when creating a vNext application.

Please read the following and vote on it, add comments to it as well. If you believe that being able to create a single binary that can target XP and above, and still have access to the latest compilers, libraries, and headers that vNext provides (i.e. not have to use the “native multi-targeting” feature, then please see below:

http://connect.microsoft.com/VisualStudio/feedback/details/690617