zoukankan      html  css  js  c++  java
  • 使用CreateProcess创建新的process 并返回process运行结束返回值

    转自:http://blog.csdn.net/zgl7903/article/details/5975284

    转载这篇主要是记住:获得create的新进程运行结束时的返回值的方法

    如下:

    1.  
      #include <malloc.h>
    2.  
       
    3.  
      DWORD run_Execute(LPCTSTR lpszFile, LPCTSTR lpszParam)
    4.  
      {
    5.  
      DWORD exitCode = 0;
    6.  
      PROCESS_INFORMATION pInfo = {0};
    7.  
      STARTUPINFO sInfo = {0};
    8.  
      sInfo.cb = sizeof(STARTUPINFO);
    9.  
      sInfo.wShowWindow = SW_SHOW;
    10.  
       
    11.  
      int nCmdLen = (_tcslen(lpszFile) + _tcslen(lpszParam) + 2) * sizeof(TCHAR);
    12.  
      LPTSTR lpszCmd = (LPTSTR)_alloca(nCmdLen);
    13.  
      memset(lpszCmd, 0, nCmdLen);
    14.  
      _tcscpy(lpszCmd, lpszFile);
    15.  
      if(lpszParam)
    16.  
      {
    17.  
      _tcscat(lpszCmd, _T(" "));
    18.  
      _tcscat(lpszCmd, lpszParam);
    19.  
      }
    20.  
       
    21.  
      if(CreateProcess(
    22.  
      NULL, //LPCTSTR lpApplicationName, // pointer to name of executable module
    23.  
      lpszCmd, //LPTSTR lpCommandLine, // pointer to command line string
    24.  
      NULL, //LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes
    25.  
      NULL, //LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes
    26.  
      FALSE, //BOOL bInheritHandles, // handle inheritance flag
    27.  
      0, //DWORD dwCreationFlags, // creation flags
    28.  
      NULL, //LPVOID lpEnvironment, // pointer to new environment block
    29.  
      NULL, //LPCTSTR lpCurrentDirectory, // pointer to current directory name
    30.  
      &sInfo, //LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
    31.  
      &pInfo)) //LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
    32.  
      {
    33.  
      // Wait until child process exits.
    34.  
      WaitForSingleObject( pInfo.hProcess, INFINITE );
    35.  
       
    36.  
      if (GetExitCodeProcess(pInfo.hProcess, &exitCode))
    37.  
      {
    38.  
      TRACE( _T("Exit code = %d/n"), exitCode);
    39.  
      }
    40.  
      else
    41.  
      {
    42.  
      TRACE( _T("GetExitCodeProcess() failed: %ld/n"), GetLastError());
    43.  
      ASSERT(0);
    44.  
      }
    45.  
       
    46.  
      // Close process and thread handles.
    47.  
      CloseHandle( pInfo.hProcess );
    48.  
      CloseHandle( pInfo.hThread );
    49.  
      }
    50.  
      else
    51.  
      {
    52.  
      TRACE( _T("CreateProcess() failed: %ld/n"), GetLastError());
    53.  
      ASSERT(0);
    54.  
      }
    55.  
       
    56.  
      return exitCode;
    57.  
      }

    1.  
      //测试示例
    2.  
      run_Execute(_T("notepad.exe"), _T("c://temp//aa.txt"));

    jpg 转 rar 
  • 相关阅读:
    LeetCode 842. Split Array into Fibonacci Sequence
    LeetCode 1087. Brace Expansion
    LeetCode 1219. Path with Maximum Gold
    LeetCode 1079. Letter Tile Possibilities
    LeetCode 1049. Last Stone Weight II
    LeetCode 1046. Last Stone Weight
    LeetCode 1139. Largest 1-Bordered Square
    LeetCode 764. Largest Plus Sign
    LeetCode 1105. Filling Bookcase Shelves
    LeetCode 1027. Longest Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/kuangke/p/9524138.html
Copyright © 2011-2022 走看看