关于c++中调用exe的几种方式

来源:互联网 发布:威海志成网络 编辑:程序博客网 时间:2024/06/09 22:44

1,利用CreateProcess调用

#include <windows.h>int run_exe(string& str_exe_path, string& str_cmd_path){    PROCESS_INFORMATION ProcessInfo;    STARTUPINFO StartupInfo;    ZeroMemory(&StartupInfo, sizeof(StartupInfo));    StartupInfo.cb = sizeof StartupInfo;    StartupInfo.lpReserved = NULL;    StartupInfo.lpDesktop = NULL;    StartupInfo.lpTitle = NULL;    StartupInfo.dwFlags = STARTF_USESHOWWINDOW;    StartupInfo.wShowWindow = SW_HIDE;    StartupInfo.cbReserved2 = NULL;    StartupInfo.lpReserved2 = NULL;    char* pszCmdLine = (char*)str_cmd_path.c_str();    if (1 != CreateProcessA(str_exe_path.c_str(), pszCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo))return -1;    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);    DWORD dexitcode = 0; if (1 != GetExitCodeProcess(ProcessInfo.hProcess, &dexitcode))return -1;    CloseHandle(ProcessInfo.hThread);    CloseHandle(ProcessInfo.hProcess);    return 0;}

其中:str_exe_path为exe的路径,str_cmd_path为str_exe_path+“调用参数”,参数之间需用空格分开。

2,利用WinExec调用
这个函数最简单,只有两个参数,原型如下:
UINT WinExec( LPCSTR lpCmdLine,UINT uCmdShow );
lpCmdLine为cmd命令参数,uCmdShow 为显示方式 。
上面的例子用WinExec调用,如下:
WinExec(pszCmdLine, SW_SHOW);

原创粉丝点击