Home > Article > Backend Development > How to Launch Executable Files Securely in C : Why CreateProcess() Is Your Best Choice?
In this guide, we'll explore how to open an executable file (.exe) from within another C executable.
Before delving into the solution, it's crucial to highlight the dangers of employing the system() function. System() exhibits several drawbacks:
Instead of system(), we recommend utilizing the CreateProcess() function. This function allows you to launch an executable file, creating an independent process.
#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, // executable path argv[1], // command line NULL, // process handle not inheritable NULL, // thread handle not inheritable FALSE, // no handle inheritance 0, // no creation flags NULL, // parent's environment block NULL, // parent's starting directory &si, // STARTUPINFO structure &pi // PROCESS_INFORMATION structure ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); }
The error you encountered likely stems from the fact that you didn't specify the full path of the executable file. Ensure that you provide the complete path, including the file name.
The above is the detailed content of How to Launch Executable Files Securely in C : Why CreateProcess() Is Your Best Choice?. For more information, please follow other related articles on the PHP Chinese website!