Home  >  Article  >  Backend Development  >  How to Launch an .exe File from Another C .exe Using CreateProcess()?

How to Launch an .exe File from Another C .exe Using CreateProcess()?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 04:03:02495browse

How to Launch an .exe File from Another C   .exe Using CreateProcess()?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn