프로세스 만들기

CreateProcess 함수는 만들기 프로세스와 독립적으로 실행되는 새 프로세스를 만듭니다. 간단히 하기 위해 이 관계를 부모-자식 관계라고 합니다.

다음 코드에서는 프로세스를 만드는 방법을 보여 줍니다.

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

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

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

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

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

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

CreateProcess가 성공하면 새 프로세스 및 기본 스레드에 대한 핸들 및 식별자가 포함된 PROCESS_INFORMATION 구조를 반환합니다. 스레드 및 프로세스 핸들은 전체 액세스 권한으로 만들어지지만 보안 설명자를 지정하면 액세스를 제한할 수 있습니다. 이러한 핸들이 더 이상 필요하지 않으면 CloseHandle 함수를 사용하여 핸들을 닫습니다.

CreateProcessAsUser 또는 CreateProcessWithLogonW 함수를 사용하여 프로세스를 만들 수도 있습니다. 이러한 함수를 사용하면 프로세스가 실행되는 사용자 계정의 보안 컨텍스트를 지정할 수 있습니다.