Win32 API 日本語リファレンス
ホームSystem.ApplicationInstallationAndServicing › MsiEnumComponentCostsW

MsiEnumComponentCostsW

関数
コンポーネントのドライブ別ディスクコストを列挙する(Unicode版)。
DLLmsi.dll文字セットUnicode (-W)呼出規約winapi対応OSwindows8.0

シグネチャ

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

DWORD MsiEnumComponentCostsW(
    MSIHANDLE hInstall,
    LPCWSTR szComponent,
    DWORD dwIndex,
    INSTALLSTATE iState,
    LPWSTR szDriveBuf,
    DWORD* pcchDriveBuf,
    INT* piCost,
    INT* piTempCost
);

パラメーター

名前方向説明
hInstallMSIHANDLEinDLL カスタムアクションに提供された、または MsiOpenPackageMsiOpenPackageExMsiOpenProduct から取得したインストールへのハンドル。
szComponentLPCWSTRinComponent テーブルの Component 列に列挙されているとおりのコンポーネント名を指定する、null で終わる文字列。このパラメーターは null を指定できます。szComponent が null または空文字列の場合、 MsiEnumComponentCosts はインストール中に使用されるドライブごとの合計ディスク領域を列挙します。この場合、iState は無視されます。インストーラーのコストには、セキュアフォルダーへのデータベースのキャッシュに要するコストや、インストールスクリプトの作成に要するコストが含まれます。インストール中に使用される合計ディスク領域は、コンポーネントのインストール後に使用される領域より大きくなる場合がある点に注意してください。
dwIndexDWORDinドライブの 0 から始まるインデックス。このパラメーターは、 MsiEnumComponentCosts 関数の最初の呼び出しでは 0 とし、以降の呼び出しでは増分する必要があります。
iStateINSTALLSTATEin列挙対象として要求されるコンポーネントの状態。szComponent に Null または空文字列を渡した場合、インストーラーは iState パラメーターを無視します。
szDriveBufLPWSTRoutnull 終端文字を含むドライブ名を保持するバッファー。エラーが発生した場合は空文字列になります。
pcchDriveBufDWORD*inoutlpDriveBuf パラメーターが指すバッファーのサイズを TCHAR 単位で指定する変数へのポインター。このサイズには終端の null 文字を含める必要があります。指定されたバッファーが小さすぎる場合、pcchDriveBuf が指す変数には null 終端文字を含まない文字数が格納されます。
piCostINT*outドライブごとのコンポーネントのコスト。512 バイトの倍数で表されます。エラーが発生した場合、この値は 0 になります。piCost で返される値は、インストール後にコンポーネントが使用する最終的なディスク領域です。szComponent に Null または空文字列を渡した場合、インストーラーは piCost の値を 0 に設定します。
piTempCostINT*outインストール期間中のドライブごとのコンポーネントコスト。エラーが発生した場合は 0 です。*piTempCost の値は、インストール期間中に必要となる一時的な領域要件を表します。この一時的な領域要件は、インストール期間中のみ必要となる領域です。これは最終的なディスク領域要件には影響しません。

戻り値の型: DWORD

公式ドキュメント

MsiEnumComponentCosts 関数は、コンポーネントのインストールに必要なディスク領域をドライブごとに列挙します。(Unicode)

戻り値

戻り値 意味
ERROR_INVALID_HANDLE_STATE
構成データが破損しています。
ERROR_INVALID_PARAMETER
無効なパラメーターが関数に渡されました。
ERROR_NO_MORE_ITEMS
返すドライブがこれ以上ありません。
ERROR_SUCCESS
値が列挙されました。
ERROR_UNKNOWN_COMPONENT
コンポーネントが見つかりません。
ERROR_FUNCTION_NOT_CALLED
コスト計算が完了していません。
ERROR_MORE_DATA
ドライブ名に対してバッファーが十分な大きさではありません。
ERROR_INVALID_HANDLE
指定されたハンドルが無効または非アクティブです。

解説(Remarks)

ドライブごとのディスク領域コストを列挙する推奨方法は次のとおりです。dwIndex を 0 に設定して開始し、呼び出しごとに 1 ずつ増分します。 MsiEnumComponentCostsERROR_SUCCESS を返す限り、列挙を続行します。

MsiEnumComponentCosts はカスタムアクションから呼び出すことができます。

インストールの最終的な合計ディスクコストは、すべてのコンポーネントのコストの合計に、Windows インストーラーのコスト(szComponent = null)を加えたものです。

メモ

msiquery.h ヘッダーは、MsiEnumComponentCosts を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

DWORD MsiEnumComponentCostsW(
    MSIHANDLE hInstall,
    LPCWSTR szComponent,
    DWORD dwIndex,
    INSTALLSTATE iState,
    LPWSTR szDriveBuf,
    DWORD* pcchDriveBuf,
    INT* piCost,
    INT* piTempCost
);
[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint MsiEnumComponentCostsW(
    uint hInstall,   // MSIHANDLE
    [MarshalAs(UnmanagedType.LPWStr)] string szComponent,   // LPCWSTR
    uint dwIndex,   // DWORD
    int iState,   // INSTALLSTATE
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szDriveBuf,   // LPWSTR out
    ref uint pcchDriveBuf,   // DWORD* in/out
    out int piCost,   // INT* out
    out int piTempCost   // INT* out
);
<DllImport("msi.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MsiEnumComponentCostsW(
    hInstall As UInteger,   ' MSIHANDLE
    <MarshalAs(UnmanagedType.LPWStr)> szComponent As String,   ' LPCWSTR
    dwIndex As UInteger,   ' DWORD
    iState As Integer,   ' INSTALLSTATE
    <MarshalAs(UnmanagedType.LPWStr)> szDriveBuf As System.Text.StringBuilder,   ' LPWSTR out
    ByRef pcchDriveBuf As UInteger,   ' DWORD* in/out
    <Out> ByRef piCost As Integer,   ' INT* out
    <Out> ByRef piTempCost As Integer   ' INT* out
) As UInteger
End Function
' hInstall : MSIHANDLE
' szComponent : LPCWSTR
' dwIndex : DWORD
' iState : INSTALLSTATE
' szDriveBuf : LPWSTR out
' pcchDriveBuf : DWORD* in/out
' piCost : INT* out
' piTempCost : INT* out
Declare PtrSafe Function MsiEnumComponentCostsW Lib "msi" ( _
    ByVal hInstall As Long, _
    ByVal szComponent As LongPtr, _
    ByVal dwIndex As Long, _
    ByVal iState As Long, _
    ByVal szDriveBuf As LongPtr, _
    ByRef pcchDriveBuf As Long, _
    ByRef piCost As Long, _
    ByRef piTempCost 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

MsiEnumComponentCostsW = ctypes.windll.msi.MsiEnumComponentCostsW
MsiEnumComponentCostsW.restype = wintypes.DWORD
MsiEnumComponentCostsW.argtypes = [
    wintypes.DWORD,  # hInstall : MSIHANDLE
    wintypes.LPCWSTR,  # szComponent : LPCWSTR
    wintypes.DWORD,  # dwIndex : DWORD
    ctypes.c_int,  # iState : INSTALLSTATE
    wintypes.LPWSTR,  # szDriveBuf : LPWSTR out
    ctypes.POINTER(wintypes.DWORD),  # pcchDriveBuf : DWORD* in/out
    ctypes.POINTER(ctypes.c_int),  # piCost : INT* out
    ctypes.POINTER(ctypes.c_int),  # piTempCost : INT* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('msi.dll')
MsiEnumComponentCostsW = Fiddle::Function.new(
  lib['MsiEnumComponentCostsW'],
  [
    -Fiddle::TYPE_INT,  # hInstall : MSIHANDLE
    Fiddle::TYPE_VOIDP,  # szComponent : LPCWSTR
    -Fiddle::TYPE_INT,  # dwIndex : DWORD
    Fiddle::TYPE_INT,  # iState : INSTALLSTATE
    Fiddle::TYPE_VOIDP,  # szDriveBuf : LPWSTR out
    Fiddle::TYPE_VOIDP,  # pcchDriveBuf : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # piCost : INT* out
    Fiddle::TYPE_VOIDP,  # piTempCost : INT* out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "msi")]
extern "system" {
    fn MsiEnumComponentCostsW(
        hInstall: u32,  // MSIHANDLE
        szComponent: *const u16,  // LPCWSTR
        dwIndex: u32,  // DWORD
        iState: i32,  // INSTALLSTATE
        szDriveBuf: *mut u16,  // LPWSTR out
        pcchDriveBuf: *mut u32,  // DWORD* in/out
        piCost: *mut i32,  // INT* out
        piTempCost: *mut i32  // INT* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
public static extern uint MsiEnumComponentCostsW(uint hInstall, [MarshalAs(UnmanagedType.LPWStr)] string szComponent, uint dwIndex, int iState, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szDriveBuf, ref uint pcchDriveBuf, out int piCost, out int piTempCost);
"@
$api = Add-Type -MemberDefinition $sig -Name 'msi_MsiEnumComponentCostsW' -Namespace Win32 -PassThru
# $api::MsiEnumComponentCostsW(hInstall, szComponent, dwIndex, iState, szDriveBuf, pcchDriveBuf, piCost, piTempCost)
#uselib "msi.dll"
#func global MsiEnumComponentCostsW "MsiEnumComponentCostsW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; MsiEnumComponentCostsW hInstall, szComponent, dwIndex, iState, varptr(szDriveBuf), varptr(pcchDriveBuf), varptr(piCost), varptr(piTempCost)   ; 戻り値は stat
; hInstall : MSIHANDLE -> "wptr"
; szComponent : LPCWSTR -> "wptr"
; dwIndex : DWORD -> "wptr"
; iState : INSTALLSTATE -> "wptr"
; szDriveBuf : LPWSTR out -> "wptr"
; pcchDriveBuf : DWORD* in/out -> "wptr"
; piCost : INT* out -> "wptr"
; piTempCost : INT* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "msi.dll"
#cfunc global MsiEnumComponentCostsW "MsiEnumComponentCostsW" int, wstr, int, int, var, var, var, var
; res = MsiEnumComponentCostsW(hInstall, szComponent, dwIndex, iState, szDriveBuf, pcchDriveBuf, piCost, piTempCost)
; hInstall : MSIHANDLE -> "int"
; szComponent : LPCWSTR -> "wstr"
; dwIndex : DWORD -> "int"
; iState : INSTALLSTATE -> "int"
; szDriveBuf : LPWSTR out -> "var"
; pcchDriveBuf : DWORD* in/out -> "var"
; piCost : INT* out -> "var"
; piTempCost : INT* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD MsiEnumComponentCostsW(MSIHANDLE hInstall, LPCWSTR szComponent, DWORD dwIndex, INSTALLSTATE iState, LPWSTR szDriveBuf, DWORD* pcchDriveBuf, INT* piCost, INT* piTempCost)
#uselib "msi.dll"
#cfunc global MsiEnumComponentCostsW "MsiEnumComponentCostsW" int, wstr, int, int, var, var, var, var
; res = MsiEnumComponentCostsW(hInstall, szComponent, dwIndex, iState, szDriveBuf, pcchDriveBuf, piCost, piTempCost)
; hInstall : MSIHANDLE -> "int"
; szComponent : LPCWSTR -> "wstr"
; dwIndex : DWORD -> "int"
; iState : INSTALLSTATE -> "int"
; szDriveBuf : LPWSTR out -> "var"
; pcchDriveBuf : DWORD* in/out -> "var"
; piCost : INT* out -> "var"
; piTempCost : INT* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	msi = windows.NewLazySystemDLL("msi.dll")
	procMsiEnumComponentCostsW = msi.NewProc("MsiEnumComponentCostsW")
)

// hInstall (MSIHANDLE), szComponent (LPCWSTR), dwIndex (DWORD), iState (INSTALLSTATE), szDriveBuf (LPWSTR out), pcchDriveBuf (DWORD* in/out), piCost (INT* out), piTempCost (INT* out)
r1, _, err := procMsiEnumComponentCostsW.Call(
	uintptr(hInstall),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szComponent))),
	uintptr(dwIndex),
	uintptr(iState),
	uintptr(szDriveBuf),
	uintptr(pcchDriveBuf),
	uintptr(piCost),
	uintptr(piTempCost),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function MsiEnumComponentCostsW(
  hInstall: DWORD;   // MSIHANDLE
  szComponent: PWideChar;   // LPCWSTR
  dwIndex: DWORD;   // DWORD
  iState: Integer;   // INSTALLSTATE
  szDriveBuf: PWideChar;   // LPWSTR out
  pcchDriveBuf: Pointer;   // DWORD* in/out
  piCost: Pointer;   // INT* out
  piTempCost: Pointer   // INT* out
): DWORD; stdcall;
  external 'msi.dll' name 'MsiEnumComponentCostsW';
result := DllCall("msi\MsiEnumComponentCostsW"
    , "UInt", hInstall   ; MSIHANDLE
    , "WStr", szComponent   ; LPCWSTR
    , "UInt", dwIndex   ; DWORD
    , "Int", iState   ; INSTALLSTATE
    , "Ptr", szDriveBuf   ; LPWSTR out
    , "Ptr", pcchDriveBuf   ; DWORD* in/out
    , "Ptr", piCost   ; INT* out
    , "Ptr", piTempCost   ; INT* out
    , "UInt")   ; return: DWORD
●MsiEnumComponentCostsW(hInstall, szComponent, dwIndex, iState, szDriveBuf, pcchDriveBuf, piCost, piTempCost) = DLL("msi.dll", "dword MsiEnumComponentCostsW(dword, char*, dword, int, char*, void*, void*, void*)")
# 呼び出し: MsiEnumComponentCostsW(hInstall, szComponent, dwIndex, iState, szDriveBuf, pcchDriveBuf, piCost, piTempCost)
# hInstall : MSIHANDLE -> "dword"
# szComponent : LPCWSTR -> "char*"
# dwIndex : DWORD -> "dword"
# iState : INSTALLSTATE -> "int"
# szDriveBuf : LPWSTR out -> "char*"
# pcchDriveBuf : DWORD* in/out -> "void*"
# piCost : INT* out -> "void*"
# piTempCost : INT* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef MsiEnumComponentCostsWNative = Uint32 Function(Uint32, Pointer<Utf16>, Uint32, Int32, Pointer<Utf16>, Pointer<Uint32>, Pointer<Int32>, Pointer<Int32>);
typedef MsiEnumComponentCostsWDart = int Function(int, Pointer<Utf16>, int, int, Pointer<Utf16>, Pointer<Uint32>, Pointer<Int32>, Pointer<Int32>);
final MsiEnumComponentCostsW = DynamicLibrary.open('msi.dll')
    .lookupFunction<MsiEnumComponentCostsWNative, MsiEnumComponentCostsWDart>('MsiEnumComponentCostsW');
// hInstall : MSIHANDLE -> Uint32
// szComponent : LPCWSTR -> Pointer<Utf16>
// dwIndex : DWORD -> Uint32
// iState : INSTALLSTATE -> Int32
// szDriveBuf : LPWSTR out -> Pointer<Utf16>
// pcchDriveBuf : DWORD* in/out -> Pointer<Uint32>
// piCost : INT* out -> Pointer<Int32>
// piTempCost : INT* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MsiEnumComponentCostsW(
  hInstall: DWORD;   // MSIHANDLE
  szComponent: PWideChar;   // LPCWSTR
  dwIndex: DWORD;   // DWORD
  iState: Integer;   // INSTALLSTATE
  szDriveBuf: PWideChar;   // LPWSTR out
  pcchDriveBuf: Pointer;   // DWORD* in/out
  piCost: Pointer;   // INT* out
  piTempCost: Pointer   // INT* out
): DWORD; stdcall;
  external 'msi.dll' name 'MsiEnumComponentCostsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MsiEnumComponentCostsW"
  c_MsiEnumComponentCostsW :: Word32 -> CWString -> Word32 -> Int32 -> CWString -> Ptr Word32 -> Ptr Int32 -> Ptr Int32 -> IO Word32
-- hInstall : MSIHANDLE -> Word32
-- szComponent : LPCWSTR -> CWString
-- dwIndex : DWORD -> Word32
-- iState : INSTALLSTATE -> Int32
-- szDriveBuf : LPWSTR out -> CWString
-- pcchDriveBuf : DWORD* in/out -> Ptr Word32
-- piCost : INT* out -> Ptr Int32
-- piTempCost : INT* out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let msienumcomponentcostsw =
  foreign "MsiEnumComponentCostsW"
    (uint32_t @-> (ptr uint16_t) @-> uint32_t @-> int32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> (ptr int32_t) @-> (ptr int32_t) @-> returning uint32_t)
(* hInstall : MSIHANDLE -> uint32_t *)
(* szComponent : LPCWSTR -> (ptr uint16_t) *)
(* dwIndex : DWORD -> uint32_t *)
(* iState : INSTALLSTATE -> int32_t *)
(* szDriveBuf : LPWSTR out -> (ptr uint16_t) *)
(* pcchDriveBuf : DWORD* in/out -> (ptr uint32_t) *)
(* piCost : INT* out -> (ptr int32_t) *)
(* piTempCost : INT* out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library msi (t "msi.dll"))
(cffi:use-foreign-library msi)

(cffi:defcfun ("MsiEnumComponentCostsW" msi-enum-component-costs-w :convention :stdcall) :uint32
  (h-install :uint32)   ; MSIHANDLE
  (sz-component (:string :encoding :utf-16le))   ; LPCWSTR
  (dw-index :uint32)   ; DWORD
  (i-state :int32)   ; INSTALLSTATE
  (sz-drive-buf :pointer)   ; LPWSTR out
  (pcch-drive-buf :pointer)   ; DWORD* in/out
  (pi-cost :pointer)   ; INT* out
  (pi-temp-cost :pointer))   ; INT* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MsiEnumComponentCostsW = Win32::API::More->new('msi',
    'DWORD MsiEnumComponentCostsW(DWORD hInstall, LPCWSTR szComponent, DWORD dwIndex, int iState, LPWSTR szDriveBuf, LPVOID pcchDriveBuf, LPVOID piCost, LPVOID piTempCost)');
# my $ret = $MsiEnumComponentCostsW->Call($hInstall, $szComponent, $dwIndex, $iState, $szDriveBuf, $pcchDriveBuf, $piCost, $piTempCost);
# hInstall : MSIHANDLE -> DWORD
# szComponent : LPCWSTR -> LPCWSTR
# dwIndex : DWORD -> DWORD
# iState : INSTALLSTATE -> int
# szDriveBuf : LPWSTR out -> LPWSTR
# pcchDriveBuf : DWORD* in/out -> LPVOID
# piCost : INT* out -> LPVOID
# piTempCost : INT* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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