windows 在 C++ 中执行另一个程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2244439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute another program in C++
提问by Russo
I want to remotely execute another application from my C++ program. So far I played along with the CreateProcess(...) function and it works just fine.
我想从我的 C++ 程序远程执行另一个应用程序。到目前为止,我一直在使用 CreateProcess(...) 函数,它工作得很好。
The problem however is that I need the full path of the other program but I do not know the directory of it. So what I want is that I just have to enter the name of the other program, like when you type "cmd" or "winword" into Run... it opens the corresponding programs.
然而,问题是我需要另一个程序的完整路径,但我不知道它的目录。所以我想要的是我只需要输入另一个程序的名称,就像在运行中键入“cmd”或“winword”一样……它会打开相应的程序。
Thanks in advance, Russo
提前致谢,罗素
回答by
If you are using CreateProcess like this:
如果您像这样使用 CreateProcess:
CreateProcessA( "winword.exe", .... );
then the PATH variable will not be used. You need to use the second parameter:
那么 PATH 变量将不会被使用。您需要使用第二个参数:
CreateProcessA( NULL, "winword.exe", .... );
See http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspxfor details.
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx。
回答by MSalters
You're looking for ShellExecute()
. That will even work if you pass it a proper URL, just like the Run menu.
您正在寻找ShellExecute()
. 如果您向它传递正确的 URL,这甚至会起作用,就像运行菜单一样。
回答by IVlad
The directories of the programs you can run from start -> run are added to the PATH variable. You can add the folder your program is to the PATH and then use CreateProcess(). However, you say you don't know the directory, so you probably can't do this.
您可以从 start -> run 运行的程序目录被添加到 PATH 变量中。您可以将程序所在的文件夹添加到 PATH,然后使用 CreateProcess()。但是,您说您不知道该目录,因此您可能无法执行此操作。
Do you know a partial path? For example, do you know that your exe will always be in C:\something\something\ or a subfolder of this path? If so, look up FindFirst() and FindNext() to list all the files in that directory and search for your exe, then use CreateProcess() when you find your exe.
你知道部分路径吗?例如,您是否知道您的 exe 将始终位于 C:\something\something\ 或此路径的子文件夹中?如果是这样,请查找 FindFirst() 和 FindNext() 以列出该目录中的所有文件并搜索您的 exe,然后在找到您的 exe 时使用 CreateProcess()。
http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspxshows how to list files in a directory. You will have to modify it to also search subdirectories (for example, make a recursive function).
http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx显示了如何列出目录中的文件。您将不得不修改它以搜索子目录(例如,创建递归函数)。
回答by Greg
Launching programs and counting on PATH in any way is considered insecure coding. System PATHs may get polluted with locations that aren't secured properly such as a network drive. The best way to launch an application is to launch the executable from exactly where it stands and set the CWD to the location of the executable as installed. Otherwise you could be launching malicious code.
以任何方式启动程序和依靠 PATH 都被认为是不安全的编码。系统路径可能会被未正确保护的位置(例如网络驱动器)污染。启动应用程序的最佳方法是从它所在的位置启动可执行文件,并将 CWD 设置为安装时可执行文件的位置。否则,您可能会启动恶意代码。
Most likely some combination of information from here will help get the location correctly: Detecting installed programs via registry
很可能来自此处的一些信息组合将有助于正确获取位置: 通过注册表检测已安装的程序
Greg
格雷格