Win32 API 日本語リファレンス
ホームDevices.Enumeration.Pnp › SwDeviceCreate

SwDeviceCreate

関数
ソフトウェアデバイスを作成して列挙させる。
DLLCFGMGR32.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// CFGMGR32.dll
#include <windows.h>

HRESULT SwDeviceCreate(
    LPCWSTR pszEnumeratorName,
    LPCWSTR pszParentDeviceInstance,
    const SW_DEVICE_CREATE_INFO* pCreateInfo,
    DWORD cPropertyCount,
    const DEVPROPERTY* pProperties,   // optional
    SW_DEVICE_CREATE_CALLBACK pCallback,
    void* pContext,   // optional
    HSWDEVICE* phSwDevice
);

パラメーター

名前方向説明
pszEnumeratorNameLPCWSTRinソフトウェアデバイスのエニュメレータ名を指定する文字列。デバイス識別の名前空間となる。
pszParentDeviceInstanceLPCWSTRin親デバイスのインスタンスIDを指定する文字列。階層上の親を示す。
pCreateInfoSW_DEVICE_CREATE_INFO*inデバイス作成情報を格納したSW_DEVICE_CREATE_INFO構造体へのポインタを指定する。
cPropertyCountDWORDinpProperties配列が含むプロパティ数を指定する。0なら設定なし。
pPropertiesDEVPROPERTY*inoptionalデバイスに設定するDEVPROPERTY配列へのポインタを指定する。NULL可。
pCallbackSW_DEVICE_CREATE_CALLBACKinデバイス作成完了を通知するコールバック関数へのポインタを指定する。
pContextvoid*inoptionalコールバックに渡す任意のコンテキストポインタを指定する。NULL可。
phSwDeviceHSWDEVICE*out作成されたソフトウェアデバイスのハンドルを受け取るHSWDEVICEへのポインタを指定する。

戻り値の型: HRESULT

各言語での呼び出し定義

// CFGMGR32.dll
#include <windows.h>

HRESULT SwDeviceCreate(
    LPCWSTR pszEnumeratorName,
    LPCWSTR pszParentDeviceInstance,
    const SW_DEVICE_CREATE_INFO* pCreateInfo,
    DWORD cPropertyCount,
    const DEVPROPERTY* pProperties,   // optional
    SW_DEVICE_CREATE_CALLBACK pCallback,
    void* pContext,   // optional
    HSWDEVICE* phSwDevice
);
[DllImport("CFGMGR32.dll", ExactSpelling = true)]
static extern int SwDeviceCreate(
    [MarshalAs(UnmanagedType.LPWStr)] string pszEnumeratorName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pszParentDeviceInstance,   // LPCWSTR
    IntPtr pCreateInfo,   // SW_DEVICE_CREATE_INFO*
    uint cPropertyCount,   // DWORD
    IntPtr pProperties,   // DEVPROPERTY* optional
    IntPtr pCallback,   // SW_DEVICE_CREATE_CALLBACK
    IntPtr pContext,   // void* optional
    IntPtr phSwDevice   // HSWDEVICE* out
);
<DllImport("CFGMGR32.dll", ExactSpelling:=True)>
Public Shared Function SwDeviceCreate(
    <MarshalAs(UnmanagedType.LPWStr)> pszEnumeratorName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pszParentDeviceInstance As String,   ' LPCWSTR
    pCreateInfo As IntPtr,   ' SW_DEVICE_CREATE_INFO*
    cPropertyCount As UInteger,   ' DWORD
    pProperties As IntPtr,   ' DEVPROPERTY* optional
    pCallback As IntPtr,   ' SW_DEVICE_CREATE_CALLBACK
    pContext As IntPtr,   ' void* optional
    phSwDevice As IntPtr   ' HSWDEVICE* out
) As Integer
End Function
' pszEnumeratorName : LPCWSTR
' pszParentDeviceInstance : LPCWSTR
' pCreateInfo : SW_DEVICE_CREATE_INFO*
' cPropertyCount : DWORD
' pProperties : DEVPROPERTY* optional
' pCallback : SW_DEVICE_CREATE_CALLBACK
' pContext : void* optional
' phSwDevice : HSWDEVICE* out
Declare PtrSafe Function SwDeviceCreate Lib "cfgmgr32" ( _
    ByVal pszEnumeratorName As LongPtr, _
    ByVal pszParentDeviceInstance As LongPtr, _
    ByVal pCreateInfo As LongPtr, _
    ByVal cPropertyCount As Long, _
    ByVal pProperties As LongPtr, _
    ByVal pCallback As LongPtr, _
    ByVal pContext As LongPtr, _
    ByVal phSwDevice As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SwDeviceCreate = ctypes.windll.cfgmgr32.SwDeviceCreate
SwDeviceCreate.restype = ctypes.c_int
SwDeviceCreate.argtypes = [
    wintypes.LPCWSTR,  # pszEnumeratorName : LPCWSTR
    wintypes.LPCWSTR,  # pszParentDeviceInstance : LPCWSTR
    ctypes.c_void_p,  # pCreateInfo : SW_DEVICE_CREATE_INFO*
    wintypes.DWORD,  # cPropertyCount : DWORD
    ctypes.c_void_p,  # pProperties : DEVPROPERTY* optional
    ctypes.c_void_p,  # pCallback : SW_DEVICE_CREATE_CALLBACK
    ctypes.POINTER(None),  # pContext : void* optional
    ctypes.c_void_p,  # phSwDevice : HSWDEVICE* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CFGMGR32.dll')
SwDeviceCreate = Fiddle::Function.new(
  lib['SwDeviceCreate'],
  [
    Fiddle::TYPE_VOIDP,  # pszEnumeratorName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pszParentDeviceInstance : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pCreateInfo : SW_DEVICE_CREATE_INFO*
    -Fiddle::TYPE_INT,  # cPropertyCount : DWORD
    Fiddle::TYPE_VOIDP,  # pProperties : DEVPROPERTY* optional
    Fiddle::TYPE_VOIDP,  # pCallback : SW_DEVICE_CREATE_CALLBACK
    Fiddle::TYPE_VOIDP,  # pContext : void* optional
    Fiddle::TYPE_VOIDP,  # phSwDevice : HSWDEVICE* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "cfgmgr32")]
extern "system" {
    fn SwDeviceCreate(
        pszEnumeratorName: *const u16,  // LPCWSTR
        pszParentDeviceInstance: *const u16,  // LPCWSTR
        pCreateInfo: *const SW_DEVICE_CREATE_INFO,  // SW_DEVICE_CREATE_INFO*
        cPropertyCount: u32,  // DWORD
        pProperties: *const DEVPROPERTY,  // DEVPROPERTY* optional
        pCallback: *const core::ffi::c_void,  // SW_DEVICE_CREATE_CALLBACK
        pContext: *mut (),  // void* optional
        phSwDevice: *mut *mut core::ffi::c_void  // HSWDEVICE* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("CFGMGR32.dll")]
public static extern int SwDeviceCreate([MarshalAs(UnmanagedType.LPWStr)] string pszEnumeratorName, [MarshalAs(UnmanagedType.LPWStr)] string pszParentDeviceInstance, IntPtr pCreateInfo, uint cPropertyCount, IntPtr pProperties, IntPtr pCallback, IntPtr pContext, IntPtr phSwDevice);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CFGMGR32_SwDeviceCreate' -Namespace Win32 -PassThru
# $api::SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
#uselib "CFGMGR32.dll"
#func global SwDeviceCreate "SwDeviceCreate" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SwDeviceCreate pszEnumeratorName, pszParentDeviceInstance, varptr(pCreateInfo), cPropertyCount, varptr(pProperties), pCallback, pContext, phSwDevice   ; 戻り値は stat
; pszEnumeratorName : LPCWSTR -> "sptr"
; pszParentDeviceInstance : LPCWSTR -> "sptr"
; pCreateInfo : SW_DEVICE_CREATE_INFO* -> "sptr"
; cPropertyCount : DWORD -> "sptr"
; pProperties : DEVPROPERTY* optional -> "sptr"
; pCallback : SW_DEVICE_CREATE_CALLBACK -> "sptr"
; pContext : void* optional -> "sptr"
; phSwDevice : HSWDEVICE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CFGMGR32.dll"
#cfunc global SwDeviceCreate "SwDeviceCreate" wstr, wstr, var, int, var, sptr, sptr, sptr
; res = SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
; pszEnumeratorName : LPCWSTR -> "wstr"
; pszParentDeviceInstance : LPCWSTR -> "wstr"
; pCreateInfo : SW_DEVICE_CREATE_INFO* -> "var"
; cPropertyCount : DWORD -> "int"
; pProperties : DEVPROPERTY* optional -> "var"
; pCallback : SW_DEVICE_CREATE_CALLBACK -> "sptr"
; pContext : void* optional -> "sptr"
; phSwDevice : HSWDEVICE* out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT SwDeviceCreate(LPCWSTR pszEnumeratorName, LPCWSTR pszParentDeviceInstance, SW_DEVICE_CREATE_INFO* pCreateInfo, DWORD cPropertyCount, DEVPROPERTY* pProperties, SW_DEVICE_CREATE_CALLBACK pCallback, void* pContext, HSWDEVICE* phSwDevice)
#uselib "CFGMGR32.dll"
#cfunc global SwDeviceCreate "SwDeviceCreate" wstr, wstr, var, int, var, intptr, intptr, intptr
; res = SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
; pszEnumeratorName : LPCWSTR -> "wstr"
; pszParentDeviceInstance : LPCWSTR -> "wstr"
; pCreateInfo : SW_DEVICE_CREATE_INFO* -> "var"
; cPropertyCount : DWORD -> "int"
; pProperties : DEVPROPERTY* optional -> "var"
; pCallback : SW_DEVICE_CREATE_CALLBACK -> "intptr"
; pContext : void* optional -> "intptr"
; phSwDevice : HSWDEVICE* out -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	cfgmgr32 = windows.NewLazySystemDLL("CFGMGR32.dll")
	procSwDeviceCreate = cfgmgr32.NewProc("SwDeviceCreate")
)

// pszEnumeratorName (LPCWSTR), pszParentDeviceInstance (LPCWSTR), pCreateInfo (SW_DEVICE_CREATE_INFO*), cPropertyCount (DWORD), pProperties (DEVPROPERTY* optional), pCallback (SW_DEVICE_CREATE_CALLBACK), pContext (void* optional), phSwDevice (HSWDEVICE* out)
r1, _, err := procSwDeviceCreate.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszEnumeratorName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszParentDeviceInstance))),
	uintptr(pCreateInfo),
	uintptr(cPropertyCount),
	uintptr(pProperties),
	uintptr(pCallback),
	uintptr(pContext),
	uintptr(phSwDevice),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SwDeviceCreate(
  pszEnumeratorName: PWideChar;   // LPCWSTR
  pszParentDeviceInstance: PWideChar;   // LPCWSTR
  pCreateInfo: Pointer;   // SW_DEVICE_CREATE_INFO*
  cPropertyCount: DWORD;   // DWORD
  pProperties: Pointer;   // DEVPROPERTY* optional
  pCallback: Pointer;   // SW_DEVICE_CREATE_CALLBACK
  pContext: Pointer;   // void* optional
  phSwDevice: Pointer   // HSWDEVICE* out
): Integer; stdcall;
  external 'CFGMGR32.dll' name 'SwDeviceCreate';
result := DllCall("CFGMGR32\SwDeviceCreate"
    , "WStr", pszEnumeratorName   ; LPCWSTR
    , "WStr", pszParentDeviceInstance   ; LPCWSTR
    , "Ptr", pCreateInfo   ; SW_DEVICE_CREATE_INFO*
    , "UInt", cPropertyCount   ; DWORD
    , "Ptr", pProperties   ; DEVPROPERTY* optional
    , "Ptr", pCallback   ; SW_DEVICE_CREATE_CALLBACK
    , "Ptr", pContext   ; void* optional
    , "Ptr", phSwDevice   ; HSWDEVICE* out
    , "Int")   ; return: HRESULT
●SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice) = DLL("CFGMGR32.dll", "int SwDeviceCreate(char*, char*, void*, dword, void*, void*, void*, void*)")
# 呼び出し: SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
# pszEnumeratorName : LPCWSTR -> "char*"
# pszParentDeviceInstance : LPCWSTR -> "char*"
# pCreateInfo : SW_DEVICE_CREATE_INFO* -> "void*"
# cPropertyCount : DWORD -> "dword"
# pProperties : DEVPROPERTY* optional -> "void*"
# pCallback : SW_DEVICE_CREATE_CALLBACK -> "void*"
# pContext : void* optional -> "void*"
# phSwDevice : HSWDEVICE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "cfgmgr32" fn SwDeviceCreate(
    pszEnumeratorName: [*c]const u16, // LPCWSTR
    pszParentDeviceInstance: [*c]const u16, // LPCWSTR
    pCreateInfo: [*c]SW_DEVICE_CREATE_INFO, // SW_DEVICE_CREATE_INFO*
    cPropertyCount: u32, // DWORD
    pProperties: [*c]DEVPROPERTY, // DEVPROPERTY* optional
    pCallback: ?*anyopaque, // SW_DEVICE_CREATE_CALLBACK
    pContext: ?*anyopaque, // void* optional
    phSwDevice: ?*anyopaque // HSWDEVICE* out
) callconv(std.os.windows.WINAPI) i32;
proc SwDeviceCreate(
    pszEnumeratorName: WideCString,  # LPCWSTR
    pszParentDeviceInstance: WideCString,  # LPCWSTR
    pCreateInfo: ptr SW_DEVICE_CREATE_INFO,  # SW_DEVICE_CREATE_INFO*
    cPropertyCount: uint32,  # DWORD
    pProperties: ptr DEVPROPERTY,  # DEVPROPERTY* optional
    pCallback: pointer,  # SW_DEVICE_CREATE_CALLBACK
    pContext: pointer,  # void* optional
    phSwDevice: pointer  # HSWDEVICE* out
): int32 {.importc: "SwDeviceCreate", stdcall, dynlib: "CFGMGR32.dll".}
pragma(lib, "cfgmgr32");
extern(Windows)
int SwDeviceCreate(
    const(wchar)* pszEnumeratorName,   // LPCWSTR
    const(wchar)* pszParentDeviceInstance,   // LPCWSTR
    SW_DEVICE_CREATE_INFO* pCreateInfo,   // SW_DEVICE_CREATE_INFO*
    uint cPropertyCount,   // DWORD
    DEVPROPERTY* pProperties,   // DEVPROPERTY* optional
    void* pCallback,   // SW_DEVICE_CREATE_CALLBACK
    void* pContext,   // void* optional
    void* phSwDevice   // HSWDEVICE* out
);
ccall((:SwDeviceCreate, "CFGMGR32.dll"), stdcall, Int32,
      (Cwstring, Cwstring, Ptr{SW_DEVICE_CREATE_INFO}, UInt32, Ptr{DEVPROPERTY}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
      pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
# pszEnumeratorName : LPCWSTR -> Cwstring
# pszParentDeviceInstance : LPCWSTR -> Cwstring
# pCreateInfo : SW_DEVICE_CREATE_INFO* -> Ptr{SW_DEVICE_CREATE_INFO}
# cPropertyCount : DWORD -> UInt32
# pProperties : DEVPROPERTY* optional -> Ptr{DEVPROPERTY}
# pCallback : SW_DEVICE_CREATE_CALLBACK -> Ptr{Cvoid}
# pContext : void* optional -> Ptr{Cvoid}
# phSwDevice : HSWDEVICE* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SwDeviceCreate(
    const uint16_t* pszEnumeratorName,
    const uint16_t* pszParentDeviceInstance,
    void* pCreateInfo,
    uint32_t cPropertyCount,
    void* pProperties,
    void* pCallback,
    void* pContext,
    void* phSwDevice);
]]
local cfgmgr32 = ffi.load("cfgmgr32")
-- cfgmgr32.SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
-- pszEnumeratorName : LPCWSTR
-- pszParentDeviceInstance : LPCWSTR
-- pCreateInfo : SW_DEVICE_CREATE_INFO*
-- cPropertyCount : DWORD
-- pProperties : DEVPROPERTY* optional
-- pCallback : SW_DEVICE_CREATE_CALLBACK
-- pContext : void* optional
-- phSwDevice : HSWDEVICE* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CFGMGR32.dll');
const SwDeviceCreate = lib.func('__stdcall', 'SwDeviceCreate', 'int32_t', ['str16', 'str16', 'void *', 'uint32_t', 'void *', 'void *', 'void *', 'void *']);
// SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
// pszEnumeratorName : LPCWSTR -> 'str16'
// pszParentDeviceInstance : LPCWSTR -> 'str16'
// pCreateInfo : SW_DEVICE_CREATE_INFO* -> 'void *'
// cPropertyCount : DWORD -> 'uint32_t'
// pProperties : DEVPROPERTY* optional -> 'void *'
// pCallback : SW_DEVICE_CREATE_CALLBACK -> 'void *'
// pContext : void* optional -> 'void *'
// phSwDevice : HSWDEVICE* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("CFGMGR32.dll", {
  SwDeviceCreate: { parameters: ["buffer", "buffer", "pointer", "u32", "pointer", "pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice)
// pszEnumeratorName : LPCWSTR -> "buffer"
// pszParentDeviceInstance : LPCWSTR -> "buffer"
// pCreateInfo : SW_DEVICE_CREATE_INFO* -> "pointer"
// cPropertyCount : DWORD -> "u32"
// pProperties : DEVPROPERTY* optional -> "pointer"
// pCallback : SW_DEVICE_CREATE_CALLBACK -> "pointer"
// pContext : void* optional -> "pointer"
// phSwDevice : HSWDEVICE* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SwDeviceCreate(
    const uint16_t* pszEnumeratorName,
    const uint16_t* pszParentDeviceInstance,
    void* pCreateInfo,
    uint32_t cPropertyCount,
    void* pProperties,
    void* pCallback,
    void* pContext,
    void* phSwDevice);
C, "CFGMGR32.dll");
// $ffi->SwDeviceCreate(pszEnumeratorName, pszParentDeviceInstance, pCreateInfo, cPropertyCount, pProperties, pCallback, pContext, phSwDevice);
// pszEnumeratorName : LPCWSTR
// pszParentDeviceInstance : LPCWSTR
// pCreateInfo : SW_DEVICE_CREATE_INFO*
// cPropertyCount : DWORD
// pProperties : DEVPROPERTY* optional
// pCallback : SW_DEVICE_CREATE_CALLBACK
// pContext : void* optional
// phSwDevice : HSWDEVICE* 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 Cfgmgr32 extends StdCallLibrary {
    Cfgmgr32 INSTANCE = Native.load("cfgmgr32", Cfgmgr32.class);
    int SwDeviceCreate(
        WString pszEnumeratorName,   // LPCWSTR
        WString pszParentDeviceInstance,   // LPCWSTR
        Pointer pCreateInfo,   // SW_DEVICE_CREATE_INFO*
        int cPropertyCount,   // DWORD
        Pointer pProperties,   // DEVPROPERTY* optional
        Callback pCallback,   // SW_DEVICE_CREATE_CALLBACK
        Pointer pContext,   // void* optional
        Pointer phSwDevice   // HSWDEVICE* out
    );
}
@[Link("cfgmgr32")]
lib LibCFGMGR32
  fun SwDeviceCreate = SwDeviceCreate(
    pszEnumeratorName : UInt16*,   # LPCWSTR
    pszParentDeviceInstance : UInt16*,   # LPCWSTR
    pCreateInfo : SW_DEVICE_CREATE_INFO*,   # SW_DEVICE_CREATE_INFO*
    cPropertyCount : UInt32,   # DWORD
    pProperties : DEVPROPERTY*,   # DEVPROPERTY* optional
    pCallback : Void*,   # SW_DEVICE_CREATE_CALLBACK
    pContext : Void*,   # void* optional
    phSwDevice : Void*   # HSWDEVICE* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SwDeviceCreateNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Void>, Pointer<Void>);
typedef SwDeviceCreateDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Void>, int, Pointer<Void>, Pointer<Void>, Pointer<Void>, Pointer<Void>);
final SwDeviceCreate = DynamicLibrary.open('CFGMGR32.dll')
    .lookupFunction<SwDeviceCreateNative, SwDeviceCreateDart>('SwDeviceCreate');
// pszEnumeratorName : LPCWSTR -> Pointer<Utf16>
// pszParentDeviceInstance : LPCWSTR -> Pointer<Utf16>
// pCreateInfo : SW_DEVICE_CREATE_INFO* -> Pointer<Void>
// cPropertyCount : DWORD -> Uint32
// pProperties : DEVPROPERTY* optional -> Pointer<Void>
// pCallback : SW_DEVICE_CREATE_CALLBACK -> Pointer<Void>
// pContext : void* optional -> Pointer<Void>
// phSwDevice : HSWDEVICE* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SwDeviceCreate(
  pszEnumeratorName: PWideChar;   // LPCWSTR
  pszParentDeviceInstance: PWideChar;   // LPCWSTR
  pCreateInfo: Pointer;   // SW_DEVICE_CREATE_INFO*
  cPropertyCount: DWORD;   // DWORD
  pProperties: Pointer;   // DEVPROPERTY* optional
  pCallback: Pointer;   // SW_DEVICE_CREATE_CALLBACK
  pContext: Pointer;   // void* optional
  phSwDevice: Pointer   // HSWDEVICE* out
): Integer; stdcall;
  external 'CFGMGR32.dll' name 'SwDeviceCreate';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SwDeviceCreate"
  c_SwDeviceCreate :: CWString -> CWString -> Ptr () -> Word32 -> Ptr () -> Ptr () -> Ptr () -> Ptr () -> IO Int32
-- pszEnumeratorName : LPCWSTR -> CWString
-- pszParentDeviceInstance : LPCWSTR -> CWString
-- pCreateInfo : SW_DEVICE_CREATE_INFO* -> Ptr ()
-- cPropertyCount : DWORD -> Word32
-- pProperties : DEVPROPERTY* optional -> Ptr ()
-- pCallback : SW_DEVICE_CREATE_CALLBACK -> Ptr ()
-- pContext : void* optional -> Ptr ()
-- phSwDevice : HSWDEVICE* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let swdevicecreate =
  foreign "SwDeviceCreate"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr void) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* pszEnumeratorName : LPCWSTR -> (ptr uint16_t) *)
(* pszParentDeviceInstance : LPCWSTR -> (ptr uint16_t) *)
(* pCreateInfo : SW_DEVICE_CREATE_INFO* -> (ptr void) *)
(* cPropertyCount : DWORD -> uint32_t *)
(* pProperties : DEVPROPERTY* optional -> (ptr void) *)
(* pCallback : SW_DEVICE_CREATE_CALLBACK -> (ptr void) *)
(* pContext : void* optional -> (ptr void) *)
(* phSwDevice : HSWDEVICE* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library cfgmgr32 (t "CFGMGR32.dll"))
(cffi:use-foreign-library cfgmgr32)

(cffi:defcfun ("SwDeviceCreate" sw-device-create :convention :stdcall) :int32
  (psz-enumerator-name (:string :encoding :utf-16le))   ; LPCWSTR
  (psz-parent-device-instance (:string :encoding :utf-16le))   ; LPCWSTR
  (p-create-info :pointer)   ; SW_DEVICE_CREATE_INFO*
  (c-property-count :uint32)   ; DWORD
  (p-properties :pointer)   ; DEVPROPERTY* optional
  (p-callback :pointer)   ; SW_DEVICE_CREATE_CALLBACK
  (p-context :pointer)   ; void* optional
  (ph-sw-device :pointer))   ; HSWDEVICE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SwDeviceCreate = Win32::API::More->new('CFGMGR32',
    'int SwDeviceCreate(LPCWSTR pszEnumeratorName, LPCWSTR pszParentDeviceInstance, LPVOID pCreateInfo, DWORD cPropertyCount, LPVOID pProperties, LPVOID pCallback, LPVOID pContext, HANDLE phSwDevice)');
# my $ret = $SwDeviceCreate->Call($pszEnumeratorName, $pszParentDeviceInstance, $pCreateInfo, $cPropertyCount, $pProperties, $pCallback, $pContext, $phSwDevice);
# pszEnumeratorName : LPCWSTR -> LPCWSTR
# pszParentDeviceInstance : LPCWSTR -> LPCWSTR
# pCreateInfo : SW_DEVICE_CREATE_INFO* -> LPVOID
# cPropertyCount : DWORD -> DWORD
# pProperties : DEVPROPERTY* optional -> LPVOID
# pCallback : SW_DEVICE_CREATE_CALLBACK -> LPVOID
# pContext : void* optional -> LPVOID
# phSwDevice : HSWDEVICE* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

使用する型