Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Program Written in VS2012 Works w/ Win7/8/2008R2/2012, but not 2003/XP/32bit?

Tags:

c

visual-c++

I have to start by saying that I am very much a programming noob. I do not understand all the compiler options or nuances of the IDE, not by a longshot. But I am trying to teach myself more about native programming languages. (I'm decent with C#, but that is much easier than C as I am discovering.)

Today, I wrote this small program in C. It is a console/command line program. I used Visual Studio 2012 and my development machine alternates between Windows 7 and 8, 64 bit. To start, what I did was create a new VC++ project, and I chose a Blank Project. Then I created a new app.c file. I also created a *.rc file to give the executable some extra properties like "File Version" and "Company Name" when you browse the file properties in Windows Explorer. Then I went to the properties of the project, chose Configuration Properties -> C/C++ -> Code Generation and I changed Runtime Library to "Multi-threaded (/MT) so that I wouldn't have to distribute the msvcr100.dll file along with my executable.

In the app.c file, I placed the following code:

#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <WtsApi32.h>
#pragma comment(lib, "WtsApi32.lib")

void main(int argc, char *argv[])
{
    char *helpMsg = "blah";
    char *hostName, *connState = "";
    char *addrFamily = "";
    HANDLE hHost = NULL;

    ...stuff and so forth and so on...
}

Then I built/compiled the program, and the executable works just fine on Windows 7, 8, Server 2008R2, Server 2012, all 64 bit. But when I try to run the program on Server 2003 (and I am guessing WinXP, etc., as well,) I am greeted with the Windows dialog box:

"Foo.exe is not a valid Win32 application."

So my question is, is there something obvious/simple that I am missing that will allow this executable to also work on earlier XP/2003/32bit platforms that I am missing? I do not believe that I am using any 64-bit exclusive features in my program. But I figured that since I did choose "Blank Project" instead of "Win32 Console Application" that I may be missing some setting.

Edit: Here is the dumpbin.exe /headers output when run against my exe:

Microsoft (R) COFF/PE Dumper Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\users\me\Release\foo.exe

PE signature found

File Type: EXECUTABLE IMAGE

FILE HEADER VALUES
         14C machine (x86)
           5 number of sections
    50F604BC time date stamp Tue Jan 15 19:39:08 2013
           0 file pointer to symbol table
           0 number of symbols
          E0 size of optional header
         102 characteristics
               Executable
               32 bit word machine

OPTIONAL HEADER VALUES
         10B magic # (PE32)
       11.00 linker version
        7800 size of code
        A200 size of initialized data
           0 size of uninitialized data
        16A7 entry point (004016A7) _mainCRTStartup
        1000 base of code
        9000 base of data
      400000 image base (00400000 to 00414FFF)
        1000 section alignment
         200 file alignment
        6.00 operating system version
        0.00 image version
        6.00 subsystem version
           0 Win32 version
       15000 size of image
         400 size of headers
           0 checksum
           3 subsystem (Windows CUI)
        8140 DLL characteristics
               Dynamic base
               NX compatible
               Terminal Server Aware
      100000 size of stack reserve
        1000 size of stack commit
      100000 size of heap reserve
        1000 size of heap commit
           0 loader flags
          10 number of directories
           0 [       0] RVA [size] of Export Directory
        D374 [      3C] RVA [size] of Import Directory
       11000 [     538] RVA [size] of Resource Directory
           0 [       0] RVA [size] of Exception Directory
           0 [       0] RVA [size] of Certificates Directory
       12000 [     C04] RVA [size] of Base Relocation Directory
        9160 [      38] RVA [size] of Debug Directory
           0 [       0] RVA [size] of Architecture Directory
           0 [       0] RVA [size] of Global Pointer Directory
           0 [       0] RVA [size] of Thread Storage Directory
        CF98 [      40] RVA [size] of Load Configuration Directory
           0 [       0] RVA [size] of Bound Import Directory
        9000 [     118] RVA [size] of Import Address Table Directory
           0 [       0] RVA [size] of Delay Import Directory
           0 [       0] RVA [size] of COM Descriptor Directory
           0 [       0] RVA [size] of Reserved Directory


SECTION HEADER #1
.text name
7670 virtual size
1000 virtual address (00401000 to 0040866F)
7800 size of raw data
 400 file pointer to raw data (00000400 to 00007BFF)
   0 file pointer to relocation table
   0 file pointer to line numbers
   0 number of relocations
   0 number of line numbers
60000020 flags
     Code
     Execute Read

SECTION HEADER #2
.rdata name
49E2 virtual size
9000 virtual address (00409000 to 0040D9E1)
4A00 size of raw data
7C00 file pointer to raw data (00007C00 to 0000C5FF)
   0 file pointer to relocation table
   0 file pointer to line numbers
   0 number of relocations
   0 number of line numbers
40000040 flags
     Initialized Data
     Read Only

Debug Directories

    Time Type       Size      RVA  Pointer
-------- ------ -------- -------- --------
50F604BC cv           61 0000CFE0     BBE0    Format: RSDS, {582D0FF2-59C1-4633-AF2A-E4A4AD6BFA2C}, 1, C:\Users\me\Release\users.pdb
50F604BC feat         10 0000D044     BC44    Counts: Pre-VC++ 11.00=0, C/C++=116, /GS=116, /sdl=0

SECTION HEADER #3
.data name
2C04 virtual size
E000 virtual address (0040E000 to 00410C03)
 E00 size of raw data
C600 file pointer to raw data (0000C600 to 0000D3FF)
   0 file pointer to relocation table
   0 file pointer to line numbers
   0 number of relocations
   0 number of line numbers
C0000040 flags
     Initialized Data
     Read Write

SECTION HEADER #4
.rsrc name
 538 virtual size
11000 virtual address (00411000 to 00411537)
 600 size of raw data
D400 file pointer to raw data (0000D400 to 0000D9FF)
   0 file pointer to relocation table
   0 file pointer to line numbers
   0 number of relocations
   0 number of line numbers
 40000040 flags
     Initialized Data
     Read Only

 SECTION HEADER #5
.reloc name
235C virtual size
12000 virtual address (00412000 to 0041435B)
2400 size of raw data
DA00 file pointer to raw data (0000DA00 to 0000FDFF)
   0 file pointer to relocation table
   0 file pointer to line numbers
   0 number of relocations
   0 number of line numbers
 42000040 flags
     Initialized Data
     Discardable
     Read Only

Summary

    3000 .data
    5000 .rdata
    3000 .reloc
    1000 .rsrc
    8000 .text

I have also tried going to Project Properties -> Linker -> System: Minimum Required Version and changing that to 5.00 and 1.00 or whatever, but it has no effect. dumpbin.exe still reports the OS version as 6.00. I have even used editbin.exe /version 5.00 on the exe and no errors were reported... and yet dumpbin.exe still reports 6.00 for the OS version.

like image 944
Ryan Ries Avatar asked Jan 16 '13 01:01

Ryan Ries


3 Answers

VS2012 originally shipped without supporting XP/2003. The updated CRT and runtime support libraries are using too many Windows api functions that are not available on those operating systems. This created quite a stir among its customers, to put it mildly, and they re-engineered the libraries to dynamically bind to these functions and limp along it they are missing. This was made available in Update 1, you'll need to use Project + Properties, General, Platform Toolset = v110_xp to build programs that use those libraries.

Note how it changes a linker setting, the important one, Linker > System > Minimum Required Version = "5.01". Which ensures that the executable file is marked to be compatible with the XP sub-system version. You'll also build against SDK version 7.1, the last one that is still compatible with XP.

When you use the default toolset (v110) then you target sub-system 6.00 and SDK version 8. Version 6.00 was the last major kernel revision, started with Vista.

A brief overview of the new api functions being used to give you a (very rough) idea what is missing in the XP version:

  • FlsAlloc, FlsFree, FlsGetValue, FlsSetValue : safe thread-local storage
  • InitializeCriticalSectionEx, CreateSemaphoreEx : safety
  • SetThreadStackGuarantee : stability
  • CreateThreadPoolTimer, SetThreadPoolTimer, WaitForThreadPoolTimerCallbacks, CloseThreadPoolTimer : cheaper timers
  • CreateThreadPoolWait, SetThreadPoolWait, CloseThreadPoolWait : cheaper waits?
  • FlushProcessWriteBuffers, GetCurrentProcessorNumber, GetLogicalProcessorInformation : threading
  • FreeLibraryWhenCallbackReturns : stability?
  • CreateSymbolicLink : functionality
  • InitOnceExecuteOnce : unknown
  • SetDefaultDllDirectories : unknown
  • EnumLocalesEx, CompareStringEx, GetDateFormatEx, GetLocalInfoEx, GetTimeFormatEx, GetUserDefaultLocaleName, IsValidLocaleName, LCMapStringEx : better locale support
like image 113
Hans Passant Avatar answered Nov 14 '22 16:11

Hans Passant


I figured it out myself. (But thank you Hans for steering me in the right direction.) For some reason, even with Update 1 and even after setting my toolset to v110_xp, and setting the minimum required version to 5.01 in the Linker options, the resulting dumpbin app.exe /headers still reports a minimum operating system version of 6.0.

So I simply ran

editbin.exe app.exe /SUBSYSTEM:CONSOLE,5.01 /OSVERSION:5.1

And the executable now runs just fine on older operating systems. I'm thinking there still might be a little bit of a bug somewhere in Visual Studio.

like image 4
Ryan Ries Avatar answered Nov 14 '22 18:11

Ryan Ries


The MSVC Team Blog says that when using MSBuild or DEVENV from the command-line with the v110_xp platform toolset, no other changes are necessary. This information is incorrect/incomplete. The /SUBSYSTEM linker argument and associated "Minimum Required Version" must also be set appropriately.

The MSDN documentation for /ENTRY states that, if the /SUBSYSTEM argument is not specified that the SUBSYSTEM and ENTRY POINT are determined automatically. My hunch is that when this happens, the SUBSYSTEM's "Minimum Required Version" argument is also automatically overridden.

The v110_xp toolset automatically specifies the SUBSYSTEM's MRV ("5.1" (WindowsXP)) but not the SUBSYSTEM. As such, the MRV will be overridden, for example, by the linker to "6.0". Running the application will then cause WindowsXP to show the error message stating that the application "is not a valid Win32 application."

like image 2
Jeff Marr Avatar answered Nov 14 '22 16:11

Jeff Marr