Created
          July 18, 2014 06:15 
        
      - 
      
 - 
        
Save wyyqyl/ba3d63385a711d42e8ab to your computer and use it in GitHub Desktop.  
    run_process
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | void run_process(const wchar_t* name, const wchar_t* param) { | |
| SHELLEXECUTEINFOW info; | |
| info.cbSize = sizeof(SHELLEXECUTEINFOW); | |
| info.fMask = SEE_MASK_NOCLOSEPROCESS; | |
| info.hwnd = NULL; | |
| info.lpVerb = NULL; | |
| info.lpFile = name; | |
| info.lpParameters = param; | |
| info.lpDirectory = NULL; | |
| info.nShow = SW_HIDE; | |
| info.hInstApp = NULL; | |
| ShellExecuteExW(&info); | |
| WaitForSingleObject(info.hProcess, INFINITE); | |
| } | |
| void run_process(const std::wstring process, const std::wstring param, | |
| const std::wstring outfile /*= std::wstring()*/) { | |
| HANDLE h = INVALID_HANDLE_VALUE; | |
| if (outfile.length() > 0) { | |
| SECURITY_ATTRIBUTES sa; | |
| sa.nLength = sizeof(sa); | |
| sa.lpSecurityDescriptor = NULL; | |
| sa.bInheritHandle = TRUE; | |
| h = CreateFileW(outfile.c_str(), GENERIC_WRITE, NULL, &sa, CREATE_ALWAYS, | |
| FILE_ATTRIBUTE_NORMAL, NULL); | |
| } | |
| PROCESS_INFORMATION pi = {0}; | |
| STARTUPINFO si = {0}; | |
| si.cb = sizeof(STARTUPINFO); | |
| si.dwFlags |= STARTF_USESTDHANDLES; | |
| si.hStdInput = NULL; | |
| si.hStdError = NULL; | |
| si.hStdOutput = h; | |
| std::wstring cmd = process + L" " + param; | |
| BOOL ret = CreateProcess(NULL, (LPWSTR)(cmd.c_str()), NULL, NULL, TRUE, | |
| CREATE_NO_WINDOW, NULL, NULL, &si, &pi); | |
| if (ret) { | |
| WaitForSingleObject(pi.hProcess, INFINITE); | |
| CloseHandle(pi.hProcess); | |
| CloseHandle(pi.hThread); | |
| } | |
| if (h != INVALID_HANDLE_VALUE) { | |
| CloseHandle(h); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment