你应该总是避免使用system(),因为
这是资源沉重
它打败了安全 – 你不知道你是一个有效的命令还是在每个系统上都做同样的事情,你甚至可以启动你不打算启动的程序。 危险在于,当你直接执行一个程序时,它会获得与你的程序相同的权限 – 也就是说,例如,如果你以系统pipe理员身份运行,那么你不经意间执行的恶意程序也以系统pipe理员身份运行。 如果这不能吓倒你,请检查你的脉搏。
反病毒程序讨厌它,你的程序可能被标记为病毒。
你应该使用CreateProcess() 。
您可以使用Createprocess()来启动一个.exe,并为其创build一个新的进程。 应用程序将独立于调用应用程序运行。
以下是我在其中一个项目中使用的示例:
#include 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 ); }
编辑:你得到的错误是因为你需要指定的.exe文件的path不只是名称。 Openfile.exe可能不存在。