使用CreateProcess执行命令行

来源:互联网 发布:淘宝发布被限制 编辑:程序博客网 时间:2024/05/18 14:42
BOOLExecuteCommand(IN LPTSTR lpCommand){BOOLbRet= FALSE;UINTuResult= 0;TCHARCommandLine[MAX_PATH]= {0};STARTUPINFOStartupInfo= {0};PROCESS_INFORMATIONProcessInfo= {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;}

0 0
原创粉丝点击