Opening an .exe File from C Using CreateProcess()
When aiming to launch an .exe file from another C .exe, the system() function is generally discouraged due to its potential drawbacks related to security, efficiency, and antivirus compatibility.
Instead, consider utilizing the CreateProcess() function, which allows you to spawn a new process to execute the target .exe file independently. Here's an example demonstrating its usage:
<code class="c++">#include <windows.h> VOID startup(LPCTSTR lpApplicationName) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); CreateProcess(lpApplicationName, argv[1], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }</code>
Pass in the absolute path to the .exe file as the lpApplicationName argument to this function.
Troubleshooting the Previous Code
In your original code using system(), the error you encountered likely resulted from the lack of a path to OpenFile.exe. Ensure that the specified .exe file exists in the path you provide to system() or CreateProcess().
The above is the detailed content of How to Launch an .exe File from Another C .exe Using CreateProcess()?. For more information, please follow other related articles on the PHP Chinese website!