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

SetupInstallFileExW

関数
使用中状態の通知付きで単一ファイルをインストールする(Unicode)。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL SetupInstallFileExW(
    void* InfHandle,   // optional
    INFCONTEXT* InfContext,   // optional
    LPCWSTR SourceFile,   // optional
    LPCWSTR SourcePathRoot,   // optional
    LPCWSTR DestinationName,   // optional
    SP_COPY_STYLE CopyStyle,
    PSP_FILE_CALLBACK_W CopyMsgHandler,   // optional
    void* Context,   // optional
    BOOL* FileWasInUse
);

パラメーター

名前方向説明
InfHandlevoid*inoptional対象の開いているINFハンドル。NULL可。
InfContextINFCONTEXT*inoptionalコピー情報を含む行を示すINFCONTEXT構造体へのポインタ。NULL可。
SourceFileLPCWSTRinoptionalコピー元ファイル名(Unicode)。InfContext使用時はNULL可。
SourcePathRootLPCWSTRinoptionalソースのルートパス(Unicode)。NULL可。
DestinationNameLPCWSTRinoptionalコピー先のファイル名(Unicode)。NULLで元名を使う。
CopyStyleSP_COPY_STYLEinコピー動作を制御するスタイルフラグ。上書きやバージョン確認等を指定する。
CopyMsgHandlerPSP_FILE_CALLBACK_Winoptionalコピー進行を通知するコールバック関数。NULL可。
Contextvoid*inoptionalコールバックへ渡す任意のユーザーコンテキスト。NULL可。
FileWasInUseBOOL*out対象ファイルが使用中で再起動が必要かを受け取る出力ポインタ。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetupInstallFileExW(
    void* InfHandle,   // optional
    INFCONTEXT* InfContext,   // optional
    LPCWSTR SourceFile,   // optional
    LPCWSTR SourcePathRoot,   // optional
    LPCWSTR DestinationName,   // optional
    SP_COPY_STYLE CopyStyle,
    PSP_FILE_CALLBACK_W CopyMsgHandler,   // optional
    void* Context,   // optional
    BOOL* FileWasInUse
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetupInstallFileExW(
    IntPtr InfHandle,   // void* optional
    IntPtr InfContext,   // INFCONTEXT* optional
    [MarshalAs(UnmanagedType.LPWStr)] string SourceFile,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string SourcePathRoot,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string DestinationName,   // LPCWSTR optional
    uint CopyStyle,   // SP_COPY_STYLE
    IntPtr CopyMsgHandler,   // PSP_FILE_CALLBACK_W optional
    IntPtr Context,   // void* optional
    out bool FileWasInUse   // BOOL* out
);
<DllImport("SETUPAPI.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetupInstallFileExW(
    InfHandle As IntPtr,   ' void* optional
    InfContext As IntPtr,   ' INFCONTEXT* optional
    <MarshalAs(UnmanagedType.LPWStr)> SourceFile As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> SourcePathRoot As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> DestinationName As String,   ' LPCWSTR optional
    CopyStyle As UInteger,   ' SP_COPY_STYLE
    CopyMsgHandler As IntPtr,   ' PSP_FILE_CALLBACK_W optional
    Context As IntPtr,   ' void* optional
    <Out> ByRef FileWasInUse As Boolean   ' BOOL* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' InfHandle : void* optional
' InfContext : INFCONTEXT* optional
' SourceFile : LPCWSTR optional
' SourcePathRoot : LPCWSTR optional
' DestinationName : LPCWSTR optional
' CopyStyle : SP_COPY_STYLE
' CopyMsgHandler : PSP_FILE_CALLBACK_W optional
' Context : void* optional
' FileWasInUse : BOOL* out
Declare PtrSafe Function SetupInstallFileExW Lib "setupapi" ( _
    ByVal InfHandle As LongPtr, _
    ByVal InfContext As LongPtr, _
    ByVal SourceFile As LongPtr, _
    ByVal SourcePathRoot As LongPtr, _
    ByVal DestinationName As LongPtr, _
    ByVal CopyStyle As Long, _
    ByVal CopyMsgHandler As LongPtr, _
    ByVal Context As LongPtr, _
    ByRef FileWasInUse As Long) 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

SetupInstallFileExW = ctypes.windll.setupapi.SetupInstallFileExW
SetupInstallFileExW.restype = wintypes.BOOL
SetupInstallFileExW.argtypes = [
    ctypes.POINTER(None),  # InfHandle : void* optional
    ctypes.c_void_p,  # InfContext : INFCONTEXT* optional
    wintypes.LPCWSTR,  # SourceFile : LPCWSTR optional
    wintypes.LPCWSTR,  # SourcePathRoot : LPCWSTR optional
    wintypes.LPCWSTR,  # DestinationName : LPCWSTR optional
    wintypes.DWORD,  # CopyStyle : SP_COPY_STYLE
    ctypes.c_void_p,  # CopyMsgHandler : PSP_FILE_CALLBACK_W optional
    ctypes.POINTER(None),  # Context : void* optional
    ctypes.c_void_p,  # FileWasInUse : BOOL* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupInstallFileExW = setupapi.NewProc("SetupInstallFileExW")
)

// InfHandle (void* optional), InfContext (INFCONTEXT* optional), SourceFile (LPCWSTR optional), SourcePathRoot (LPCWSTR optional), DestinationName (LPCWSTR optional), CopyStyle (SP_COPY_STYLE), CopyMsgHandler (PSP_FILE_CALLBACK_W optional), Context (void* optional), FileWasInUse (BOOL* out)
r1, _, err := procSetupInstallFileExW.Call(
	uintptr(InfHandle),
	uintptr(InfContext),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(SourceFile))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(SourcePathRoot))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DestinationName))),
	uintptr(CopyStyle),
	uintptr(CopyMsgHandler),
	uintptr(Context),
	uintptr(FileWasInUse),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetupInstallFileExW(
  InfHandle: Pointer;   // void* optional
  InfContext: Pointer;   // INFCONTEXT* optional
  SourceFile: PWideChar;   // LPCWSTR optional
  SourcePathRoot: PWideChar;   // LPCWSTR optional
  DestinationName: PWideChar;   // LPCWSTR optional
  CopyStyle: DWORD;   // SP_COPY_STYLE
  CopyMsgHandler: Pointer;   // PSP_FILE_CALLBACK_W optional
  Context: Pointer;   // void* optional
  FileWasInUse: Pointer   // BOOL* out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupInstallFileExW';
result := DllCall("SETUPAPI\SetupInstallFileExW"
    , "Ptr", InfHandle   ; void* optional
    , "Ptr", InfContext   ; INFCONTEXT* optional
    , "WStr", SourceFile   ; LPCWSTR optional
    , "WStr", SourcePathRoot   ; LPCWSTR optional
    , "WStr", DestinationName   ; LPCWSTR optional
    , "UInt", CopyStyle   ; SP_COPY_STYLE
    , "Ptr", CopyMsgHandler   ; PSP_FILE_CALLBACK_W optional
    , "Ptr", Context   ; void* optional
    , "Ptr", FileWasInUse   ; BOOL* out
    , "Int")   ; return: BOOL
●SetupInstallFileExW(InfHandle, InfContext, SourceFile, SourcePathRoot, DestinationName, CopyStyle, CopyMsgHandler, Context, FileWasInUse) = DLL("SETUPAPI.dll", "bool SetupInstallFileExW(void*, void*, char*, char*, char*, dword, void*, void*, void*)")
# 呼び出し: SetupInstallFileExW(InfHandle, InfContext, SourceFile, SourcePathRoot, DestinationName, CopyStyle, CopyMsgHandler, Context, FileWasInUse)
# InfHandle : void* optional -> "void*"
# InfContext : INFCONTEXT* optional -> "void*"
# SourceFile : LPCWSTR optional -> "char*"
# SourcePathRoot : LPCWSTR optional -> "char*"
# DestinationName : LPCWSTR optional -> "char*"
# CopyStyle : SP_COPY_STYLE -> "dword"
# CopyMsgHandler : PSP_FILE_CALLBACK_W optional -> "void*"
# Context : void* optional -> "void*"
# FileWasInUse : BOOL* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef SetupInstallFileExWNative = Int32 Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Int32>);
typedef SetupInstallFileExWDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Void>, Pointer<Void>, Pointer<Int32>);
final SetupInstallFileExW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupInstallFileExWNative, SetupInstallFileExWDart>('SetupInstallFileExW');
// InfHandle : void* optional -> Pointer<Void>
// InfContext : INFCONTEXT* optional -> Pointer<Void>
// SourceFile : LPCWSTR optional -> Pointer<Utf16>
// SourcePathRoot : LPCWSTR optional -> Pointer<Utf16>
// DestinationName : LPCWSTR optional -> Pointer<Utf16>
// CopyStyle : SP_COPY_STYLE -> Uint32
// CopyMsgHandler : PSP_FILE_CALLBACK_W optional -> Pointer<Void>
// Context : void* optional -> Pointer<Void>
// FileWasInUse : BOOL* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupInstallFileExW(
  InfHandle: Pointer;   // void* optional
  InfContext: Pointer;   // INFCONTEXT* optional
  SourceFile: PWideChar;   // LPCWSTR optional
  SourcePathRoot: PWideChar;   // LPCWSTR optional
  DestinationName: PWideChar;   // LPCWSTR optional
  CopyStyle: DWORD;   // SP_COPY_STYLE
  CopyMsgHandler: Pointer;   // PSP_FILE_CALLBACK_W optional
  Context: Pointer;   // void* optional
  FileWasInUse: Pointer   // BOOL* out
): BOOL; stdcall;
  external 'SETUPAPI.dll' name 'SetupInstallFileExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupInstallFileExW"
  c_SetupInstallFileExW :: Ptr () -> Ptr () -> CWString -> CWString -> CWString -> Word32 -> Ptr () -> Ptr () -> Ptr CInt -> IO CInt
-- InfHandle : void* optional -> Ptr ()
-- InfContext : INFCONTEXT* optional -> Ptr ()
-- SourceFile : LPCWSTR optional -> CWString
-- SourcePathRoot : LPCWSTR optional -> CWString
-- DestinationName : LPCWSTR optional -> CWString
-- CopyStyle : SP_COPY_STYLE -> Word32
-- CopyMsgHandler : PSP_FILE_CALLBACK_W optional -> Ptr ()
-- Context : void* optional -> Ptr ()
-- FileWasInUse : BOOL* out -> Ptr CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setupinstallfileexw =
  foreign "SetupInstallFileExW"
    ((ptr void) @-> (ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr int32_t) @-> returning int32_t)
(* InfHandle : void* optional -> (ptr void) *)
(* InfContext : INFCONTEXT* optional -> (ptr void) *)
(* SourceFile : LPCWSTR optional -> (ptr uint16_t) *)
(* SourcePathRoot : LPCWSTR optional -> (ptr uint16_t) *)
(* DestinationName : LPCWSTR optional -> (ptr uint16_t) *)
(* CopyStyle : SP_COPY_STYLE -> uint32_t *)
(* CopyMsgHandler : PSP_FILE_CALLBACK_W optional -> (ptr void) *)
(* Context : void* optional -> (ptr void) *)
(* FileWasInUse : BOOL* out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupInstallFileExW" setup-install-file-ex-w :convention :stdcall) :int32
  (inf-handle :pointer)   ; void* optional
  (inf-context :pointer)   ; INFCONTEXT* optional
  (source-file (:string :encoding :utf-16le))   ; LPCWSTR optional
  (source-path-root (:string :encoding :utf-16le))   ; LPCWSTR optional
  (destination-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (copy-style :uint32)   ; SP_COPY_STYLE
  (copy-msg-handler :pointer)   ; PSP_FILE_CALLBACK_W optional
  (context :pointer)   ; void* optional
  (file-was-in-use :pointer))   ; BOOL* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupInstallFileExW = Win32::API::More->new('SETUPAPI',
    'BOOL SetupInstallFileExW(LPVOID InfHandle, LPVOID InfContext, LPCWSTR SourceFile, LPCWSTR SourcePathRoot, LPCWSTR DestinationName, DWORD CopyStyle, LPVOID CopyMsgHandler, LPVOID Context, LPVOID FileWasInUse)');
# my $ret = $SetupInstallFileExW->Call($InfHandle, $InfContext, $SourceFile, $SourcePathRoot, $DestinationName, $CopyStyle, $CopyMsgHandler, $Context, $FileWasInUse);
# InfHandle : void* optional -> LPVOID
# InfContext : INFCONTEXT* optional -> LPVOID
# SourceFile : LPCWSTR optional -> LPCWSTR
# SourcePathRoot : LPCWSTR optional -> LPCWSTR
# DestinationName : LPCWSTR optional -> LPCWSTR
# CopyStyle : SP_COPY_STYLE -> DWORD
# CopyMsgHandler : PSP_FILE_CALLBACK_W optional -> LPVOID
# Context : void* optional -> LPVOID
# FileWasInUse : BOOL* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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