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

SetupDiCreateDeviceInfoW

関数
デバイス情報セットに新しいデバイス情報要素を作成する。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetupDiCreateDeviceInfoW(
    HDEVINFO DeviceInfoSet,
    LPCWSTR DeviceName,
    const GUID* ClassGuid,
    LPCWSTR DeviceDescription,   // optional
    HWND hwndParent,   // optional
    SETUP_DI_DEVICE_CREATION_FLAGS CreationFlags,
    SP_DEVINFO_DATA* DeviceInfoData   // optional
);

パラメーター

名前方向説明
DeviceInfoSetHDEVINFOin新しいデバイス情報要素を追加する対象のデバイス情報セット。
DeviceNameLPCWSTRin作成するデバイスインスタンスのID/名前(Unicode)。
ClassGuidGUID*inデバイスのセットアップクラスGUIDへのポインタ。
DeviceDescriptionLPCWSTRinoptionalデバイスの説明文字列(Unicode)。NULL可。
hwndParentHWNDinoptionalUI表示時の親ウィンドウハンドル。NULL可。
CreationFlagsSETUP_DI_DEVICE_CREATION_FLAGSinDICD_*系の作成動作を制御するフラグ。
DeviceInfoDataSP_DEVINFO_DATA*inoutoptional作成された要素情報を受け取るSP_DEVINFO_DATAへのポインタ(出力)。NULL可。

戻り値の型: BOOL

各言語での呼び出し定義

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

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

SetupDiCreateDeviceInfoW = ctypes.windll.setupapi.SetupDiCreateDeviceInfoW
SetupDiCreateDeviceInfoW.restype = wintypes.BOOL
SetupDiCreateDeviceInfoW.argtypes = [
    ctypes.c_ssize_t,  # DeviceInfoSet : HDEVINFO
    wintypes.LPCWSTR,  # DeviceName : LPCWSTR
    ctypes.c_void_p,  # ClassGuid : GUID*
    wintypes.LPCWSTR,  # DeviceDescription : LPCWSTR optional
    wintypes.HANDLE,  # hwndParent : HWND optional
    wintypes.DWORD,  # CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS
    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')
SetupDiCreateDeviceInfoW = Fiddle::Function.new(
  lib['SetupDiCreateDeviceInfoW'],
  [
    Fiddle::TYPE_INTPTR_T,  # DeviceInfoSet : HDEVINFO
    Fiddle::TYPE_VOIDP,  # DeviceName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # ClassGuid : GUID*
    Fiddle::TYPE_VOIDP,  # DeviceDescription : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # hwndParent : HWND optional
    -Fiddle::TYPE_INT,  # CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS
    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 SetupDiCreateDeviceInfoW(
        DeviceInfoSet: isize,  // HDEVINFO
        DeviceName: *const u16,  // LPCWSTR
        ClassGuid: *const GUID,  // GUID*
        DeviceDescription: *const u16,  // LPCWSTR optional
        hwndParent: *mut core::ffi::c_void,  // HWND optional
        CreationFlags: u32,  // SETUP_DI_DEVICE_CREATION_FLAGS
        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 SetupDiCreateDeviceInfoW(IntPtr DeviceInfoSet, [MarshalAs(UnmanagedType.LPWStr)] string DeviceName, ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPWStr)] string DeviceDescription, IntPtr hwndParent, uint CreationFlags, IntPtr DeviceInfoData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupDiCreateDeviceInfoW' -Namespace Win32 -PassThru
# $api::SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
#uselib "SETUPAPI.dll"
#func global SetupDiCreateDeviceInfoW "SetupDiCreateDeviceInfoW" wptr, wptr, wptr, wptr, wptr, wptr, wptr
; SetupDiCreateDeviceInfoW DeviceInfoSet, DeviceName, varptr(ClassGuid), DeviceDescription, hwndParent, CreationFlags, varptr(DeviceInfoData)   ; 戻り値は stat
; DeviceInfoSet : HDEVINFO -> "wptr"
; DeviceName : LPCWSTR -> "wptr"
; ClassGuid : GUID* -> "wptr"
; DeviceDescription : LPCWSTR optional -> "wptr"
; hwndParent : HWND optional -> "wptr"
; CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> "wptr"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupDiCreateDeviceInfoW "SetupDiCreateDeviceInfoW" sptr, wstr, var, wstr, sptr, int, var
; res = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
; DeviceInfoSet : HDEVINFO -> "sptr"
; DeviceName : LPCWSTR -> "wstr"
; ClassGuid : GUID* -> "var"
; DeviceDescription : LPCWSTR optional -> "wstr"
; hwndParent : HWND optional -> "sptr"
; CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> "int"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetupDiCreateDeviceInfoW(HDEVINFO DeviceInfoSet, LPCWSTR DeviceName, GUID* ClassGuid, LPCWSTR DeviceDescription, HWND hwndParent, SETUP_DI_DEVICE_CREATION_FLAGS CreationFlags, SP_DEVINFO_DATA* DeviceInfoData)
#uselib "SETUPAPI.dll"
#cfunc global SetupDiCreateDeviceInfoW "SetupDiCreateDeviceInfoW" intptr, wstr, var, wstr, intptr, int, var
; res = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
; DeviceInfoSet : HDEVINFO -> "intptr"
; DeviceName : LPCWSTR -> "wstr"
; ClassGuid : GUID* -> "var"
; DeviceDescription : LPCWSTR optional -> "wstr"
; hwndParent : HWND optional -> "intptr"
; CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> "int"
; DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupDiCreateDeviceInfoW = setupapi.NewProc("SetupDiCreateDeviceInfoW")
)

// DeviceInfoSet (HDEVINFO), DeviceName (LPCWSTR), ClassGuid (GUID*), DeviceDescription (LPCWSTR optional), hwndParent (HWND optional), CreationFlags (SETUP_DI_DEVICE_CREATION_FLAGS), DeviceInfoData (SP_DEVINFO_DATA* optional, in/out)
r1, _, err := procSetupDiCreateDeviceInfoW.Call(
	uintptr(DeviceInfoSet),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DeviceName))),
	uintptr(ClassGuid),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DeviceDescription))),
	uintptr(hwndParent),
	uintptr(CreationFlags),
	uintptr(DeviceInfoData),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetupDiCreateDeviceInfoW(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceName: PWideChar;   // LPCWSTR
  ClassGuid: PGUID;   // GUID*
  DeviceDescription: PWideChar;   // LPCWSTR optional
  hwndParent: THandle;   // HWND optional
  CreationFlags: DWORD;   // SETUP_DI_DEVICE_CREATION_FLAGS
  DeviceInfoData: Pointer   // SP_DEVINFO_DATA* optional, in/out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiCreateDeviceInfoW';
result := DllCall("SETUPAPI\SetupDiCreateDeviceInfoW"
    , "Ptr", DeviceInfoSet   ; HDEVINFO
    , "WStr", DeviceName   ; LPCWSTR
    , "Ptr", ClassGuid   ; GUID*
    , "WStr", DeviceDescription   ; LPCWSTR optional
    , "Ptr", hwndParent   ; HWND optional
    , "UInt", CreationFlags   ; SETUP_DI_DEVICE_CREATION_FLAGS
    , "Ptr", DeviceInfoData   ; SP_DEVINFO_DATA* optional, in/out
    , "Int")   ; return: BOOL
●SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData) = DLL("SETUPAPI.dll", "bool SetupDiCreateDeviceInfoW(int, char*, void*, char*, void*, dword, void*)")
# 呼び出し: SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
# DeviceInfoSet : HDEVINFO -> "int"
# DeviceName : LPCWSTR -> "char*"
# ClassGuid : GUID* -> "void*"
# DeviceDescription : LPCWSTR optional -> "char*"
# hwndParent : HWND optional -> "void*"
# CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> "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 SetupDiCreateDeviceInfoW(
    DeviceInfoSet: isize, // HDEVINFO
    DeviceName: [*c]const u16, // LPCWSTR
    ClassGuid: [*c]GUID, // GUID*
    DeviceDescription: [*c]const u16, // LPCWSTR optional
    hwndParent: ?*anyopaque, // HWND optional
    CreationFlags: u32, // SETUP_DI_DEVICE_CREATION_FLAGS
    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 SetupDiCreateDeviceInfoW(
    DeviceInfoSet: int,  # HDEVINFO
    DeviceName: WideCString,  # LPCWSTR
    ClassGuid: ptr GUID,  # GUID*
    DeviceDescription: WideCString,  # LPCWSTR optional
    hwndParent: pointer,  # HWND optional
    CreationFlags: uint32,  # SETUP_DI_DEVICE_CREATION_FLAGS
    DeviceInfoData: ptr SP_DEVINFO_DATA  # SP_DEVINFO_DATA* optional, in/out
): int32 {.importc: "SetupDiCreateDeviceInfoW", stdcall, dynlib: "SETUPAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "setupapi");
extern(Windows)
int SetupDiCreateDeviceInfoW(
    ptrdiff_t DeviceInfoSet,   // HDEVINFO
    const(wchar)* DeviceName,   // LPCWSTR
    GUID* ClassGuid,   // GUID*
    const(wchar)* DeviceDescription,   // LPCWSTR optional
    void* hwndParent,   // HWND optional
    uint CreationFlags,   // SETUP_DI_DEVICE_CREATION_FLAGS
    SP_DEVINFO_DATA* DeviceInfoData   // SP_DEVINFO_DATA* optional, in/out
);
ccall((:SetupDiCreateDeviceInfoW, "SETUPAPI.dll"), stdcall, Int32,
      (Int, Cwstring, Ptr{GUID}, Cwstring, Ptr{Cvoid}, UInt32, Ptr{SP_DEVINFO_DATA}),
      DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
# DeviceInfoSet : HDEVINFO -> Int
# DeviceName : LPCWSTR -> Cwstring
# ClassGuid : GUID* -> Ptr{GUID}
# DeviceDescription : LPCWSTR optional -> Cwstring
# hwndParent : HWND optional -> Ptr{Cvoid}
# CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> 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 SetupDiCreateDeviceInfoW(
    intptr_t DeviceInfoSet,
    const uint16_t* DeviceName,
    void* ClassGuid,
    const uint16_t* DeviceDescription,
    void* hwndParent,
    uint32_t CreationFlags,
    void* DeviceInfoData);
]]
local setupapi = ffi.load("setupapi")
-- setupapi.SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
-- DeviceInfoSet : HDEVINFO
-- DeviceName : LPCWSTR
-- ClassGuid : GUID*
-- DeviceDescription : LPCWSTR optional
-- hwndParent : HWND optional
-- CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS
-- 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 SetupDiCreateDeviceInfoW = lib.func('__stdcall', 'SetupDiCreateDeviceInfoW', 'int32_t', ['intptr_t', 'str16', 'void *', 'str16', 'void *', 'uint32_t', 'void *']);
// SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
// DeviceInfoSet : HDEVINFO -> 'intptr_t'
// DeviceName : LPCWSTR -> 'str16'
// ClassGuid : GUID* -> 'void *'
// DeviceDescription : LPCWSTR optional -> 'str16'
// hwndParent : HWND optional -> 'void *'
// CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> 'uint32_t'
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SETUPAPI.dll", {
  SetupDiCreateDeviceInfoW: { parameters: ["isize", "buffer", "pointer", "buffer", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData)
// DeviceInfoSet : HDEVINFO -> "isize"
// DeviceName : LPCWSTR -> "buffer"
// ClassGuid : GUID* -> "pointer"
// DeviceDescription : LPCWSTR optional -> "buffer"
// hwndParent : HWND optional -> "pointer"
// CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> "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 SetupDiCreateDeviceInfoW(
    intptr_t DeviceInfoSet,
    const uint16_t* DeviceName,
    void* ClassGuid,
    const uint16_t* DeviceDescription,
    void* hwndParent,
    uint32_t CreationFlags,
    void* DeviceInfoData);
C, "SETUPAPI.dll");
// $ffi->SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceName, ClassGuid, DeviceDescription, hwndParent, CreationFlags, DeviceInfoData);
// DeviceInfoSet : HDEVINFO
// DeviceName : LPCWSTR
// ClassGuid : GUID*
// DeviceDescription : LPCWSTR optional
// hwndParent : HWND optional
// CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS
// 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 SetupDiCreateDeviceInfoW(
        long DeviceInfoSet,   // HDEVINFO
        WString DeviceName,   // LPCWSTR
        Pointer ClassGuid,   // GUID*
        WString DeviceDescription,   // LPCWSTR optional
        Pointer hwndParent,   // HWND optional
        int CreationFlags,   // SETUP_DI_DEVICE_CREATION_FLAGS
        Pointer DeviceInfoData   // SP_DEVINFO_DATA* optional, in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("setupapi")]
lib LibSETUPAPI
  fun SetupDiCreateDeviceInfoW = SetupDiCreateDeviceInfoW(
    DeviceInfoSet : LibC::SSizeT,   # HDEVINFO
    DeviceName : UInt16*,   # LPCWSTR
    ClassGuid : GUID*,   # GUID*
    DeviceDescription : UInt16*,   # LPCWSTR optional
    hwndParent : Void*,   # HWND optional
    CreationFlags : UInt32,   # SETUP_DI_DEVICE_CREATION_FLAGS
    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 SetupDiCreateDeviceInfoWNative = Int32 Function(IntPtr, Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Void>);
typedef SetupDiCreateDeviceInfoWDart = int Function(int, Pointer<Utf16>, Pointer<Void>, Pointer<Utf16>, Pointer<Void>, int, Pointer<Void>);
final SetupDiCreateDeviceInfoW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupDiCreateDeviceInfoWNative, SetupDiCreateDeviceInfoWDart>('SetupDiCreateDeviceInfoW');
// DeviceInfoSet : HDEVINFO -> IntPtr
// DeviceName : LPCWSTR -> Pointer<Utf16>
// ClassGuid : GUID* -> Pointer<Void>
// DeviceDescription : LPCWSTR optional -> Pointer<Utf16>
// hwndParent : HWND optional -> Pointer<Void>
// CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> Uint32
// DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupDiCreateDeviceInfoW(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceName: PWideChar;   // LPCWSTR
  ClassGuid: PGUID;   // GUID*
  DeviceDescription: PWideChar;   // LPCWSTR optional
  hwndParent: THandle;   // HWND optional
  CreationFlags: DWORD;   // SETUP_DI_DEVICE_CREATION_FLAGS
  DeviceInfoData: Pointer   // SP_DEVINFO_DATA* optional, in/out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiCreateDeviceInfoW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let setupdicreatedeviceinfow =
  foreign "SetupDiCreateDeviceInfoW"
    (intptr_t @-> (ptr uint16_t) @-> (ptr void) @-> (ptr uint16_t) @-> (ptr void) @-> uint32_t @-> (ptr void) @-> returning int32_t)
(* DeviceInfoSet : HDEVINFO -> intptr_t *)
(* DeviceName : LPCWSTR -> (ptr uint16_t) *)
(* ClassGuid : GUID* -> (ptr void) *)
(* DeviceDescription : LPCWSTR optional -> (ptr uint16_t) *)
(* hwndParent : HWND optional -> (ptr void) *)
(* CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> 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 ("SetupDiCreateDeviceInfoW" setup-di-create-device-info-w :convention :stdcall) :int32
  (device-info-set :int64)   ; HDEVINFO
  (device-name (:string :encoding :utf-16le))   ; LPCWSTR
  (class-guid :pointer)   ; GUID*
  (device-description (:string :encoding :utf-16le))   ; LPCWSTR optional
  (hwnd-parent :pointer)   ; HWND optional
  (creation-flags :uint32)   ; SETUP_DI_DEVICE_CREATION_FLAGS
  (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 $SetupDiCreateDeviceInfoW = Win32::API::More->new('SETUPAPI',
    'BOOL SetupDiCreateDeviceInfoW(LPARAM DeviceInfoSet, LPCWSTR DeviceName, LPVOID ClassGuid, LPCWSTR DeviceDescription, HANDLE hwndParent, DWORD CreationFlags, LPVOID DeviceInfoData)');
# my $ret = $SetupDiCreateDeviceInfoW->Call($DeviceInfoSet, $DeviceName, $ClassGuid, $DeviceDescription, $hwndParent, $CreationFlags, $DeviceInfoData);
# DeviceInfoSet : HDEVINFO -> LPARAM
# DeviceName : LPCWSTR -> LPCWSTR
# ClassGuid : GUID* -> LPVOID
# DeviceDescription : LPCWSTR optional -> LPCWSTR
# hwndParent : HWND optional -> HANDLE
# CreationFlags : SETUP_DI_DEVICE_CREATION_FLAGS -> DWORD
# DeviceInfoData : SP_DEVINFO_DATA* optional, in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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