Home > Backend Development > C++ > body text

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

Barbara Streisand
Release: 2024-10-30 04:03:02
Original
494 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!