EnumDeviceDrivers
関数シグネチャ
// PSAPI.dll
#include <windows.h>
BOOL EnumDeviceDrivers(
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 のアドレスが格納されます。
例
例については、Enumerating all Device Drivers in the System を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// PSAPI.dll
#include <windows.h>
BOOL EnumDeviceDrivers(
void** lpImageBase,
DWORD cb,
DWORD* lpcbNeeded
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("PSAPI.dll", SetLastError = true, ExactSpelling = true)]
static extern bool EnumDeviceDrivers(
IntPtr lpImageBase, // void** out
uint cb, // DWORD
out uint lpcbNeeded // DWORD* out
);<DllImport("PSAPI.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function EnumDeviceDrivers(
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 EnumDeviceDrivers Lib "psapi" ( _
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
EnumDeviceDrivers = ctypes.windll.psapi.EnumDeviceDrivers
EnumDeviceDrivers.restype = wintypes.BOOL
EnumDeviceDrivers.argtypes = [
ctypes.c_void_p, # lpImageBase : void** out
wintypes.DWORD, # cb : DWORD
ctypes.POINTER(wintypes.DWORD), # lpcbNeeded : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('PSAPI.dll')
EnumDeviceDrivers = Fiddle::Function.new(
lib['EnumDeviceDrivers'],
[
Fiddle::TYPE_VOIDP, # lpImageBase : void** out
-Fiddle::TYPE_INT, # cb : DWORD
Fiddle::TYPE_VOIDP, # lpcbNeeded : DWORD* out
],
Fiddle::TYPE_INT)#[link(name = "psapi")]
extern "system" {
fn EnumDeviceDrivers(
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("PSAPI.dll", SetLastError = true)]
public static extern bool EnumDeviceDrivers(IntPtr lpImageBase, uint cb, out uint lpcbNeeded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'PSAPI_EnumDeviceDrivers' -Namespace Win32 -PassThru
# $api::EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)#uselib "PSAPI.dll"
#func global EnumDeviceDrivers "EnumDeviceDrivers" sptr, sptr, sptr
; EnumDeviceDrivers lpImageBase, cb, varptr(lpcbNeeded) ; 戻り値は stat
; lpImageBase : void** out -> "sptr"
; cb : DWORD -> "sptr"
; lpcbNeeded : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "PSAPI.dll" #cfunc global EnumDeviceDrivers "EnumDeviceDrivers" sptr, int, var ; res = EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) ; lpImageBase : void** out -> "sptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "PSAPI.dll" #cfunc global EnumDeviceDrivers "EnumDeviceDrivers" sptr, int, sptr ; res = EnumDeviceDrivers(lpImageBase, cb, varptr(lpcbNeeded)) ; lpImageBase : void** out -> "sptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL EnumDeviceDrivers(void** lpImageBase, DWORD cb, DWORD* lpcbNeeded) #uselib "PSAPI.dll" #cfunc global EnumDeviceDrivers "EnumDeviceDrivers" intptr, int, var ; res = EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) ; lpImageBase : void** out -> "intptr" ; cb : DWORD -> "int" ; lpcbNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL EnumDeviceDrivers(void** lpImageBase, DWORD cb, DWORD* lpcbNeeded) #uselib "PSAPI.dll" #cfunc global EnumDeviceDrivers "EnumDeviceDrivers" intptr, int, intptr ; res = EnumDeviceDrivers(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 (
psapi = windows.NewLazySystemDLL("PSAPI.dll")
procEnumDeviceDrivers = psapi.NewProc("EnumDeviceDrivers")
)
// lpImageBase (void** out), cb (DWORD), lpcbNeeded (DWORD* out)
r1, _, err := procEnumDeviceDrivers.Call(
uintptr(lpImageBase),
uintptr(cb),
uintptr(lpcbNeeded),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction EnumDeviceDrivers(
lpImageBase: Pointer; // void** out
cb: DWORD; // DWORD
lpcbNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'PSAPI.dll' name 'EnumDeviceDrivers';result := DllCall("PSAPI\EnumDeviceDrivers"
, "Ptr", lpImageBase ; void** out
, "UInt", cb ; DWORD
, "Ptr", lpcbNeeded ; DWORD* out
, "Int") ; return: BOOL●EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded) = DLL("PSAPI.dll", "bool EnumDeviceDrivers(void*, dword, void*)")
# 呼び出し: EnumDeviceDrivers(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 "psapi" fn EnumDeviceDrivers(
lpImageBase: ?*anyopaque, // void** out
cb: u32, // DWORD
lpcbNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;proc EnumDeviceDrivers(
lpImageBase: pointer, # void** out
cb: uint32, # DWORD
lpcbNeeded: ptr uint32 # DWORD* out
): int32 {.importc: "EnumDeviceDrivers", stdcall, dynlib: "PSAPI.dll".}pragma(lib, "psapi");
extern(Windows)
int EnumDeviceDrivers(
void** lpImageBase, // void** out
uint cb, // DWORD
uint* lpcbNeeded // DWORD* out
);ccall((:EnumDeviceDrivers, "PSAPI.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 EnumDeviceDrivers(
void** lpImageBase,
uint32_t cb,
uint32_t* lpcbNeeded);
]]
local psapi = ffi.load("psapi")
-- psapi.EnumDeviceDrivers(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('PSAPI.dll');
const EnumDeviceDrivers = lib.func('__stdcall', 'EnumDeviceDrivers', 'int32_t', ['void *', 'uint32_t', 'uint32_t *']);
// EnumDeviceDrivers(lpImageBase, cb, lpcbNeeded)
// lpImageBase : void** out -> 'void *'
// cb : DWORD -> 'uint32_t'
// lpcbNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("PSAPI.dll", {
EnumDeviceDrivers: { parameters: ["pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.EnumDeviceDrivers(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 EnumDeviceDrivers(
void** lpImageBase,
uint32_t cb,
uint32_t* lpcbNeeded);
C, "PSAPI.dll");
// $ffi->EnumDeviceDrivers(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 Psapi extends StdCallLibrary {
Psapi INSTANCE = Native.load("psapi", Psapi.class);
boolean EnumDeviceDrivers(
Pointer lpImageBase, // void** out
int cb, // DWORD
IntByReference lpcbNeeded // DWORD* out
);
}@[Link("psapi")]
lib LibPSAPI
fun EnumDeviceDrivers = EnumDeviceDrivers(
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 EnumDeviceDriversNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Uint32>);
typedef EnumDeviceDriversDart = int Function(Pointer<Void>, int, Pointer<Uint32>);
final EnumDeviceDrivers = DynamicLibrary.open('PSAPI.dll')
.lookupFunction<EnumDeviceDriversNative, EnumDeviceDriversDart>('EnumDeviceDrivers');
// lpImageBase : void** out -> Pointer<Void>
// cb : DWORD -> Uint32
// lpcbNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function EnumDeviceDrivers(
lpImageBase: Pointer; // void** out
cb: DWORD; // DWORD
lpcbNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'PSAPI.dll' name 'EnumDeviceDrivers';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "EnumDeviceDrivers"
c_EnumDeviceDrivers :: 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 enumdevicedrivers =
foreign "EnumDeviceDrivers"
((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 psapi (t "PSAPI.dll"))
(cffi:use-foreign-library psapi)
(cffi:defcfun ("EnumDeviceDrivers" 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 $EnumDeviceDrivers = Win32::API::More->new('PSAPI',
'BOOL EnumDeviceDrivers(LPVOID lpImageBase, DWORD cb, LPVOID lpcbNeeded)');
# my $ret = $EnumDeviceDrivers->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で取得する。