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

SetupPromptForDiskW

関数
ソースディスクの挿入を求めるダイアログを表示する(Unicode)。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD SetupPromptForDiskW(
    HWND hwndParent,
    LPCWSTR DialogTitle,   // optional
    LPCWSTR DiskName,   // optional
    LPCWSTR PathToSource,   // optional
    LPCWSTR FileSought,
    LPCWSTR TagFile,   // optional
    DWORD DiskPromptStyle,
    LPWSTR PathBuffer,   // optional
    DWORD PathBufferSize,
    DWORD* PathRequiredSize   // optional
);

パラメーター

名前方向説明
hwndParentHWNDinダイアログの親ウィンドウハンドル。NULL可。
DialogTitleLPCWSTRinoptionalダイアログのタイトル文字列(Unicode)。NULLで既定値を使う。
DiskNameLPCWSTRinoptional要求するディスク/メディアの表示名(Unicode)。NULL可。
PathToSourceLPCWSTRinoptionalソースの既定検索パス(Unicode)。NULL可。
FileSoughtLPCWSTRin存在確認の対象ファイル名(Unicode)。
TagFileLPCWSTRinoptionalメディア識別用のタグファイル名(Unicode)。NULL可。
DiskPromptStyleDWORDinダイアログの表示動作を制御するスタイルフラグ。
PathBufferLPWSTRoutoptionalユーザーが選択したソースパスを受け取るバッファ(Unicode)。NULLでサイズ取得のみ可。
PathBufferSizeDWORDinPathBufferのサイズ(文字数)。
PathRequiredSizeDWORD*outoptionalパスに必要なバッファサイズを受け取る出力ポインタ。NULL可。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD SetupPromptForDiskW(
    HWND hwndParent,
    LPCWSTR DialogTitle,   // optional
    LPCWSTR DiskName,   // optional
    LPCWSTR PathToSource,   // optional
    LPCWSTR FileSought,
    LPCWSTR TagFile,   // optional
    DWORD DiskPromptStyle,
    LPWSTR PathBuffer,   // optional
    DWORD PathBufferSize,
    DWORD* PathRequiredSize   // optional
);
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint SetupPromptForDiskW(
    IntPtr hwndParent,   // HWND
    [MarshalAs(UnmanagedType.LPWStr)] string DialogTitle,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string DiskName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string PathToSource,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string FileSought,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string TagFile,   // LPCWSTR optional
    uint DiskPromptStyle,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder PathBuffer,   // LPWSTR optional, out
    uint PathBufferSize,   // DWORD
    IntPtr PathRequiredSize   // DWORD* optional, out
);
<DllImport("SETUPAPI.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetupPromptForDiskW(
    hwndParent As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPWStr)> DialogTitle As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> DiskName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> PathToSource As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> FileSought As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> TagFile As String,   ' LPCWSTR optional
    DiskPromptStyle As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> PathBuffer As System.Text.StringBuilder,   ' LPWSTR optional, out
    PathBufferSize As UInteger,   ' DWORD
    PathRequiredSize As IntPtr   ' DWORD* optional, out
) As UInteger
End Function
' hwndParent : HWND
' DialogTitle : LPCWSTR optional
' DiskName : LPCWSTR optional
' PathToSource : LPCWSTR optional
' FileSought : LPCWSTR
' TagFile : LPCWSTR optional
' DiskPromptStyle : DWORD
' PathBuffer : LPWSTR optional, out
' PathBufferSize : DWORD
' PathRequiredSize : DWORD* optional, out
Declare PtrSafe Function SetupPromptForDiskW Lib "setupapi" ( _
    ByVal hwndParent As LongPtr, _
    ByVal DialogTitle As LongPtr, _
    ByVal DiskName As LongPtr, _
    ByVal PathToSource As LongPtr, _
    ByVal FileSought As LongPtr, _
    ByVal TagFile As LongPtr, _
    ByVal DiskPromptStyle As Long, _
    ByVal PathBuffer As LongPtr, _
    ByVal PathBufferSize As Long, _
    ByVal PathRequiredSize 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

SetupPromptForDiskW = ctypes.windll.setupapi.SetupPromptForDiskW
SetupPromptForDiskW.restype = wintypes.DWORD
SetupPromptForDiskW.argtypes = [
    wintypes.HANDLE,  # hwndParent : HWND
    wintypes.LPCWSTR,  # DialogTitle : LPCWSTR optional
    wintypes.LPCWSTR,  # DiskName : LPCWSTR optional
    wintypes.LPCWSTR,  # PathToSource : LPCWSTR optional
    wintypes.LPCWSTR,  # FileSought : LPCWSTR
    wintypes.LPCWSTR,  # TagFile : LPCWSTR optional
    wintypes.DWORD,  # DiskPromptStyle : DWORD
    wintypes.LPWSTR,  # PathBuffer : LPWSTR optional, out
    wintypes.DWORD,  # PathBufferSize : DWORD
    ctypes.POINTER(wintypes.DWORD),  # PathRequiredSize : DWORD* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SETUPAPI.dll')
SetupPromptForDiskW = Fiddle::Function.new(
  lib['SetupPromptForDiskW'],
  [
    Fiddle::TYPE_VOIDP,  # hwndParent : HWND
    Fiddle::TYPE_VOIDP,  # DialogTitle : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # DiskName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # PathToSource : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # FileSought : LPCWSTR
    Fiddle::TYPE_VOIDP,  # TagFile : LPCWSTR optional
    -Fiddle::TYPE_INT,  # DiskPromptStyle : DWORD
    Fiddle::TYPE_VOIDP,  # PathBuffer : LPWSTR optional, out
    -Fiddle::TYPE_INT,  # PathBufferSize : DWORD
    Fiddle::TYPE_VOIDP,  # PathRequiredSize : DWORD* optional, out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "setupapi")]
extern "system" {
    fn SetupPromptForDiskW(
        hwndParent: *mut core::ffi::c_void,  // HWND
        DialogTitle: *const u16,  // LPCWSTR optional
        DiskName: *const u16,  // LPCWSTR optional
        PathToSource: *const u16,  // LPCWSTR optional
        FileSought: *const u16,  // LPCWSTR
        TagFile: *const u16,  // LPCWSTR optional
        DiskPromptStyle: u32,  // DWORD
        PathBuffer: *mut u16,  // LPWSTR optional, out
        PathBufferSize: u32,  // DWORD
        PathRequiredSize: *mut u32  // DWORD* optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint SetupPromptForDiskW(IntPtr hwndParent, [MarshalAs(UnmanagedType.LPWStr)] string DialogTitle, [MarshalAs(UnmanagedType.LPWStr)] string DiskName, [MarshalAs(UnmanagedType.LPWStr)] string PathToSource, [MarshalAs(UnmanagedType.LPWStr)] string FileSought, [MarshalAs(UnmanagedType.LPWStr)] string TagFile, uint DiskPromptStyle, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder PathBuffer, uint PathBufferSize, IntPtr PathRequiredSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupPromptForDiskW' -Namespace Win32 -PassThru
# $api::SetupPromptForDiskW(hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, PathBuffer, PathBufferSize, PathRequiredSize)
#uselib "SETUPAPI.dll"
#func global SetupPromptForDiskW "SetupPromptForDiskW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; SetupPromptForDiskW hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, varptr(PathBuffer), PathBufferSize, varptr(PathRequiredSize)   ; 戻り値は stat
; hwndParent : HWND -> "wptr"
; DialogTitle : LPCWSTR optional -> "wptr"
; DiskName : LPCWSTR optional -> "wptr"
; PathToSource : LPCWSTR optional -> "wptr"
; FileSought : LPCWSTR -> "wptr"
; TagFile : LPCWSTR optional -> "wptr"
; DiskPromptStyle : DWORD -> "wptr"
; PathBuffer : LPWSTR optional, out -> "wptr"
; PathBufferSize : DWORD -> "wptr"
; PathRequiredSize : DWORD* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupPromptForDiskW "SetupPromptForDiskW" sptr, wstr, wstr, wstr, wstr, wstr, int, var, int, var
; res = SetupPromptForDiskW(hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, PathBuffer, PathBufferSize, PathRequiredSize)
; hwndParent : HWND -> "sptr"
; DialogTitle : LPCWSTR optional -> "wstr"
; DiskName : LPCWSTR optional -> "wstr"
; PathToSource : LPCWSTR optional -> "wstr"
; FileSought : LPCWSTR -> "wstr"
; TagFile : LPCWSTR optional -> "wstr"
; DiskPromptStyle : DWORD -> "int"
; PathBuffer : LPWSTR optional, out -> "var"
; PathBufferSize : DWORD -> "int"
; PathRequiredSize : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD SetupPromptForDiskW(HWND hwndParent, LPCWSTR DialogTitle, LPCWSTR DiskName, LPCWSTR PathToSource, LPCWSTR FileSought, LPCWSTR TagFile, DWORD DiskPromptStyle, LPWSTR PathBuffer, DWORD PathBufferSize, DWORD* PathRequiredSize)
#uselib "SETUPAPI.dll"
#cfunc global SetupPromptForDiskW "SetupPromptForDiskW" intptr, wstr, wstr, wstr, wstr, wstr, int, var, int, var
; res = SetupPromptForDiskW(hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, PathBuffer, PathBufferSize, PathRequiredSize)
; hwndParent : HWND -> "intptr"
; DialogTitle : LPCWSTR optional -> "wstr"
; DiskName : LPCWSTR optional -> "wstr"
; PathToSource : LPCWSTR optional -> "wstr"
; FileSought : LPCWSTR -> "wstr"
; TagFile : LPCWSTR optional -> "wstr"
; DiskPromptStyle : DWORD -> "int"
; PathBuffer : LPWSTR optional, out -> "var"
; PathBufferSize : DWORD -> "int"
; PathRequiredSize : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupPromptForDiskW = setupapi.NewProc("SetupPromptForDiskW")
)

// hwndParent (HWND), DialogTitle (LPCWSTR optional), DiskName (LPCWSTR optional), PathToSource (LPCWSTR optional), FileSought (LPCWSTR), TagFile (LPCWSTR optional), DiskPromptStyle (DWORD), PathBuffer (LPWSTR optional, out), PathBufferSize (DWORD), PathRequiredSize (DWORD* optional, out)
r1, _, err := procSetupPromptForDiskW.Call(
	uintptr(hwndParent),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DialogTitle))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DiskName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(PathToSource))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(FileSought))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(TagFile))),
	uintptr(DiskPromptStyle),
	uintptr(PathBuffer),
	uintptr(PathBufferSize),
	uintptr(PathRequiredSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SetupPromptForDiskW(
  hwndParent: THandle;   // HWND
  DialogTitle: PWideChar;   // LPCWSTR optional
  DiskName: PWideChar;   // LPCWSTR optional
  PathToSource: PWideChar;   // LPCWSTR optional
  FileSought: PWideChar;   // LPCWSTR
  TagFile: PWideChar;   // LPCWSTR optional
  DiskPromptStyle: DWORD;   // DWORD
  PathBuffer: PWideChar;   // LPWSTR optional, out
  PathBufferSize: DWORD;   // DWORD
  PathRequiredSize: Pointer   // DWORD* optional, out
): DWORD; stdcall;
  external 'SETUPAPI.dll' name 'SetupPromptForDiskW';
result := DllCall("SETUPAPI\SetupPromptForDiskW"
    , "Ptr", hwndParent   ; HWND
    , "WStr", DialogTitle   ; LPCWSTR optional
    , "WStr", DiskName   ; LPCWSTR optional
    , "WStr", PathToSource   ; LPCWSTR optional
    , "WStr", FileSought   ; LPCWSTR
    , "WStr", TagFile   ; LPCWSTR optional
    , "UInt", DiskPromptStyle   ; DWORD
    , "Ptr", PathBuffer   ; LPWSTR optional, out
    , "UInt", PathBufferSize   ; DWORD
    , "Ptr", PathRequiredSize   ; DWORD* optional, out
    , "UInt")   ; return: DWORD
●SetupPromptForDiskW(hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, PathBuffer, PathBufferSize, PathRequiredSize) = DLL("SETUPAPI.dll", "dword SetupPromptForDiskW(void*, char*, char*, char*, char*, char*, dword, char*, dword, void*)")
# 呼び出し: SetupPromptForDiskW(hwndParent, DialogTitle, DiskName, PathToSource, FileSought, TagFile, DiskPromptStyle, PathBuffer, PathBufferSize, PathRequiredSize)
# hwndParent : HWND -> "void*"
# DialogTitle : LPCWSTR optional -> "char*"
# DiskName : LPCWSTR optional -> "char*"
# PathToSource : LPCWSTR optional -> "char*"
# FileSought : LPCWSTR -> "char*"
# TagFile : LPCWSTR optional -> "char*"
# DiskPromptStyle : DWORD -> "dword"
# PathBuffer : LPWSTR optional, out -> "char*"
# PathBufferSize : DWORD -> "dword"
# PathRequiredSize : DWORD* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef SetupPromptForDiskWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32, Pointer<Uint32>);
typedef SetupPromptForDiskWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Utf16>, int, Pointer<Uint32>);
final SetupPromptForDiskW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupPromptForDiskWNative, SetupPromptForDiskWDart>('SetupPromptForDiskW');
// hwndParent : HWND -> Pointer<Void>
// DialogTitle : LPCWSTR optional -> Pointer<Utf16>
// DiskName : LPCWSTR optional -> Pointer<Utf16>
// PathToSource : LPCWSTR optional -> Pointer<Utf16>
// FileSought : LPCWSTR -> Pointer<Utf16>
// TagFile : LPCWSTR optional -> Pointer<Utf16>
// DiskPromptStyle : DWORD -> Uint32
// PathBuffer : LPWSTR optional, out -> Pointer<Utf16>
// PathBufferSize : DWORD -> Uint32
// PathRequiredSize : DWORD* optional, out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupPromptForDiskW(
  hwndParent: THandle;   // HWND
  DialogTitle: PWideChar;   // LPCWSTR optional
  DiskName: PWideChar;   // LPCWSTR optional
  PathToSource: PWideChar;   // LPCWSTR optional
  FileSought: PWideChar;   // LPCWSTR
  TagFile: PWideChar;   // LPCWSTR optional
  DiskPromptStyle: DWORD;   // DWORD
  PathBuffer: PWideChar;   // LPWSTR optional, out
  PathBufferSize: DWORD;   // DWORD
  PathRequiredSize: Pointer   // DWORD* optional, out
): DWORD; stdcall;
  external 'SETUPAPI.dll' name 'SetupPromptForDiskW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupPromptForDiskW"
  c_SetupPromptForDiskW :: Ptr () -> CWString -> CWString -> CWString -> CWString -> CWString -> Word32 -> CWString -> Word32 -> Ptr Word32 -> IO Word32
-- hwndParent : HWND -> Ptr ()
-- DialogTitle : LPCWSTR optional -> CWString
-- DiskName : LPCWSTR optional -> CWString
-- PathToSource : LPCWSTR optional -> CWString
-- FileSought : LPCWSTR -> CWString
-- TagFile : LPCWSTR optional -> CWString
-- DiskPromptStyle : DWORD -> Word32
-- PathBuffer : LPWSTR optional, out -> CWString
-- PathBufferSize : DWORD -> Word32
-- PathRequiredSize : DWORD* optional, out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setuppromptfordiskw =
  foreign "SetupPromptForDiskW"
    ((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint32_t) @-> returning uint32_t)
(* hwndParent : HWND -> (ptr void) *)
(* DialogTitle : LPCWSTR optional -> (ptr uint16_t) *)
(* DiskName : LPCWSTR optional -> (ptr uint16_t) *)
(* PathToSource : LPCWSTR optional -> (ptr uint16_t) *)
(* FileSought : LPCWSTR -> (ptr uint16_t) *)
(* TagFile : LPCWSTR optional -> (ptr uint16_t) *)
(* DiskPromptStyle : DWORD -> uint32_t *)
(* PathBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* PathBufferSize : DWORD -> uint32_t *)
(* PathRequiredSize : DWORD* optional, out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupPromptForDiskW" setup-prompt-for-disk-w :convention :stdcall) :uint32
  (hwnd-parent :pointer)   ; HWND
  (dialog-title (:string :encoding :utf-16le))   ; LPCWSTR optional
  (disk-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (path-to-source (:string :encoding :utf-16le))   ; LPCWSTR optional
  (file-sought (:string :encoding :utf-16le))   ; LPCWSTR
  (tag-file (:string :encoding :utf-16le))   ; LPCWSTR optional
  (disk-prompt-style :uint32)   ; DWORD
  (path-buffer :pointer)   ; LPWSTR optional, out
  (path-buffer-size :uint32)   ; DWORD
  (path-required-size :pointer))   ; DWORD* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupPromptForDiskW = Win32::API::More->new('SETUPAPI',
    'DWORD SetupPromptForDiskW(HANDLE hwndParent, LPCWSTR DialogTitle, LPCWSTR DiskName, LPCWSTR PathToSource, LPCWSTR FileSought, LPCWSTR TagFile, DWORD DiskPromptStyle, LPWSTR PathBuffer, DWORD PathBufferSize, LPVOID PathRequiredSize)');
# my $ret = $SetupPromptForDiskW->Call($hwndParent, $DialogTitle, $DiskName, $PathToSource, $FileSought, $TagFile, $DiskPromptStyle, $PathBuffer, $PathBufferSize, $PathRequiredSize);
# hwndParent : HWND -> HANDLE
# DialogTitle : LPCWSTR optional -> LPCWSTR
# DiskName : LPCWSTR optional -> LPCWSTR
# PathToSource : LPCWSTR optional -> LPCWSTR
# FileSought : LPCWSTR -> LPCWSTR
# TagFile : LPCWSTR optional -> LPCWSTR
# DiskPromptStyle : DWORD -> DWORD
# PathBuffer : LPWSTR optional, out -> LPWSTR
# PathBufferSize : DWORD -> DWORD
# PathRequiredSize : DWORD* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い