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

GetDeviceDriverFileNameW

関数
デバイスドライバのファイルパスをUnicodeで取得する。
DLLPSAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// PSAPI.dll  (Unicode / -W)
#include <windows.h>

DWORD GetDeviceDriverFileNameW(
    void* ImageBase,
    LPWSTR lpFilename,
    DWORD nSize
);

パラメーター

名前方向説明
ImageBasevoid*inデバイスドライバーのロードアドレス。
lpFilenameLPWSTRoutデバイスドライバーへのパスを受け取るバッファーへのポインター。
nSizeDWORDinlpFilename バッファーのサイズ(文字数)。パスと終端の null 文字を格納できるだけの大きさがバッファーにない場合、文字列は切り詰められます。

戻り値の型: DWORD

公式ドキュメント

指定したデバイスドライバーで利用可能なパスを取得します。(Unicode)

戻り値

関数が成功した場合、戻り値はバッファーにコピーされた文字列の長さ(終端の null 文字を含まない)を表します。

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

解説(Remarks)

Windows 7 および Windows Server 2008 R2 以降、Psapi.h は PSAPI 関数のバージョン番号を定めています。PSAPI のバージョン番号は、関数を呼び出す際に使用する名前と、プログラムがロードしなければならないライブラリに影響します。

PSAPI_VERSION が 2 以上の場合、この関数は Psapi.h で K32GetDeviceDriverFileName として定義され、Kernel32.lib および Kernel32.dll でエクスポートされます。PSAPI_VERSION が 1 の場合、この関数は Psapi.h で GetDeviceDriverFileName として定義され、K32GetDeviceDriverFileName を呼び出すラッパーとして Psapi.lib および Psapi.dll でエクスポートされます。

以前のバージョンの Windows と Windows 7 以降の両方で実行する必要があるプログラムは、常にこの関数を GetDeviceDriverFileName として呼び出してください。シンボルが正しく解決されるようにするには、TARGETLIBS マクロに Psapi.lib を追加し、–DPSAPI_VERSION=1 を指定してプログラムをコンパイルします。実行時の動的リンクを使用するには、Psapi.dll をロードします。

メモ

psapi.h ヘッダーは、GetDeviceDriverFileName を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用と、エンコーディング非依存でないコードを混在させると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

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

各言語での呼び出し定義

// PSAPI.dll  (Unicode / -W)
#include <windows.h>

DWORD GetDeviceDriverFileNameW(
    void* ImageBase,
    LPWSTR lpFilename,
    DWORD nSize
);
[DllImport("PSAPI.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetDeviceDriverFileNameW(
    IntPtr ImageBase,   // void*
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpFilename,   // LPWSTR out
    uint nSize   // DWORD
);
<DllImport("PSAPI.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetDeviceDriverFileNameW(
    ImageBase As IntPtr,   ' void*
    <MarshalAs(UnmanagedType.LPWStr)> lpFilename As System.Text.StringBuilder,   ' LPWSTR out
    nSize As UInteger   ' DWORD
) As UInteger
End Function
' ImageBase : void*
' lpFilename : LPWSTR out
' nSize : DWORD
Declare PtrSafe Function GetDeviceDriverFileNameW Lib "psapi" ( _
    ByVal ImageBase As LongPtr, _
    ByVal lpFilename As LongPtr, _
    ByVal nSize As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetDeviceDriverFileNameW = ctypes.windll.psapi.GetDeviceDriverFileNameW
GetDeviceDriverFileNameW.restype = wintypes.DWORD
GetDeviceDriverFileNameW.argtypes = [
    ctypes.POINTER(None),  # ImageBase : void*
    wintypes.LPWSTR,  # lpFilename : LPWSTR out
    wintypes.DWORD,  # nSize : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('PSAPI.dll')
GetDeviceDriverFileNameW = Fiddle::Function.new(
  lib['GetDeviceDriverFileNameW'],
  [
    Fiddle::TYPE_VOIDP,  # ImageBase : void*
    Fiddle::TYPE_VOIDP,  # lpFilename : LPWSTR out
    -Fiddle::TYPE_INT,  # nSize : DWORD
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "psapi")]
extern "system" {
    fn GetDeviceDriverFileNameW(
        ImageBase: *mut (),  // void*
        lpFilename: *mut u16,  // LPWSTR out
        nSize: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("PSAPI.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetDeviceDriverFileNameW(IntPtr ImageBase, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpFilename, uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'PSAPI_GetDeviceDriverFileNameW' -Namespace Win32 -PassThru
# $api::GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
#uselib "PSAPI.dll"
#func global GetDeviceDriverFileNameW "GetDeviceDriverFileNameW" wptr, wptr, wptr
; GetDeviceDriverFileNameW ImageBase, varptr(lpFilename), nSize   ; 戻り値は stat
; ImageBase : void* -> "wptr"
; lpFilename : LPWSTR out -> "wptr"
; nSize : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "PSAPI.dll"
#cfunc global GetDeviceDriverFileNameW "GetDeviceDriverFileNameW" sptr, var, int
; res = GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
; ImageBase : void* -> "sptr"
; lpFilename : LPWSTR out -> "var"
; nSize : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD GetDeviceDriverFileNameW(void* ImageBase, LPWSTR lpFilename, DWORD nSize)
#uselib "PSAPI.dll"
#cfunc global GetDeviceDriverFileNameW "GetDeviceDriverFileNameW" intptr, var, int
; res = GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
; ImageBase : void* -> "intptr"
; lpFilename : LPWSTR out -> "var"
; nSize : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	psapi = windows.NewLazySystemDLL("PSAPI.dll")
	procGetDeviceDriverFileNameW = psapi.NewProc("GetDeviceDriverFileNameW")
)

// ImageBase (void*), lpFilename (LPWSTR out), nSize (DWORD)
r1, _, err := procGetDeviceDriverFileNameW.Call(
	uintptr(ImageBase),
	uintptr(lpFilename),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetDeviceDriverFileNameW(
  ImageBase: Pointer;   // void*
  lpFilename: PWideChar;   // LPWSTR out
  nSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'PSAPI.dll' name 'GetDeviceDriverFileNameW';
result := DllCall("PSAPI\GetDeviceDriverFileNameW"
    , "Ptr", ImageBase   ; void*
    , "Ptr", lpFilename   ; LPWSTR out
    , "UInt", nSize   ; DWORD
    , "UInt")   ; return: DWORD
●GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize) = DLL("PSAPI.dll", "dword GetDeviceDriverFileNameW(void*, char*, dword)")
# 呼び出し: GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
# ImageBase : void* -> "void*"
# lpFilename : LPWSTR out -> "char*"
# nSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "psapi" fn GetDeviceDriverFileNameW(
    ImageBase: ?*anyopaque, // void*
    lpFilename: [*c]u16, // LPWSTR out
    nSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetDeviceDriverFileNameW(
    ImageBase: pointer,  # void*
    lpFilename: ptr uint16,  # LPWSTR out
    nSize: uint32  # DWORD
): uint32 {.importc: "GetDeviceDriverFileNameW", stdcall, dynlib: "PSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "psapi");
extern(Windows)
uint GetDeviceDriverFileNameW(
    void* ImageBase,   // void*
    wchar* lpFilename,   // LPWSTR out
    uint nSize   // DWORD
);
ccall((:GetDeviceDriverFileNameW, "PSAPI.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Ptr{UInt16}, UInt32),
      ImageBase, lpFilename, nSize)
# ImageBase : void* -> Ptr{Cvoid}
# lpFilename : LPWSTR out -> Ptr{UInt16}
# nSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetDeviceDriverFileNameW(
    void* ImageBase,
    uint16_t* lpFilename,
    uint32_t nSize);
]]
local psapi = ffi.load("psapi")
-- psapi.GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
-- ImageBase : void*
-- lpFilename : LPWSTR out
-- nSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('PSAPI.dll');
const GetDeviceDriverFileNameW = lib.func('__stdcall', 'GetDeviceDriverFileNameW', 'uint32_t', ['void *', 'uint16_t *', 'uint32_t']);
// GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
// ImageBase : void* -> 'void *'
// lpFilename : LPWSTR out -> 'uint16_t *'
// nSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("PSAPI.dll", {
  GetDeviceDriverFileNameW: { parameters: ["pointer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize)
// ImageBase : void* -> "pointer"
// lpFilename : LPWSTR out -> "buffer"
// nSize : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetDeviceDriverFileNameW(
    void* ImageBase,
    uint16_t* lpFilename,
    uint32_t nSize);
C, "PSAPI.dll");
// $ffi->GetDeviceDriverFileNameW(ImageBase, lpFilename, nSize);
// ImageBase : void*
// lpFilename : LPWSTR out
// nSize : 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 Psapi extends StdCallLibrary {
    Psapi INSTANCE = Native.load("psapi", Psapi.class, W32APIOptions.UNICODE_OPTIONS);
    int GetDeviceDriverFileNameW(
        Pointer ImageBase,   // void*
        char[] lpFilename,   // LPWSTR out
        int nSize   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("psapi")]
lib LibPSAPI
  fun GetDeviceDriverFileNameW = GetDeviceDriverFileNameW(
    ImageBase : Void*,   # void*
    lpFilename : UInt16*,   # LPWSTR out
    nSize : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetDeviceDriverFileNameWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef GetDeviceDriverFileNameWDart = int Function(Pointer<Void>, Pointer<Utf16>, int);
final GetDeviceDriverFileNameW = DynamicLibrary.open('PSAPI.dll')
    .lookupFunction<GetDeviceDriverFileNameWNative, GetDeviceDriverFileNameWDart>('GetDeviceDriverFileNameW');
// ImageBase : void* -> Pointer<Void>
// lpFilename : LPWSTR out -> Pointer<Utf16>
// nSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetDeviceDriverFileNameW(
  ImageBase: Pointer;   // void*
  lpFilename: PWideChar;   // LPWSTR out
  nSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'PSAPI.dll' name 'GetDeviceDriverFileNameW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetDeviceDriverFileNameW"
  c_GetDeviceDriverFileNameW :: Ptr () -> CWString -> Word32 -> IO Word32
-- ImageBase : void* -> Ptr ()
-- lpFilename : LPWSTR out -> CWString
-- nSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getdevicedriverfilenamew =
  foreign "GetDeviceDriverFileNameW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* ImageBase : void* -> (ptr void) *)
(* lpFilename : LPWSTR out -> (ptr uint16_t) *)
(* nSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library psapi (t "PSAPI.dll"))
(cffi:use-foreign-library psapi)

(cffi:defcfun ("GetDeviceDriverFileNameW" get-device-driver-file-name-w :convention :stdcall) :uint32
  (image-base :pointer)   ; void*
  (lp-filename :pointer)   ; LPWSTR out
  (n-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetDeviceDriverFileNameW = Win32::API::More->new('PSAPI',
    'DWORD GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename, DWORD nSize)');
# my $ret = $GetDeviceDriverFileNameW->Call($ImageBase, $lpFilename, $nSize);
# ImageBase : void* -> LPVOID
# lpFilename : LPWSTR out -> LPWSTR
# nSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目