views:

384

answers:

3

I'd like to compile a C++ project with just a single call to WinExec in order to launch another executable with some command line parameters. I've no idea what settings to specify in my project in order to get produce an executable that works without requiring Microsoft side-by-side DLLs, which I don't want to have to install on my target system. Any hints?

The symptom is an application which fails to start and the following event getting written to the application log (freely translated from French):

Error, SideBySide, event #33
Microsoft.VC90.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8" cannot be found.

UPDATE: I know that using WinExec is bad practice, but it works like a charm, and Microsoft can't possibly remove it in any future release of the API, can't they?

+7  A: 

If you specify that you want to statically link the run-time (/MT or /MTd) you should be good. Project Properties->C/C++->Code Generation->Runtime Library

Eclipse
+6  A: 

If all you need is CreateProcess/ShellExecute (WinExec is deprecated since NT 3.1/Win 95), you don't need any runtime library at all. In Project Properties / Configuration / Linker / Input enable Ignore All Default Libraries and add kernel32.lib to Additional Dependencies.

jachymko
+1  A: 

The solution has been answered (partially) by both jachymko and Josh. Here is the full solution:

  1. Set Project Properties / Configuration / Linker / Input / Ignore All Default Libraries to Yes and add kernel32.lib to Additional Dependencies. This alone won't link, as the code automatically refers to __security_check_cookie and _WinMainCRTStartup.

  2. Remove /GS switch to instruct the compiler not to inject the security check code. For this, set Project Properties / Configuration / C/C++ / Code Generation / Buffer Security Check to No.

  3. Set Project Properties / Configuration / C/C++ / Code Generation / Runtime Library to Multi-threaded (/MT).

  4. The initial Visual Studio 2008 generated code contains an entry point named _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int). Modify it by renaming it WinMain and convert the third argument to LPSTR.

  5. Set Project Properties / Configuration / Linker / Advanced / Entry Point to WinMain.

With these changes to a default C++ project, the code finally compiles and links, and runs on a freshly installed Vista or XP, which lacks the runtime library.

Pierre