MSDN Documentation

Home Search

GetProcessId

Retrieves the process identifier of the specified process.

Header

#include <processthreadsapi.h>

Syntax

DWORD GetProcessId(
    HANDLE hProcess
);

Parameters

ParameterDescription
hProcessHandle to the process. The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right.

Return value

If the function succeeds, the return value is the process identifier. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Example

C++
C#
#include <windows.h>
#include <iostream>

int main() {
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    if (CreateProcess(L"C:\\Windows\\System32\\notepad.exe",
                      NULL, NULL, NULL, FALSE, 0, NULL, NULL,
                      &si, &pi)) {
        DWORD pid = GetProcessId(pi.hProcess);
        std::wcout << L"Process ID: " << pid << std::endl;
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    } else {
        std::cerr << "CreateProcess failed: " << GetLastError() << std::endl;
    }
    return 0;
}
using System;
using System.Diagnostics;

class Program {
    static void Main() {
        Process p = Process.Start(@"C:\Windows\System32\notepad.exe");
        Console.WriteLine($"Process ID: {p.Id}");
        p.WaitForExit();
    }
}

See Also