C中调用带参数的exe并接收返回值

本文详细介绍了如何使用C++编写代码来启动并获取test.exe进程的退出状态。通过调用CreateProcess函数启动目标进程,并利用GetExitCodeProcess函数获取其退出代码,从而实现对进程生命周期的监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

test.exe

[c-sharp]  view plain copy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. int main(int argc, char* argv[])  
  4. {  
  5.  return 0;  
  6. }  

 获取test.exe的返回值

[c-sharp]  view plain copy
  1. #include "stdafx.h"  
  2. #include "windows.h"  
  3. int main(int argc, char* argv[])  
  4. {  
  5.     DWORD    dwExitCode = -1;  
  6.   
  7.     STARTUPINFO si;  
  8.     PROCESS_INFORMATION pi;  
  9.       
  10.     ZeroMemory( &si, sizeof(si) );  
  11.     si.cb = sizeof(si);  
  12.     ZeroMemory( &pi, sizeof(pi) );  
  13.       
  14.     // Start the child process.   
  15.     if( !CreateProcess( "E://test.exe", // an exe file.   
  16.         "hello.txt",        // parameter for your exe file.   
  17.         NULL,             // Process handle not inheritable.   
  18.         NULL,             // Thread handle not inheritable.   
  19.         FALSE,            // Set handle inheritance to FALSE.   
  20.         0,                // No creation flags.   
  21.         NULL,             // Use parent's environment block.   
  22.         NULL,             // Use parent's starting directory.   
  23.         &si,              // Pointer to STARTUPINFO structure.  
  24.         &pi )             // Pointer to PROCESS_INFORMATION structure.  
  25.         )   
  26.     {  
  27.         MessageBox(NULL, "CreateProcess failed.","ERROR",NULL );  
  28.     }  
  29.       
  30.     // Wait until child process exits.  
  31.     WaitForSingleObject( pi.hProcess, INFINITE );  
  32.       
  33.     GetExitCodeProcess(pi.hProcess,&dwExitCode);  
  34.       
  35.     printf("Exit code : %d/n",dwExitCode);  
  36.       
  37.     // Close process and thread handles.   
  38.     CloseHandle( pi.hProcess );  
  39.     CloseHandle( pi.hThread );  
  40.       
  41.     return 0;  
  42.   
  43. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值