我想要做的是从另一个.exe打开一个.exe.我真的不知道该怎么做,所以我搜索了互联网.我从互联网上尝试了一些建议的方法,但它没有用.
这是我的代码:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
system ("OpenFile.exe");
system ("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在DEV C++中运行它时,它没有编译,我收到一个错误.有人可以帮帮我吗?
Jon*_*ona 69
你应该总是避免使用system()
因为
您应该使用CreateProcess().
您可以使用Createprocess()来启动.exe并为其创建新进程.应用程序将独立于调用应用程序运行.
这是我在我的一个项目中使用的一个例子:
#include <windows.h>
VOID startup(LPCTSTR lpApplicationName)
{
// additional information
STARTUPINFO 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
CreateProcess( lpApplicationName, // the path
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Run Code Online (Sandbox Code Playgroud)
编辑:您得到的错误是因为您需要指定.exe文件的路径而不仅仅是名称.Openfile.exe可能不存在.
小智 17
我在这方面取得了很大的成功:
#include <iostream>
#include <windows.h>
int main() {
ShellExecute(NULL, "open", "path\\to\\file.exe", NULL, NULL, SW_SHOWDEFAULT);
}
Run Code Online (Sandbox Code Playgroud)
如果您有兴趣,请填写完整的文档:
http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx.
小智 5
尝试这个:
#include <windows.h>
int main ()
{
system ("start notepad.exe") // As an example. Change [notepad] to any executable file //
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
149914 次 |
最近记录: |