1

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.

5
  • If you want this to be multiplatform, you can't use the Windows-specific ShellExecute. Your system command is correct; it sounds like you gave it an incorrect path. Commented Feb 24, 2012 at 23:14
  • Try escaping backslashes in the string argument to system(): system("C:\\filepath\\file.exe");. Chances are your first code snippet will work as expected then. Commented Feb 24, 2012 at 23:15
  • I did escape the \\, and my path is correct Commented Feb 24, 2012 at 23:19
  • 1
    Um, no. Hans Passant escaped your backslashes when he edited your question. Try the code as @FrédéricHamidi and Hans suggested and it will work if the path is correct. Commented Feb 24, 2012 at 23:27
  • When I posted initially - there were just two lines of TEXT. My actual path is not in the filepath directory and my executable is not named file. I had double backslashes in my code from the beginning... I wish solutions were that simple. Commented Feb 25, 2012 at 0:01

3 Answers 3

2

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:\>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

7 Comments

@user1217150 Are you sure your file exists and is where you have said it is?
After including Shellapi.h - the Build just got funny. After listing a few errors from INSIDE Shellapi.h, I even got the following: "fatal error C1003: error count exceeds 100; stopping compilation". I don't think this program likes me.
spencercw: I am actually running another C++ program and I copied its path from the Debug.
@user1217150 Have you included Windows.h first? Most of the Windows headers don't work at all if you don't include Windows.h first.
I have been able to run something just now using the system (ShellExecute will not build).<br> My problem - which didn't go away - is that i am trying to do this as part of "unit testing" (run that particular program with every command line option required and impossible), and if I surround my system command with a BOOST_AUTO_TEST_CASE(stuff) it doesn't work (it did when I removed the #include <boost/test/unit_test.hpp> and replaced the BOOST_AUTO_TEST_CASE with main. Is it totally wrong to want to run test cases this way ?
|
1

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.