参考的这里
但是有问题,主要是微软抛弃了一些接口
修改如下
static DWORD RunSilent(char* strFunct, char* strstrParams)
{
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
char Args[4096];
char *pEnvCMD = NULL;
char *pDefaultCMD = "CMD.EXE";
ULONG rc;
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
Args[0] = 0;
//pEnvCMD = getenv("COMSPEC");
size_t t_requiredSize;
//errno_t t_err = _dupenv_s(&pEnvCMD,&len,"COMSPEC");
//获取cmd的路径
getenv_s(&t_requiredSize, NULL, 0, "COMSPEC");
if (0 == t_requiredSize)
{
printf("robin:not found this comspec\n");
return -1;
}
pEnvCMD = (char *)malloc(t_requiredSize * sizeof(char));
getenv_s(&t_requiredSize, pEnvCMD, t_requiredSize, "COMSPEC");
if (pEnvCMD){
//strcpy(Args, pEnvCMD);
strcpy_s(Args, pEnvCMD);
}
else{
//strcpy(Args, pDefaultCMD);
strcpy_s(Args, pDefaultCMD);
}
// "/c" option - Do the command then terminate the command window
//strcat(Args, " /c ");
strcat_s(Args, " /c ");
//the application you would like to run from the command window
//strcat(Args, strFunct);
//strcat(Args, " ");
strcat_s(Args, strFunct);
strcat_s(Args, " ");
//the parameters passed to the application being run from the command window.
strcat_s(Args, strstrParams);
if (!CreateProcess(NULL, Args, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&StartupInfo,
&ProcessInfo))
{
return GetLastError();
}
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
rc = 0;
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
free(pEnvCMD);
return rc;
}
这些安全的函数没有验证返回值是否正确,我懒~~