共用方式為


C6335

更新:2007 年 11 月

警告 C6335: 遺漏處理序資訊控制代碼 <handlename>

這則警告表示函式之 CreateProcess 系統所傳回的處理序資訊控制代碼,需要使用 CloseHandle 予以關閉。若無法這麼做,將會導致控制代碼遺漏。

範例

下列程式碼將產生出這個警告:

#include <windows.h>
#include <stdio.h>

void f( )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( "C:\\WINDOWS\\system32\\calc.exe",
                        NULL,  
                        NULL,             
                        NULL,              
                        FALSE,             
                        0,                 
                        NULL,              
                        NULL,              
                        &si,    // Pointer to STARTUPINFO structure.
                        &pi ) ) // Pointer to PROCESS_INFORMATION
  {
    puts("Error");
    return;
  }
    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
}

若要更正這則警告,請依下列程式碼所示,呼叫 CloseHandle (pi.hThread) 以關閉執行緒控制代碼:

#include <windows.h>
#include <stdio.h>

void f( )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( "C:\\WINDOWS\\system32\\calc.exe",
                        NULL,  
                        NULL,             
                        NULL,              
                        FALSE,             
                        0,                 
                        NULL,              
                        NULL,              
                        &si,    // Pointer to STARTUPINFO structure.
                        &pi ) ) // Pointer to PROCESS_INFORMATION
    {
      puts("Error");
      return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

請參閱

其他資源

CreateProcess

CloseHandle