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開く既存デバイスのインスタンスID(Unicode)。
hwndParentHWNDinoptionalUI表示時の親ウィンドウハンドル。NULL可。
OpenFlagsDWORDinDIOD_*系のオープン動作を制御するフラグ。
DeviceInfoDataSP_DEVINFO_DATA*inoutoptional開いた要素情報を受け取るSP_DEVINFO_DATAへのポインタ(出力)。NULL可。

戻り値の型: 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 <MarshalAs(UnmanagedType.Bool)> 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 版の利用を推奨。
const std = @import("std");

extern "setupapi" fn SetupDiOpenDeviceInfoW(
    DeviceInfoSet: isize, // HDEVINFO
    DeviceInstanceId: [*c]const u16, // LPCWSTR
    hwndParent: ?*anyopaque, // HWND optional
    OpenFlags: u32, // DWORD
    DeviceInfoData: [*c]SP_DEVINFO_DATA // SP_DEVINFO_DATA* optional, in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SetupDiOpenDeviceInfoW(
    DeviceInfoSet: int,  # HDEVINFO
    DeviceInstanceId: WideCString,  # LPCWSTR
    hwndParent: pointer,  # HWND optional
    OpenFlags: uint32,  # DWORD
    DeviceInfoData: ptr SP_DEVINFO_DATA  # SP_DEVINFO_DATA* optional, in/out
): int32 {.importc: "SetupDiOpenDeviceInfoW", stdcall, dynlib: "SETUPAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "setupapi");
extern(Windows)
int SetupDiOpenDeviceInfoW(
    ptrdiff_t DeviceInfoSet,   // HDEVINFO
    const(wchar)* DeviceInstanceId,   // LPCWSTR
    void* hwndParent,   // HWND optional
    uint OpenFlags,   // DWORD
    SP_DEVINFO_DATA* DeviceInfoData   // SP_DEVINFO_DATA* optional, in/out
);
ccall((:SetupDiOpenDeviceInfoW, "SETUPAPI.dll"), stdcall, Int32,
      (Int, Cwstring, Ptr{Cvoid}, UInt32, Ptr{SP_DEVINFO_DATA}),
      DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
# DeviceInfoSet : HDEVINFO -> Int
# DeviceInstanceId : LPCWSTR -> Cwstring
# hwndParent : HWND optional -> Ptr{Cvoid}
# OpenFlags : DWORD -> UInt32
# DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> Ptr{SP_DEVINFO_DATA}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetupDiOpenDeviceInfoW(
    intptr_t DeviceInfoSet,
    const uint16_t* DeviceInstanceId,
    void* hwndParent,
    uint32_t OpenFlags,
    void* DeviceInfoData);
]]
local setupapi = ffi.load("setupapi")
-- setupapi.SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
-- DeviceInfoSet : HDEVINFO
-- DeviceInstanceId : LPCWSTR
-- hwndParent : HWND optional
-- OpenFlags : DWORD
-- DeviceInfoData : SP_DEVINFO_DATA* optional, in/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 SetupDiOpenDeviceInfoW = lib.func('__stdcall', 'SetupDiOpenDeviceInfoW', 'int32_t', ['intptr_t', 'str16', 'void *', 'uint32_t', 'void *']);
// SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
// DeviceInfoSet : HDEVINFO -> 'intptr_t'
// DeviceInstanceId : LPCWSTR -> 'str16'
// hwndParent : HWND optional -> 'void *'
// OpenFlags : DWORD -> 'uint32_t'
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SETUPAPI.dll", {
  SetupDiOpenDeviceInfoW: { parameters: ["isize", "buffer", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData)
// DeviceInfoSet : HDEVINFO -> "isize"
// DeviceInstanceId : LPCWSTR -> "buffer"
// hwndParent : HWND optional -> "pointer"
// OpenFlags : DWORD -> "u32"
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetupDiOpenDeviceInfoW(
    intptr_t DeviceInfoSet,
    const uint16_t* DeviceInstanceId,
    void* hwndParent,
    uint32_t OpenFlags,
    void* DeviceInfoData);
C, "SETUPAPI.dll");
// $ffi->SetupDiOpenDeviceInfoW(DeviceInfoSet, DeviceInstanceId, hwndParent, OpenFlags, DeviceInfoData);
// DeviceInfoSet : HDEVINFO
// DeviceInstanceId : LPCWSTR
// hwndParent : HWND optional
// OpenFlags : DWORD
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/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 SetupDiOpenDeviceInfoW(
        long DeviceInfoSet,   // HDEVINFO
        WString DeviceInstanceId,   // LPCWSTR
        Pointer hwndParent,   // HWND optional
        int OpenFlags,   // DWORD
        Pointer DeviceInfoData   // SP_DEVINFO_DATA* optional, in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("setupapi")]
lib LibSETUPAPI
  fun SetupDiOpenDeviceInfoW = SetupDiOpenDeviceInfoW(
    DeviceInfoSet : LibC::SSizeT,   # HDEVINFO
    DeviceInstanceId : UInt16*,   # LPCWSTR
    hwndParent : Void*,   # HWND optional
    OpenFlags : UInt32,   # DWORD
    DeviceInfoData : SP_DEVINFO_DATA*   # SP_DEVINFO_DATA* optional, in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetupDiOpenDeviceInfoWNative = Int32 Function(IntPtr, Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Void>);
typedef SetupDiOpenDeviceInfoWDart = int Function(int, Pointer<Utf16>, Pointer<Void>, int, Pointer<Void>);
final SetupDiOpenDeviceInfoW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupDiOpenDeviceInfoWNative, SetupDiOpenDeviceInfoWDart>('SetupDiOpenDeviceInfoW');
// DeviceInfoSet : HDEVINFO -> IntPtr
// DeviceInstanceId : LPCWSTR -> Pointer<Utf16>
// hwndParent : HWND optional -> Pointer<Void>
// OpenFlags : DWORD -> Uint32
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
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';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupDiOpenDeviceInfoW"
  c_SetupDiOpenDeviceInfoW :: CIntPtr -> CWString -> Ptr () -> Word32 -> Ptr () -> IO CInt
-- DeviceInfoSet : HDEVINFO -> CIntPtr
-- DeviceInstanceId : LPCWSTR -> CWString
-- hwndParent : HWND optional -> Ptr ()
-- OpenFlags : DWORD -> Word32
-- DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setupdiopendeviceinfow =
  foreign "SetupDiOpenDeviceInfoW"
    (intptr_t @-> (ptr uint16_t) @-> (ptr void) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* DeviceInfoSet : HDEVINFO -> intptr_t *)
(* DeviceInstanceId : LPCWSTR -> (ptr uint16_t) *)
(* hwndParent : HWND optional -> (ptr void) *)
(* OpenFlags : DWORD -> uint32_t *)
(* DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupDiOpenDeviceInfoW" setup-di-open-device-info-w :convention :stdcall) :int32
  (device-info-set :int64)   ; HDEVINFO
  (device-instance-id (:string :encoding :utf-16le))   ; LPCWSTR
  (hwnd-parent :pointer)   ; HWND optional
  (open-flags :uint32)   ; DWORD
  (device-info-data :pointer))   ; SP_DEVINFO_DATA* optional, in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupDiOpenDeviceInfoW = Win32::API::More->new('SETUPAPI',
    'BOOL SetupDiOpenDeviceInfoW(LPARAM DeviceInfoSet, LPCWSTR DeviceInstanceId, HANDLE hwndParent, DWORD OpenFlags, LPVOID DeviceInfoData)');
# my $ret = $SetupDiOpenDeviceInfoW->Call($DeviceInfoSet, $DeviceInstanceId, $hwndParent, $OpenFlags, $DeviceInfoData);
# DeviceInfoSet : HDEVINFO -> LPARAM
# DeviceInstanceId : LPCWSTR -> LPCWSTR
# hwndParent : HWND optional -> HANDLE
# OpenFlags : DWORD -> DWORD
# DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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