BOOL
ExecuteCommand(
IN LPTSTR lpCommand
)
{
BOOL bRet = FALSE;
UINT uResult = 0;
TCHAR CommandLine[MAX_PATH] = {0};
STARTUPINFO StartupInfo = {0};
PROCESS_INFORMATION ProcessInfo = {0};
__try
{
uResult = GetSystemDirectory(CommandLine, MAX_PATH);
if (!uResult)
{
printf("[ExecuteCommand] : GetSystemDirectory failed. (%d) \n", GetLastError());
__leave;
}
_tcscat_s(CommandLine, MAX_PATH, _T("\\cmd.exe /c "));
_tcscat_s(CommandLine, MAX_PATH, lpCommand);
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
bRet = CreateProcess(NULL, CommandLine, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
if (!bRet)
{
printf("[ExecuteCommand] : CreateProcess failed. (%d) \n", GetLastError());
__leave;
}
if (ProcessInfo.hProcess)
{
// 是cmd的句柄
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
}
}
__finally
{
if (ProcessInfo.hProcess)
CloseHandle(ProcessInfo.hProcess);
if (ProcessInfo.hThread)
CloseHandle(ProcessInfo.hThread);
}
return bRet;
}
使用CreateProcess执行命令行
最新推荐文章于 2025-03-11 09:15:00 发布