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

SetupDiOpenDeviceInfoW

関数
インスタンスIDで既存デバイスを開き情報セットに追加する。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetupDiOpenDeviceInfoW(
    HDEVINFO DeviceInfoSet,
    LPCWSTR DeviceInstanceId,
    HWND hwndParent,   // optional
    DWORD OpenFlags,
    SP_DEVINFO_DATA* DeviceInfoData   // optional
);

パラメーター

名前方向
DeviceInfoSetHDEVINFOin
DeviceInstanceIdLPCWSTRin
hwndParentHWNDinoptional
OpenFlagsDWORDin
DeviceInfoDataSP_DEVINFO_DATA*inoutoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

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

SetupDiOpenDeviceInfoW = ctypes.windll.setupapi.SetupDiOpenDeviceInfoW
SetupDiOpenDeviceInfoW.restype = wintypes.BOOL
SetupDiOpenDeviceInfoW.argtypes = [
    ctypes.c_ssize_t,  # DeviceInfoSet : HDEVINFO
    wintypes.LPCWSTR,  # DeviceInstanceId : LPCWSTR
    wintypes.HANDLE,  # hwndParent : HWND optional
    wintypes.DWORD,  # OpenFlags : DWORD
    ctypes.c_void_p,  # DeviceInfoData : SP_DEVINFO_DATA* optional, in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SETUPAPI.dll')
SetupDiOpenDeviceInfoW = Fiddle::Function.new(
  lib['SetupDiOpenDeviceInfoW'],
  [
    Fiddle::TYPE_INTPTR_T,  # DeviceInfoSet : HDEVINFO
    Fiddle::TYPE_VOIDP,  # DeviceInstanceId : LPCWSTR
    Fiddle::TYPE_VOIDP,  # hwndParent : HWND optional
    -Fiddle::TYPE_INT,  # OpenFlags : DWORD
    Fiddle::TYPE_VOIDP,  # DeviceInfoData : SP_DEVINFO_DATA* optional, in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "setupapi")]
extern "system" {
    fn SetupDiOpenDeviceInfoW(
        DeviceInfoSet: isize,  // HDEVINFO
        DeviceInstanceId: *const u16,  // LPCWSTR
        hwndParent: *mut core::ffi::c_void,  // HWND optional
        OpenFlags: u32,  // DWORD
        DeviceInfoData: *mut SP_DEVINFO_DATA  // SP_DEVINFO_DATA* optional, in/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 SetupDiOpenDeviceInfoW(IntPtr DeviceInfoSet, [MarshalAs(UnmanagedType.LPWStr)] string DeviceInstanceId, IntPtr hwndParent, uint OpenFlags, IntPtr DeviceInfoData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupDiOpenDeviceInfoW' -Namespace Win32 -PassThru
# $api::SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
#uselib "SETUPAPI.dll"
#func global SetupDiOpenDeviceInfoW "SetupDiOpenDeviceInfoW" wptr, wptr, wptr, wptr, wptr
; SetupDiOpenDeviceInfoW DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, varptr(DeviceInfoData)   ; 戻り値は stat
; DeviceInfoSet : HDEVINFO -> "wptr"
; DeviceInstanceId : LPCWSTR -> "wptr"
; hwndParent : HWND optional -> "wptr"
; OpenFlags : DWORD -> "wptr"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupDiOpenDeviceInfoW "SetupDiOpenDeviceInfoW" sptr, wstr, sptr, int, var
; res = SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
; DeviceInfoSet : HDEVINFO -> "sptr"
; DeviceInstanceId : LPCWSTR -> "wstr"
; hwndParent : HWND optional -> "sptr"
; OpenFlags : DWORD -> "int"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetupDiOpenDeviceInfoW(HDEVINFO DeviceInfoSet, LPCWSTR DeviceInstanceId, HWND hwndParent, DWORD OpenFlags, SP_DEVINFO_DATA* DeviceInfoData)
#uselib "SETUPAPI.dll"
#cfunc global SetupDiOpenDeviceInfoW "SetupDiOpenDeviceInfoW" intptr, wstr, intptr, int, var
; res = SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
; DeviceInfoSet : HDEVINFO -> "intptr"
; DeviceInstanceId : LPCWSTR -> "wstr"
; hwndParent : HWND optional -> "intptr"
; OpenFlags : DWORD -> "int"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupDiOpenDeviceInfoW = setupapi.NewProc("SetupDiOpenDeviceInfoW")
)

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