我希望我的C++程序在Windows中执行另一个.exe.我该怎么做?我正在使用Visual C++ 2010.
这是我的代码
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int input;
cout << "Enter 1 to execute program." << endl;
cin >> input;
if(input == 1) /*execute program here*/;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*tow 14
这是我之前寻找答案时找到的解决方案.
它声明你应该总是避免使用system(),因为:
而是可以使用CreateProcess().
Createprocess()用于启动.exe并为其创建新进程.应用程序将独立于调用应用程序运行.
#include <Windows.h>
void startup(LPCSTR lpApplicationName)
{
// additional information
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// start the program up
CreateProcessA
(
lpApplicationName, // the path
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE, // Opens file in a separate console
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*son 10
使用CreateProcess()函数.
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx
Ste*_*ieG 10
你可以使用这个system功能
int result = system("C:\\Program Files\\Program.exe");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51520 次 |
| 最近记录: |