简体   繁体   English

C ++执行外部进程

[英]C++ Executing external process

I am trying to run an executable from a c++ program. 我试图从c ++程序运行可执行文件。 I have looked and found 2 options: 我看了,找到了2个选项:
system("C:\\filepath\\file.exe"); 系统( “C:\\文件路径\\的file.exe”);
and
ShellExecute(GetDesktopWindow(), "open", "C:\\filepath\\file.exe", NULL, NULL, SW_SHOWNORMAL); 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. 对于第一个选项,除了windows.h之外,我还必须包括cstdlib,否则我的代码不会构建。
When I run the program, I get the error: 当我运行程序时,我收到错误:
"file.exe" is not recognized as an internal or external command “file.exe”未被识别为内部或外部命令
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). 我为我的项目设置了公共语言运行时支持(/ clr)选项(我还必须为运行时库设置多线程调试DLL(/ MDd)选项,否则再次,它不会构建)。
The second option will not build even with both libraries included. 即使包含两个库,第二个选项也不会构建。 I get error: 我收到错误:
error C3861: 'ShellExecute': identifier not found 错误C3861:'ShellExecute':找不到标识符

I am using VS2010 on Windows7 - and would like this to work on multiplatform... 我在Windows7上使用VS2010 - 并希望这可以在多平台上工作...

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 当我运行该程序时,我收到错误:“file.exe”未被识别为内部或外部命令

If I start up a command line prompt and type in file.exe this is what I get: 如果我启动命令行提示符并输入file.exe,这就是我得到的:

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 ,您需要包含Shellapi.h以及Windows.h,并且不需要设置/clr标志。 ShellExecute is part of the Windows API, so it won't work on other platforms. ShellExecute是Windows API的一部分,因此无法在其他平台上运行。

Note that I wrote in my question: I have set the Common Language Runtime Support (/clr) option. 请注意,我在我的问题中写道:我已经设置了公共语言运行时支持(/ clr)选项。 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... 也许非托管代码必须保持不受管理...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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