K32EnumDeviceDrivers
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL K32EnumDeviceDrivers(
void** lpImageBase,
DWORD cb,
DWORD* lpcbNeeded
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpImageBase | void** | out | デバイスドライバーのロードアドレスの一覧を受け取る配列です。 |
| cb | DWORD | in | lpImageBase 配列のサイズ(バイト単位)です。配列がロードアドレスを格納するのに十分な大きさでない場合、lpcbNeeded パラメーターに必要な配列のサイズが返されます。 |
| lpcbNeeded | DWORD* | out | lpImageBase 配列に返されたバイト数です。 |
戻り値の型: BOOL
公式ドキュメント
システム内の各デバイスドライバーのロードアドレスを取得します。
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
EnumDeviceDrivers の呼び出しによって列挙されたデバイスドライバーの数を求めるには、lpcbNeeded パラメーターに返された値を sizeof(LPVOID) で割ります。
Windows 7 および Windows Server 2008 R2 以降、Psapi.h は PSAPI 関数のバージョン番号を定めています。PSAPI のバージョン番号は、関数を呼び出すために使用する名前と、プログラムが読み込む必要のあるライブラリに影響します。
PSAPI_VERSION が 2 以上の場合、この関数は Psapi.h で K32EnumDeviceDrivers として定義され、Kernel32.lib および Kernel32.dll でエクスポートされます。PSAPI_VERSION が 1 の場合、この関数は Psapi.h で EnumDeviceDrivers として定義され、K32EnumDeviceDrivers を呼び出すラッパーとして Psapi.lib および Psapi.dll でエクスポートされます。
以前のバージョンの Windows と Windows 7 以降の両方で実行する必要があるプログラムでは、常にこの関数を EnumDeviceDrivers として呼び出してください。シンボルを正しく解決するには、TARGETLIBS マクロに Psapi.lib を追加し、–DPSAPI_VERSION=1 を指定してプログラムをコンパイルします。実行時の動的リンクを使用するには、Psapi.dll を読み込みます。
Windows 11 バージョン 24H2 以降、EnumDeviceDrivers が有効な ImageBase の値を返すには SeDebugPrivilege が必要になります。呼び出し元がこの特権を有効にしていない場合でも関数は成功しますが、返される lpImageBase 配列にはすべて NULL のアドレスが格納されます。
例
例については、システム内のすべてのデバイスドライバーの列挙 を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL K32EnumDeviceDrivers(
void** lpImageBase,
DWORD cb,
DWORD* lpcbNeeded
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern bool K32EnumDeviceDrivers(
IntPtr lpImageBase, // void** out
uint cb, // DWORD
out uint lpcbNeeded // DWORD* out
);<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function K32EnumDeviceDrivers(
lpImageBase As IntPtr, ' void** out
cb As UInteger, ' DWORD
<Out> ByRef lpcbNeeded As UInteger ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpImageBase : void** out
' cb : DWORD
' lpcbNeeded : DWORD* out
Declare PtrSafe Function K32EnumDeviceDrivers Lib "kernel32" ( _
ByVal lpImageBase As LongPtr, _
ByVal cb As Long, _
ByRef lpcbNeeded As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
K32EnumDeviceDrivers = ctypes.windll.kernel32.K32EnumDeviceDrivers
K32EnumDeviceDrivers.restype = wintypes.BOOL
K32EnumDeviceDrivers.argtypes = [
ctypes.c_void_p, # lpImageBase : void** out
wintypes.DWORD, # cb : DWORD
ctypes.POINTER(wintypes.DWORD), # lpcbNeeded : DWORD* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
K32EnumDeviceDrivers = Fiddle::Function.new(
lib['K32EnumDeviceDrivers'],
[
Fiddle::TYPE_VOIDP, # lpImageBase : void** out
-Fiddle::TYPE_INT, # cb : DWORD
Fiddle::TYPE_VOIDP, # lpcbNeeded : DWORD* out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn K32EnumDeviceDrivers(
lpImageBase: *mut *mut (), // void** out
cb: u32, // DWORD
lpcbNeeded: *mut u32 // DWORD* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll")]
public static extern bool K32EnumDeviceDrivers(IntPtr lpImageBase, uint cb, out uint lpcbNeeded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_K32EnumDeviceDrivers' -Namespace Win32 -PassThru
# $api::K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)#uselib "KERNEL32.dll"
#func global K32EnumDeviceDrivers "K32EnumDeviceDrivers" sptr, sptr, sptr
; K32EnumDeviceDrivers lpImageBase, cb, varptr(lpcbNeeded) ; 戻り値は stat
; lpImageBase : void** out -> "sptr"
; cb : DWORD -> "sptr"
; lpcbNeeded : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global K32EnumDeviceDrivers "K32EnumDeviceDrivers" sptr, int, var ; res = K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) ; lpImageBase : void** out -> "sptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global K32EnumDeviceDrivers "K32EnumDeviceDrivers" sptr, int, sptr ; res = K32EnumDeviceDrivers(lpImageBase, cb, varptr(lpcbNeeded)) ; lpImageBase : void** out -> "sptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL K32EnumDeviceDrivers(void** lpImageBase, DWORD cb, DWORD* lpcbNeeded) #uselib "KERNEL32.dll" #cfunc global K32EnumDeviceDrivers "K32EnumDeviceDrivers" intptr, int, var ; res = K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) ; lpImageBase : void** out -> "intptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL K32EnumDeviceDrivers(void** lpImageBase, DWORD cb, DWORD* lpcbNeeded) #uselib "KERNEL32.dll" #cfunc global K32EnumDeviceDrivers "K32EnumDeviceDrivers" intptr, int, intptr ; res = K32EnumDeviceDrivers(lpImageBase, cb, varptr(lpcbNeeded)) ; lpImageBase : void** out -> "intptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procK32EnumDeviceDrivers = kernel32.NewProc("K32EnumDeviceDrivers")
)
// lpImageBase (void** out), cb (DWORD), lpcbNeeded (DWORD* out)
r1, _, err := procK32EnumDeviceDrivers.Call(
uintptr(lpImageBase),
uintptr(cb),
uintptr(lpcbNeeded),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction K32EnumDeviceDrivers(
lpImageBase: Pointer; // void** out
cb: DWORD; // DWORD
lpcbNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'K32EnumDeviceDrivers';result := DllCall("KERNEL32\K32EnumDeviceDrivers"
, "Ptr", lpImageBase ; void** out
, "UInt", cb ; DWORD
, "Ptr", lpcbNeeded ; DWORD* out
, "Int") ; return: BOOL●K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) = DLL("KERNEL32.dll", "bool K32EnumDeviceDrivers(void*, dword, void*)")
# 呼び出し: K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)
# lpImageBase : void** out -> "void*"
# cb : DWORD -> "dword"
# lpcbNeeded : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn K32EnumDeviceDrivers(
lpImageBase: ?*anyopaque, // void** out
cb: u32, // DWORD
lpcbNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;proc K32EnumDeviceDrivers(
lpImageBase: pointer, # void** out
cb: uint32, # DWORD
lpcbNeeded: ptr uint32 # DWORD* out
): int32 {.importc: "K32EnumDeviceDrivers", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int K32EnumDeviceDrivers(
void** lpImageBase, // void** out
uint cb, // DWORD
uint* lpcbNeeded // DWORD* out
);ccall((:K32EnumDeviceDrivers, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32, Ptr{UInt32}),
lpImageBase, cb, lpcbNeeded)
# lpImageBase : void** out -> Ptr{Cvoid}
# cb : DWORD -> UInt32
# lpcbNeeded : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t K32EnumDeviceDrivers(
void** lpImageBase,
uint32_t cb,
uint32_t* lpcbNeeded);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)
-- lpImageBase : void** out
-- cb : DWORD
-- lpcbNeeded : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const K32EnumDeviceDrivers = lib.func('__stdcall', 'K32EnumDeviceDrivers', 'int32_t', ['void *', 'uint32_t', 'uint32_t *']);
// K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)
// lpImageBase : void** out -> 'void *'
// cb : DWORD -> 'uint32_t'
// lpcbNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
K32EnumDeviceDrivers: { parameters: ["pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)
// lpImageBase : void** out -> "pointer"
// cb : DWORD -> "u32"
// lpcbNeeded : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t K32EnumDeviceDrivers(
void** lpImageBase,
uint32_t cb,
uint32_t* lpcbNeeded);
C, "KERNEL32.dll");
// $ffi->K32EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded);
// lpImageBase : void** out
// cb : DWORD
// lpcbNeeded : DWORD* out
// 構造体/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 K32EnumDeviceDrivers(
Pointer lpImageBase, // void** out
int cb, // DWORD
IntByReference lpcbNeeded // DWORD* out
);
}@[Link("kernel32")]
lib LibKERNEL32
fun K32EnumDeviceDrivers = K32EnumDeviceDrivers(
lpImageBase : Void**, # void** out
cb : UInt32, # DWORD
lpcbNeeded : UInt32* # DWORD* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef K32EnumDeviceDriversNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Uint32>);
typedef K32EnumDeviceDriversDart = int Function(Pointer<Void>, int, Pointer<Uint32>);
final K32EnumDeviceDrivers = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<K32EnumDeviceDriversNative, K32EnumDeviceDriversDart>('K32EnumDeviceDrivers');
// lpImageBase : void** out -> Pointer<Void>
// cb : DWORD -> Uint32
// lpcbNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function K32EnumDeviceDrivers(
lpImageBase: Pointer; // void** out
cb: DWORD; // DWORD
lpcbNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'K32EnumDeviceDrivers';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "K32EnumDeviceDrivers"
c_K32EnumDeviceDrivers :: Ptr () -> Word32 -> Ptr Word32 -> IO CInt
-- lpImageBase : void** out -> Ptr ()
-- cb : DWORD -> Word32
-- lpcbNeeded : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let k32enumdevicedrivers =
foreign "K32EnumDeviceDrivers"
((ptr void) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* lpImageBase : void** out -> (ptr void) *)
(* cb : DWORD -> uint32_t *)
(* lpcbNeeded : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("K32EnumDeviceDrivers" k32-enum-device-drivers :convention :stdcall) :int32
(lp-image-base :pointer) ; void** out
(cb :uint32) ; DWORD
(lpcb-needed :pointer)) ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $K32EnumDeviceDrivers = Win32::API::More->new('KERNEL32',
'BOOL K32EnumDeviceDrivers(LPVOID lpImageBase, DWORD cb, LPVOID lpcbNeeded)');
# my $ret = $K32EnumDeviceDrivers->Call($lpImageBase, $cb, $lpcbNeeded);
# lpImageBase : void** out -> LPVOID
# cb : DWORD -> DWORD
# lpcbNeeded : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f GetDeviceDriverBaseNameW — デバイスドライバの基本名をUnicode文字列で取得する。
- f GetDeviceDriverFileNameW — デバイスドライバのファイルパスをUnicodeで取得する。