Win32 API 日本語リファレンス
ホームSystem.Threading › GetProcessInformation

GetProcessInformation

関数
指定プロセスの情報をクラス指定で取得する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSwindows8.0

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL GetProcessInformation(
    HANDLE hProcess,
    PROCESS_INFORMATION_CLASS ProcessInformationClass,
    void* ProcessInformation,
    DWORD ProcessInformationSize
);

パラメーター

名前方向説明
hProcessHANDLEinプロセスへのハンドル。このハンドルには少なくとも PROCESS_QUERY_LIMITED_INFORMATION アクセス権が必要です。詳細については、Process Security and Access Rights を参照してください。
ProcessInformationClassPROCESS_INFORMATION_CLASSin取得する情報の種類を指定する PROCESS_INFORMATION_CLASS 列挙体のメンバー。
ProcessInformationvoid*out

ProcessInformationClass パラメーターで指定された種類の情報を受け取るオブジェクトへのポインター。

ProcessInformationClass パラメーターが ProcessMemoryPriority の場合、このパラメーターは MEMORY_PRIORITY_INFORMATION structure を指している必要があります。

ProcessInformationClass パラメーターが ProcessPowerThrottling の場合、このパラメーターは PROCESS_POWER_THROTTLING_STATE structure を指している必要があります。

ProcessInformationClass パラメーターが ProcessProtectionLevelInfo の場合、このパラメーターは PROCESS_PROTECTION_LEVEL_INFORMATION structure を指している必要があります。

ProcessInformationClass パラメーターが ProcessLeapSecondInfo の場合、このパラメーターは PROCESS_LEAP_SECOND_INFO structure を指している必要があります。

ProcessInformationClass パラメーターが ProcessAppMemoryInfo の場合、このパラメーターは APP_MEMORY_INFORMATION structure を指している必要があります。

ProcessInformationClass パラメーターが ProcessMaxOverridePrefetchParameter の場合、このパラメーターは OVERRIDE_PREFETCH_PARAMETER structure を指している必要があります。

ProcessInformationSizeDWORDin

ProcessInformation パラメーターで指定された構造体のサイズ(バイト単位)。

ProcessInformationClass パラメーターが ProcessMemoryPriority の場合、このパラメーターは sizeof(MEMORY_PRIORITY_INFORMATION) である必要があります。

ProcessInformationClass パラメーターが ProcessPowerThrottling の場合、このパラメーターは sizeof(PROCESS_POWER_THROTTLING_STATE) である必要があります。

ProcessInformationClass パラメーターが ProcessProtectionLevelInfo の場合、このパラメーターは sizeof(PROCESS_PROTECTION_LEVEL_INFORMATION) である必要があります。

ProcessInformationClass パラメーターが ProcessLeapSecondInfo の場合、このパラメーターは sizeof(PROCESS_LEAP_SECOND_INFO) である必要があります。

ProcessInformationClass パラメーターが ProcessAppMemoryInfo の場合、このパラメーターは sizeof(APP_MEMORY_INFORMATION) である必要があります。

戻り値の型: BOOL

公式ドキュメント

指定したプロセスに関する情報を取得します。(GetProcessInformation)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError function を呼び出します。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL GetProcessInformation(
    HANDLE hProcess,
    PROCESS_INFORMATION_CLASS ProcessInformationClass,
    void* ProcessInformation,
    DWORD ProcessInformationSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetProcessInformation(
    IntPtr hProcess,   // HANDLE
    int ProcessInformationClass,   // PROCESS_INFORMATION_CLASS
    IntPtr ProcessInformation,   // void* out
    uint ProcessInformationSize   // DWORD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetProcessInformation(
    hProcess As IntPtr,   ' HANDLE
    ProcessInformationClass As Integer,   ' PROCESS_INFORMATION_CLASS
    ProcessInformation As IntPtr,   ' void* out
    ProcessInformationSize As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hProcess : HANDLE
' ProcessInformationClass : PROCESS_INFORMATION_CLASS
' ProcessInformation : void* out
' ProcessInformationSize : DWORD
Declare PtrSafe Function GetProcessInformation Lib "kernel32" ( _
    ByVal hProcess As LongPtr, _
    ByVal ProcessInformationClass As Long, _
    ByVal ProcessInformation As LongPtr, _
    ByVal ProcessInformationSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetProcessInformation = ctypes.windll.kernel32.GetProcessInformation
GetProcessInformation.restype = wintypes.BOOL
GetProcessInformation.argtypes = [
    wintypes.HANDLE,  # hProcess : HANDLE
    ctypes.c_int,  # ProcessInformationClass : PROCESS_INFORMATION_CLASS
    ctypes.POINTER(None),  # ProcessInformation : void* out
    wintypes.DWORD,  # ProcessInformationSize : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetProcessInformation = Fiddle::Function.new(
  lib['GetProcessInformation'],
  [
    Fiddle::TYPE_VOIDP,  # hProcess : HANDLE
    Fiddle::TYPE_INT,  # ProcessInformationClass : PROCESS_INFORMATION_CLASS
    Fiddle::TYPE_VOIDP,  # ProcessInformation : void* out
    -Fiddle::TYPE_INT,  # ProcessInformationSize : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetProcessInformation(
        hProcess: *mut core::ffi::c_void,  // HANDLE
        ProcessInformationClass: i32,  // PROCESS_INFORMATION_CLASS
        ProcessInformation: *mut (),  // void* out
        ProcessInformationSize: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool GetProcessInformation(IntPtr hProcess, int ProcessInformationClass, IntPtr ProcessInformation, uint ProcessInformationSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetProcessInformation' -Namespace Win32 -PassThru
# $api::GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
#uselib "KERNEL32.dll"
#func global GetProcessInformation "GetProcessInformation" sptr, sptr, sptr, sptr
; GetProcessInformation hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize   ; 戻り値は stat
; hProcess : HANDLE -> "sptr"
; ProcessInformationClass : PROCESS_INFORMATION_CLASS -> "sptr"
; ProcessInformation : void* out -> "sptr"
; ProcessInformationSize : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GetProcessInformation "GetProcessInformation" sptr, int, sptr, int
; res = GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
; hProcess : HANDLE -> "sptr"
; ProcessInformationClass : PROCESS_INFORMATION_CLASS -> "int"
; ProcessInformation : void* out -> "sptr"
; ProcessInformationSize : DWORD -> "int"
; BOOL GetProcessInformation(HANDLE hProcess, PROCESS_INFORMATION_CLASS ProcessInformationClass, void* ProcessInformation, DWORD ProcessInformationSize)
#uselib "KERNEL32.dll"
#cfunc global GetProcessInformation "GetProcessInformation" intptr, int, intptr, int
; res = GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
; hProcess : HANDLE -> "intptr"
; ProcessInformationClass : PROCESS_INFORMATION_CLASS -> "int"
; ProcessInformation : void* out -> "intptr"
; ProcessInformationSize : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetProcessInformation = kernel32.NewProc("GetProcessInformation")
)

// hProcess (HANDLE), ProcessInformationClass (PROCESS_INFORMATION_CLASS), ProcessInformation (void* out), ProcessInformationSize (DWORD)
r1, _, err := procGetProcessInformation.Call(
	uintptr(hProcess),
	uintptr(ProcessInformationClass),
	uintptr(ProcessInformation),
	uintptr(ProcessInformationSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetProcessInformation(
  hProcess: THandle;   // HANDLE
  ProcessInformationClass: Integer;   // PROCESS_INFORMATION_CLASS
  ProcessInformation: Pointer;   // void* out
  ProcessInformationSize: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetProcessInformation';
result := DllCall("KERNEL32\GetProcessInformation"
    , "Ptr", hProcess   ; HANDLE
    , "Int", ProcessInformationClass   ; PROCESS_INFORMATION_CLASS
    , "Ptr", ProcessInformation   ; void* out
    , "UInt", ProcessInformationSize   ; DWORD
    , "Int")   ; return: BOOL
●GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize) = DLL("KERNEL32.dll", "bool GetProcessInformation(void*, int, void*, dword)")
# 呼び出し: GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
# hProcess : HANDLE -> "void*"
# ProcessInformationClass : PROCESS_INFORMATION_CLASS -> "int"
# ProcessInformation : void* out -> "void*"
# ProcessInformationSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn GetProcessInformation(
    hProcess: ?*anyopaque, // HANDLE
    ProcessInformationClass: i32, // PROCESS_INFORMATION_CLASS
    ProcessInformation: ?*anyopaque, // void* out
    ProcessInformationSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc GetProcessInformation(
    hProcess: pointer,  # HANDLE
    ProcessInformationClass: int32,  # PROCESS_INFORMATION_CLASS
    ProcessInformation: pointer,  # void* out
    ProcessInformationSize: uint32  # DWORD
): int32 {.importc: "GetProcessInformation", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int GetProcessInformation(
    void* hProcess,   // HANDLE
    int ProcessInformationClass,   // PROCESS_INFORMATION_CLASS
    void* ProcessInformation,   // void* out
    uint ProcessInformationSize   // DWORD
);
ccall((:GetProcessInformation, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Int32, Ptr{Cvoid}, UInt32),
      hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
# hProcess : HANDLE -> Ptr{Cvoid}
# ProcessInformationClass : PROCESS_INFORMATION_CLASS -> Int32
# ProcessInformation : void* out -> Ptr{Cvoid}
# ProcessInformationSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetProcessInformation(
    void* hProcess,
    int32_t ProcessInformationClass,
    void* ProcessInformation,
    uint32_t ProcessInformationSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
-- hProcess : HANDLE
-- ProcessInformationClass : PROCESS_INFORMATION_CLASS
-- ProcessInformation : void* out
-- ProcessInformationSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetProcessInformation = lib.func('__stdcall', 'GetProcessInformation', 'int32_t', ['void *', 'int32_t', 'void *', 'uint32_t']);
// GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
// hProcess : HANDLE -> 'void *'
// ProcessInformationClass : PROCESS_INFORMATION_CLASS -> 'int32_t'
// ProcessInformation : void* out -> 'void *'
// ProcessInformationSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetProcessInformation: { parameters: ["pointer", "i32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize)
// hProcess : HANDLE -> "pointer"
// ProcessInformationClass : PROCESS_INFORMATION_CLASS -> "i32"
// ProcessInformation : void* out -> "pointer"
// ProcessInformationSize : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetProcessInformation(
    void* hProcess,
    int32_t ProcessInformationClass,
    void* ProcessInformation,
    uint32_t ProcessInformationSize);
C, "KERNEL32.dll");
// $ffi->GetProcessInformation(hProcess, ProcessInformationClass, ProcessInformation, ProcessInformationSize);
// hProcess : HANDLE
// ProcessInformationClass : PROCESS_INFORMATION_CLASS
// ProcessInformation : void* out
// ProcessInformationSize : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean GetProcessInformation(
        Pointer hProcess,   // HANDLE
        int ProcessInformationClass,   // PROCESS_INFORMATION_CLASS
        Pointer ProcessInformation,   // void* out
        int ProcessInformationSize   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetProcessInformation = GetProcessInformation(
    hProcess : Void*,   # HANDLE
    ProcessInformationClass : Int32,   # PROCESS_INFORMATION_CLASS
    ProcessInformation : Void*,   # void* out
    ProcessInformationSize : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetProcessInformationNative = Int32 Function(Pointer<Void>, Int32, Pointer<Void>, Uint32);
typedef GetProcessInformationDart = int Function(Pointer<Void>, int, Pointer<Void>, int);
final GetProcessInformation = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetProcessInformationNative, GetProcessInformationDart>('GetProcessInformation');
// hProcess : HANDLE -> Pointer<Void>
// ProcessInformationClass : PROCESS_INFORMATION_CLASS -> Int32
// ProcessInformation : void* out -> Pointer<Void>
// ProcessInformationSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetProcessInformation(
  hProcess: THandle;   // HANDLE
  ProcessInformationClass: Integer;   // PROCESS_INFORMATION_CLASS
  ProcessInformation: Pointer;   // void* out
  ProcessInformationSize: DWORD   // DWORD
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetProcessInformation';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetProcessInformation"
  c_GetProcessInformation :: Ptr () -> Int32 -> Ptr () -> Word32 -> IO CInt
-- hProcess : HANDLE -> Ptr ()
-- ProcessInformationClass : PROCESS_INFORMATION_CLASS -> Int32
-- ProcessInformation : void* out -> Ptr ()
-- ProcessInformationSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getprocessinformation =
  foreign "GetProcessInformation"
    ((ptr void) @-> int32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hProcess : HANDLE -> (ptr void) *)
(* ProcessInformationClass : PROCESS_INFORMATION_CLASS -> int32_t *)
(* ProcessInformation : void* out -> (ptr void) *)
(* ProcessInformationSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetProcessInformation" get-process-information :convention :stdcall) :int32
  (h-process :pointer)   ; HANDLE
  (process-information-class :int32)   ; PROCESS_INFORMATION_CLASS
  (process-information :pointer)   ; void* out
  (process-information-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetProcessInformation = Win32::API::More->new('KERNEL32',
    'BOOL GetProcessInformation(HANDLE hProcess, int ProcessInformationClass, LPVOID ProcessInformation, DWORD ProcessInformationSize)');
# my $ret = $GetProcessInformation->Call($hProcess, $ProcessInformationClass, $ProcessInformation, $ProcessInformationSize);
# hProcess : HANDLE -> HANDLE
# ProcessInformationClass : PROCESS_INFORMATION_CLASS -> int
# ProcessInformation : void* out -> LPVOID
# ProcessInformationSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型