读书人

怎么运行指定程序

发布时间: 2012-03-05 11:54:02 作者: rapoo

如何运行指定程序?
要求是:给定进程名称,如何在应用程序代码中运行这个给定的程序?

[解决办法]
运行指定位置的可执行程序:

STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProInfo ;
DWORD ErrorCode;

memset(&StartupInfo,0,sizeof(STARTUPINFO));
StartupInfo.cb=sizeof(STARTUPINFO);
StartupInfo.lpReserved=NULL;
StartupInfo.lpDesktop=NULL;
StartupInfo.lpTitle=NULL;
StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
StartupInfo.cbReserved2=0;
StartupInfo.lpReserved2=NULL;
StartupInfo.wShowWindow=SW_SHOWNORMAL;

bool bReturn=CreateProcess(NULL, "c:\\windows\\notepad.exe ",NULL,
NULL,FALSE,0,NULL,NULL,&StartupInfo,&ProInfo);
ErrorCode=GetLastError();

CloseHandle(ProInfo.hThread);
//等待子进程的退出
WaitForSingleObject(ProInfo.hProcess, INFINITE);
//获取子进程的退出码
GetExitCodeProcess(ProInfo.hProcess, &ErrorCode);
//关闭子进程句柄
CloseHandle(ProInfo.hProcess);

读书人网 >VC/MFC

热点推荐