Win32 API 日本語リファレンス
ホームDevices.DeviceAndDriverInstallation › SetupDiGetCustomDevicePropertyW

SetupDiGetCustomDevicePropertyW

関数
デバイスの指定したカスタムプロパティを取得する。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL SetupDiGetCustomDevicePropertyW(
    HDEVINFO DeviceInfoSet,
    SP_DEVINFO_DATA* DeviceInfoData,
    LPCWSTR CustomPropertyName,
    DWORD Flags,
    DWORD* PropertyRegDataType,   // optional
    BYTE* PropertyBuffer,
    DWORD PropertyBufferSize,
    DWORD* RequiredSize   // optional
);

パラメーター

名前方向説明
DeviceInfoSetHDEVINFOin対象デバイスを含むデバイス情報セットのハンドルを指定する。SetupDiGetClassDevs等で取得する。
DeviceInfoDataSP_DEVINFO_DATA*in情報セット内の特定デバイスを示すSP_DEVINFO_DATA構造体へのポインターを指定する。
CustomPropertyNameLPCWSTRin取得するカスタムプロパティ名を表すNULL終端のワイド文字列を指定する。レジストリのHardwareID配下の値名に相当する。
FlagsDWORDin取得方法を制御するフラグ。ハードウェアキー/デバイスキーの選択(DICUSTOMDEVPROP_MERGE_MULTISZ等)を指定する。
PropertyRegDataTypeDWORD*outoptional取得した値のレジストリデータ型(REG_SZ等)を受け取るDWORDへのポインター。不要ならNULL可。
PropertyBufferBYTE*outプロパティ値を受け取るバッファーへのポインター。サイズ問い合わせ時はNULL可。
PropertyBufferSizeDWORDinPropertyBufferのサイズをバイト単位で指定する。
RequiredSizeDWORD*outoptional実際に必要なバッファーサイズをバイト単位で受け取るDWORDへのポインター。不要ならNULL可。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetupDiGetCustomDevicePropertyW(
    HDEVINFO DeviceInfoSet,
    SP_DEVINFO_DATA* DeviceInfoData,
    LPCWSTR CustomPropertyName,
    DWORD Flags,
    DWORD* PropertyRegDataType,   // optional
    BYTE* PropertyBuffer,
    DWORD PropertyBufferSize,
    DWORD* RequiredSize   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetupDiGetCustomDevicePropertyW(
    IntPtr DeviceInfoSet,   // HDEVINFO
    IntPtr DeviceInfoData,   // SP_DEVINFO_DATA*
    [MarshalAs(UnmanagedType.LPWStr)] string CustomPropertyName,   // LPCWSTR
    uint Flags,   // DWORD
    IntPtr PropertyRegDataType,   // DWORD* optional, out
    IntPtr PropertyBuffer,   // BYTE* out
    uint PropertyBufferSize,   // DWORD
    IntPtr RequiredSize   // DWORD* optional, out
);
<DllImport("SETUPAPI.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetupDiGetCustomDevicePropertyW(
    DeviceInfoSet As IntPtr,   ' HDEVINFO
    DeviceInfoData As IntPtr,   ' SP_DEVINFO_DATA*
    <MarshalAs(UnmanagedType.LPWStr)> CustomPropertyName As String,   ' LPCWSTR
    Flags As UInteger,   ' DWORD
    PropertyRegDataType As IntPtr,   ' DWORD* optional, out
    PropertyBuffer As IntPtr,   ' BYTE* out
    PropertyBufferSize As UInteger,   ' DWORD
    RequiredSize As IntPtr   ' DWORD* optional, out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' DeviceInfoSet : HDEVINFO
' DeviceInfoData : SP_DEVINFO_DATA*
' CustomPropertyName : LPCWSTR
' Flags : DWORD
' PropertyRegDataType : DWORD* optional, out
' PropertyBuffer : BYTE* out
' PropertyBufferSize : DWORD
' RequiredSize : DWORD* optional, out
Declare PtrSafe Function SetupDiGetCustomDevicePropertyW Lib "setupapi" ( _
    ByVal DeviceInfoSet As LongPtr, _
    ByVal DeviceInfoData As LongPtr, _
    ByVal CustomPropertyName As LongPtr, _
    ByVal Flags As Long, _
    ByVal PropertyRegDataType As LongPtr, _
    ByVal PropertyBuffer As LongPtr, _
    ByVal PropertyBufferSize As Long, _
    ByVal RequiredSize As LongPtr) 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

SetupDiGetCustomDevicePropertyW = ctypes.windll.setupapi.SetupDiGetCustomDevicePropertyW
SetupDiGetCustomDevicePropertyW.restype = wintypes.BOOL
SetupDiGetCustomDevicePropertyW.argtypes = [
    ctypes.c_ssize_t,  # DeviceInfoSet : HDEVINFO
    ctypes.c_void_p,  # DeviceInfoData : SP_DEVINFO_DATA*
    wintypes.LPCWSTR,  # CustomPropertyName : LPCWSTR
    wintypes.DWORD,  # Flags : DWORD
    ctypes.POINTER(wintypes.DWORD),  # PropertyRegDataType : DWORD* optional, out
    ctypes.POINTER(ctypes.c_ubyte),  # PropertyBuffer : BYTE* out
    wintypes.DWORD,  # PropertyBufferSize : DWORD
    ctypes.POINTER(wintypes.DWORD),  # RequiredSize : DWORD* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SETUPAPI.dll')
SetupDiGetCustomDevicePropertyW = Fiddle::Function.new(
  lib['SetupDiGetCustomDevicePropertyW'],
  [
    Fiddle::TYPE_INTPTR_T,  # DeviceInfoSet : HDEVINFO
    Fiddle::TYPE_VOIDP,  # DeviceInfoData : SP_DEVINFO_DATA*
    Fiddle::TYPE_VOIDP,  # CustomPropertyName : LPCWSTR
    -Fiddle::TYPE_INT,  # Flags : DWORD
    Fiddle::TYPE_VOIDP,  # PropertyRegDataType : DWORD* optional, out
    Fiddle::TYPE_VOIDP,  # PropertyBuffer : BYTE* out
    -Fiddle::TYPE_INT,  # PropertyBufferSize : DWORD
    Fiddle::TYPE_VOIDP,  # RequiredSize : DWORD* optional, out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "setupapi")]
extern "system" {
    fn SetupDiGetCustomDevicePropertyW(
        DeviceInfoSet: isize,  // HDEVINFO
        DeviceInfoData: *mut SP_DEVINFO_DATA,  // SP_DEVINFO_DATA*
        CustomPropertyName: *const u16,  // LPCWSTR
        Flags: u32,  // DWORD
        PropertyRegDataType: *mut u32,  // DWORD* optional, out
        PropertyBuffer: *mut u8,  // BYTE* out
        PropertyBufferSize: u32,  // DWORD
        RequiredSize: *mut u32  // DWORD* optional, out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupDiGetCustomDevicePropertyW(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, [MarshalAs(UnmanagedType.LPWStr)] string CustomPropertyName, uint Flags, IntPtr PropertyRegDataType, IntPtr PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupDiGetCustomDevicePropertyW' -Namespace Win32 -PassThru
# $api::SetupDiGetCustomDevicePropertyW(DeviceInfoSet, DeviceInfoData, CustomPropertyName, Flags, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize)
#uselib "SETUPAPI.dll"
#func global SetupDiGetCustomDevicePropertyW "SetupDiGetCustomDevicePropertyW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; SetupDiGetCustomDevicePropertyW DeviceInfoSet, varptr(DeviceInfoData), CustomPropertyName, Flags, varptr(PropertyRegDataType), varptr(PropertyBuffer), PropertyBufferSize, varptr(RequiredSize)   ; 戻り値は stat
; DeviceInfoSet : HDEVINFO -> "wptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "wptr"
; CustomPropertyName : LPCWSTR -> "wptr"
; Flags : DWORD -> "wptr"
; PropertyRegDataType : DWORD* optional, out -> "wptr"
; PropertyBuffer : BYTE* out -> "wptr"
; PropertyBufferSize : DWORD -> "wptr"
; RequiredSize : DWORD* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupDiGetCustomDevicePropertyW "SetupDiGetCustomDevicePropertyW" sptr, var, wstr, int, var, var, int, var
; res = SetupDiGetCustomDevicePropertyW(DeviceInfoSet, DeviceInfoData, CustomPropertyName, Flags, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize)
; DeviceInfoSet : HDEVINFO -> "sptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "var"
; CustomPropertyName : LPCWSTR -> "wstr"
; Flags : DWORD -> "int"
; PropertyRegDataType : DWORD* optional, out -> "var"
; PropertyBuffer : BYTE* out -> "var"
; PropertyBufferSize : DWORD -> "int"
; RequiredSize : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetupDiGetCustomDevicePropertyW(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA* DeviceInfoData, LPCWSTR CustomPropertyName, DWORD Flags, DWORD* PropertyRegDataType, BYTE* PropertyBuffer, DWORD PropertyBufferSize, DWORD* RequiredSize)
#uselib "SETUPAPI.dll"
#cfunc global SetupDiGetCustomDevicePropertyW "SetupDiGetCustomDevicePropertyW" intptr, var, wstr, int, var, var, int, var
; res = SetupDiGetCustomDevicePropertyW(DeviceInfoSet, DeviceInfoData, CustomPropertyName, Flags, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize)
; DeviceInfoSet : HDEVINFO -> "intptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "var"
; CustomPropertyName : LPCWSTR -> "wstr"
; Flags : DWORD -> "int"
; PropertyRegDataType : DWORD* optional, out -> "var"
; PropertyBuffer : BYTE* out -> "var"
; PropertyBufferSize : DWORD -> "int"
; RequiredSize : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupDiGetCustomDevicePropertyW = setupapi.NewProc("SetupDiGetCustomDevicePropertyW")
)

// DeviceInfoSet (HDEVINFO), DeviceInfoData (SP_DEVINFO_DATA*), CustomPropertyName (LPCWSTR), Flags (DWORD), PropertyRegDataType (DWORD* optional, out), PropertyBuffer (BYTE* out), PropertyBufferSize (DWORD), RequiredSize (DWORD* optional, out)
r1, _, err := procSetupDiGetCustomDevicePropertyW.Call(
	uintptr(DeviceInfoSet),
	uintptr(DeviceInfoData),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(CustomPropertyName))),
	uintptr(Flags),
	uintptr(PropertyRegDataType),
	uintptr(PropertyBuffer),
	uintptr(PropertyBufferSize),
	uintptr(RequiredSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetupDiGetCustomDevicePropertyW(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceInfoData: Pointer;   // SP_DEVINFO_DATA*
  CustomPropertyName: PWideChar;   // LPCWSTR
  Flags: DWORD;   // DWORD
  PropertyRegDataType: Pointer;   // DWORD* optional, out
  PropertyBuffer: Pointer;   // BYTE* out
  PropertyBufferSize: DWORD;   // DWORD
  RequiredSize: Pointer   // DWORD* optional, out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiGetCustomDevicePropertyW';
result := DllCall("SETUPAPI\SetupDiGetCustomDevicePropertyW"
    , "Ptr", DeviceInfoSet   ; HDEVINFO
    , "Ptr", DeviceInfoData   ; SP_DEVINFO_DATA*
    , "WStr", CustomPropertyName   ; LPCWSTR
    , "UInt", Flags   ; DWORD
    , "Ptr", PropertyRegDataType   ; DWORD* optional, out
    , "Ptr", PropertyBuffer   ; BYTE* out
    , "UInt", PropertyBufferSize   ; DWORD
    , "Ptr", RequiredSize   ; DWORD* optional, out
    , "Int")   ; return: BOOL
●SetupDiGetCustomDevicePropertyW(DeviceInfoSet, DeviceInfoData, CustomPropertyName, Flags, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize) = DLL("SETUPAPI.dll", "bool SetupDiGetCustomDevicePropertyW(int, void*, char*, dword, void*, void*, dword, void*)")
# 呼び出し: SetupDiGetCustomDevicePropertyW(DeviceInfoSet, DeviceInfoData, CustomPropertyName, Flags, PropertyRegDataType, PropertyBuffer, PropertyBufferSize, RequiredSize)
# DeviceInfoSet : HDEVINFO -> "int"
# DeviceInfoData : SP_DEVINFO_DATA* -> "void*"
# CustomPropertyName : LPCWSTR -> "char*"
# Flags : DWORD -> "dword"
# PropertyRegDataType : DWORD* optional, out -> "void*"
# PropertyBuffer : BYTE* out -> "void*"
# PropertyBufferSize : DWORD -> "dword"
# RequiredSize : DWORD* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef SetupDiGetCustomDevicePropertyWNative = Int32 Function(IntPtr, Pointer<Void>, Pointer<Utf16>, Uint32, Pointer<Uint32>, Pointer<Uint8>, Uint32, Pointer<Uint32>);
typedef SetupDiGetCustomDevicePropertyWDart = int Function(int, Pointer<Void>, Pointer<Utf16>, int, Pointer<Uint32>, Pointer<Uint8>, int, Pointer<Uint32>);
final SetupDiGetCustomDevicePropertyW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupDiGetCustomDevicePropertyWNative, SetupDiGetCustomDevicePropertyWDart>('SetupDiGetCustomDevicePropertyW');
// DeviceInfoSet : HDEVINFO -> IntPtr
// DeviceInfoData : SP_DEVINFO_DATA* -> Pointer<Void>
// CustomPropertyName : LPCWSTR -> Pointer<Utf16>
// Flags : DWORD -> Uint32
// PropertyRegDataType : DWORD* optional, out -> Pointer<Uint32>
// PropertyBuffer : BYTE* out -> Pointer<Uint8>
// PropertyBufferSize : DWORD -> Uint32
// RequiredSize : DWORD* optional, out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupDiGetCustomDevicePropertyW(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceInfoData: Pointer;   // SP_DEVINFO_DATA*
  CustomPropertyName: PWideChar;   // LPCWSTR
  Flags: DWORD;   // DWORD
  PropertyRegDataType: Pointer;   // DWORD* optional, out
  PropertyBuffer: Pointer;   // BYTE* out
  PropertyBufferSize: DWORD;   // DWORD
  RequiredSize: Pointer   // DWORD* optional, out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiGetCustomDevicePropertyW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupDiGetCustomDevicePropertyW"
  c_SetupDiGetCustomDevicePropertyW :: CIntPtr -> Ptr () -> CWString -> Word32 -> Ptr Word32 -> Ptr Word8 -> Word32 -> Ptr Word32 -> IO CInt
-- DeviceInfoSet : HDEVINFO -> CIntPtr
-- DeviceInfoData : SP_DEVINFO_DATA* -> Ptr ()
-- CustomPropertyName : LPCWSTR -> CWString
-- Flags : DWORD -> Word32
-- PropertyRegDataType : DWORD* optional, out -> Ptr Word32
-- PropertyBuffer : BYTE* out -> Ptr Word8
-- PropertyBufferSize : DWORD -> Word32
-- RequiredSize : DWORD* optional, out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setupdigetcustomdevicepropertyw =
  foreign "SetupDiGetCustomDevicePropertyW"
    (intptr_t @-> (ptr void) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint32_t) @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* DeviceInfoSet : HDEVINFO -> intptr_t *)
(* DeviceInfoData : SP_DEVINFO_DATA* -> (ptr void) *)
(* CustomPropertyName : LPCWSTR -> (ptr uint16_t) *)
(* Flags : DWORD -> uint32_t *)
(* PropertyRegDataType : DWORD* optional, out -> (ptr uint32_t) *)
(* PropertyBuffer : BYTE* out -> (ptr uint8_t) *)
(* PropertyBufferSize : DWORD -> uint32_t *)
(* RequiredSize : DWORD* optional, out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupDiGetCustomDevicePropertyW" setup-di-get-custom-device-property-w :convention :stdcall) :int32
  (device-info-set :int64)   ; HDEVINFO
  (device-info-data :pointer)   ; SP_DEVINFO_DATA*
  (custom-property-name (:string :encoding :utf-16le))   ; LPCWSTR
  (flags :uint32)   ; DWORD
  (property-reg-data-type :pointer)   ; DWORD* optional, out
  (property-buffer :pointer)   ; BYTE* out
  (property-buffer-size :uint32)   ; DWORD
  (required-size :pointer))   ; DWORD* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupDiGetCustomDevicePropertyW = Win32::API::More->new('SETUPAPI',
    'BOOL SetupDiGetCustomDevicePropertyW(LPARAM DeviceInfoSet, LPVOID DeviceInfoData, LPCWSTR CustomPropertyName, DWORD Flags, LPVOID PropertyRegDataType, LPVOID PropertyBuffer, DWORD PropertyBufferSize, LPVOID RequiredSize)');
# my $ret = $SetupDiGetCustomDevicePropertyW->Call($DeviceInfoSet, $DeviceInfoData, $CustomPropertyName, $Flags, $PropertyRegDataType, $PropertyBuffer, $PropertyBufferSize, $RequiredSize);
# DeviceInfoSet : HDEVINFO -> LPARAM
# DeviceInfoData : SP_DEVINFO_DATA* -> LPVOID
# CustomPropertyName : LPCWSTR -> LPCWSTR
# Flags : DWORD -> DWORD
# PropertyRegDataType : DWORD* optional, out -> LPVOID
# PropertyBuffer : BYTE* out -> LPVOID
# PropertyBufferSize : DWORD -> DWORD
# RequiredSize : DWORD* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型