简体   繁体   中英

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.

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:\>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM