Windows API一日一練(87)CreateProcess函數

來源:互聯網
上載者:User
  Windows API一日一練(87)CreateProcess函數

人們需要處理的資訊越來越複雜,往往在一個應用程式裡是處理不完的,因此,就出現多個應用程式協同處理同一件事情。當然多個應用程式分開處理,也是比較容易開發,並且讓應用程式複雜難度迅速降低。比如在開發一個銀行的交易系統,有一個報表產生的主程式,然後還有很多小的,不同的報告產生器。這樣就需要從主程式裡建立小報表程式進行運行。建立進程運行,需要使用函數CreateProcess來實現。

函數CreateProcess聲明如下:

WINBASEAPI
BOOL
WINAPI
CreateProcessA(
    __in_opt    LPCSTR lpApplicationName,
    __inout_opt LPSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOA lpStartupInfo,
    __out      LPPROCESS_INFORMATION lpProcessInformation
    );
WINBASEAPI
BOOL
WINAPI
CreateProcessW(
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out      LPPROCESS_INFORMATION lpProcessInformation
    );
#ifdef UNICODE
#define CreateProcess CreateProcessW
#else
#define CreateProcess CreateProcessA
#endif // !UNICODE

lpApplicationName是應用程式的名稱。
lpCommandLine是命令列參數。
lpProcessAttributes是進程的屬性。
lpThreadAttributes是線程的屬性。
bInheritHandles是否繼承父進程的屬性。
dwCreationFlags是建立標誌。
lpEnvironment是環境變數。
lpCurrentDirectory是目前的目錄。
lpStartupInfo是傳給新進程的資訊。
lpProcessInformation是進程返回的資訊。

調用函數的例子如下:
#001 //建立進程。
#002  //蔡軍生 2007/12/11 QQ:9073204 深圳
#003  void TestCreateProcess(void)
#004  {
#005        //清空結構。
#006        STARTUPINFO sInfo;
#007        PROCESS_INFORMATION pInfo;
#008
#009        ZeroMemory( &sInfo, sizeof(sInfo) );
#010        sInfo.cb = sizeof(sInfo);
#011        sInfo.dwFlags = STARTF_USESHOWWINDOW;
#012        sInfo.wShowWindow = SW_SHOWNORMAL;
#013
#014        ZeroMemory( &pInfo, sizeof(pInfo) );
#015
#016        //建立一個進程。     
#017        if( !::CreateProcess( _T("WinCpp.exe"), 
#018              NULL,
#019              NULL,           
#020              NULL,           
#021              FALSE,           
#022              0,               
#023              NULL,           
#024              NULL,           
#025              &sInfo,             
#026              &pInfo )           
#027              )
#028        {
#029              //輸出出錯資訊。
#030              const int nBufSize = 512;
#031              TCHAR chBuf[nBufSize];
#032              ZeroMemory(chBuf,nBufSize);
#033
#034              wsprintf(chBuf,_T("CreateProcess failed (%d)./n"), GetLastError() );
#035              OutputDebugString(chBuf);
#036              return;
#037        }
#038
#039
#040        //等進程關閉。
#041        WaitForSingleObject( pInfo.hProcess, INFINITE );
#042
#043        //關閉進程和線程的控制代碼。
#044        CloseHandle( pInfo.hProcess );
#045        CloseHandle( pInfo.hThread );
#046
#047  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.