Thalia
Thalia

Reputation: 14635

C++ Executing external process

I am trying to run an executable from a c++ program. I have looked and found 2 options:
system("C:\filepath\file.exe");
and
ShellExecute(GetDesktopWindow(), "open", "C:\filepath\file.exe", NULL, NULL, SW_SHOWNORMAL);
Everything is beautiful, except it doesn't work.
For the first option, I had to include, apart from windows.h, also cstdlib, otherwise my code didn't build.
When I run the program, I get the error:
"file.exe" is not recognized as an internal or external command
I have set the Common Language Runtime Support (/clr) option for my project (and I also had to set the option Multi-threaded Debug DLL (/MDd) for the Runtime Library, otherwise again, it wouldn't build).
The second option will not build even with both libraries included. I get error:
error C3861: 'ShellExecute': identifier not found

I am using VS2010 on Windows7 - and would like this to work on multiplatform...

Am I asking too much ?
Thank you.

Upvotes: 1

Views: 2770

Answers (3)

Thalia
Thalia

Reputation: 14635

Note that I wrote in my question: I have set the Common Language Runtime Support (/clr) option. I did that because of a previous error suggested it.
As soon as I removed that option, I was able to run the executable. Perhaps unmanaged code must remain unmanaged...

Upvotes: 1

mrsheen
mrsheen

Reputation: 892

When I run the program, I get the error: "file.exe" is not recognized as an internal or external command

If I start up a command line prompt and type in file.exe this is what I get:

Microsoft Windows [Version 6.1.7100]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>file.exe
'file.exe' is not recognized as an internal or external command,
operable program or batch file.

C:\>

Upvotes: 2

spencercw
spencercw

Reputation: 3358

You need to replace your backslashes with double backslashes, otherwise the compiler interprets them as escape sequences:

system("C:\\filepath\\file.exe");

Regarding ShellExecute, you need to include Shellapi.h as well as Windows.h, and you don't need to set the /clr flag. ShellExecute is part of the Windows API, so it won't work on other platforms.

Upvotes: 1

Related Questions