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

MsiGetComponentPathExW

関数
インストールコンテキストとユーザーSIDを指定してコンポーネントのパスと状態を取得する。
DLLmsi.dll文字セットUnicode (-W)呼出規約winapi対応OSwindows8.0

シグネチャ

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

INSTALLSTATE MsiGetComponentPathExW(
    LPCWSTR szProductCode,
    LPCWSTR szComponentCode,
    LPCWSTR szUserSid,   // optional
    MSIINSTALLCONTEXT dwContext,   // optional
    LPWSTR lpOutPathBuffer,   // optional
    DWORD* pcchOutPathBuffer   // optional
);

パラメーター

名前方向説明
szProductCodeLPCWSTRinアプリケーションの製品コード GUID を指定する null 終端文字列値です。この関数は、このアプリケーションが使用するインストール済みコンポーネントのパスを取得します。
szComponentCodeLPCWSTRinコンポーネントコード GUID を指定する null 終端文字列値です。この関数は、このコンポーネントコードを持つインストール済みコンポーネントのパスを取得します。
szUserSidLPCWSTRinoptional

システム内のユーザーのセキュリティ識別子 (SID) を指定する null 終端文字列値です。この関数は、この SID で識別されるユーザーアカウントの下にインストールされたアプリケーションのインストール済みコンポーネントのパスを取得します。特別な SID 文字列 s-1-1-0 (Everyone) は、システム内のすべてのユーザーを指定します。このパラメーターが NULL の場合、関数は現在ログオンしているユーザーのみのインストール済みコンポーネントのパスを取得します。

SID の種類 意味
NULL
現在ログオンしているユーザーを指定します。
User SID
システム内の特定のユーザーを指定します。ユーザー SID の例としては "S-1-3-64-2415071341-1358098788-3127455600-2561" があります。
s-1-1-0
システム内のすべてのユーザーを指定します。
注意 特別な SID 文字列 s-1-5-18 (System) は、マシン単位のインストールコンテキストでインストールされたアプリケーションの検索には使用できません。SID 値を s-1-5-18 に設定すると ERROR_INVALID_PARAMETER が返されます。dwContextMSIINSTALLCONTEXT_MACHINE のみに設定されている場合、szUserSidNULL でなければなりません。
dwContextMSIINSTALLCONTEXTinoptional

インストールコンテキストを指定するフラグです。この関数は、指定されたインストールコンテキストでインストールされたアプリケーションのインストール済みコンポーネントのパスを取得します。このパラメーターには、次の値の組み合わせを指定できます。

コンテキスト 意味
MSIINSTALLCONTEXT_USERMANAGED
1
ユーザー単位の管理対象 (per-user-managed) インストールコンテキストでインストールされたアプリケーションを含めます。
MSIINSTALLCONTEXT_USERUNMANAGED
2
ユーザー単位の非管理対象 (per-user-unmanaged) インストールコンテキストでインストールされたアプリケーションを含めます。
MSIINSTALLCONTEXT_MACHINE
4
マシン単位 (per-machine) のインストールコンテキストでインストールされたアプリケーションを含めます。dwInstallContextMSIINSTALLCONTEXT_MACHINE のみに設定されている場合、szUserSID パラメーターは NULL でなければなりません。
lpOutPathBufferLPWSTRoutoptional

コンポーネントへのパスを受け取る文字列値です。このパラメーターには NULL を指定できます。コンポーネントがレジストリキーの場合、レジストリルートは数値で表されます。これがレジストリサブキーパスの場合、キーパスの末尾にバックスラッシュが付きます。レジストリ値キーパスの場合、末尾にバックスラッシュは付きません。たとえば、32 ビットオペレーティングシステム上のレジストリパス HKEY_CURRENT_USER\SOFTWARE\Microsoft は "01:\SOFTWARE\Microsoft" として返されます。32 ビットオペレーティングシステムで返されるレジストリルートは、次の表のように定義されています。

注意 64 ビットオペレーティングシステムでは、32 ビットオペレーティングシステムのレジストリキーパスと区別するために、この表の数値レジストリルートに 20 が加算されます。 たとえば、コンポーネントパスが 64 ビットオペレーティングシステム上のレジストリキーである場合、レジストリキーパス HKEY_CURRENT_USER\SOFTWARE\Microsoft は "21:\SOFTWARE\Microsoft\" として返されます。
ルート 意味
HKEY_CLASSES_ROOT
00
HKEY_CURRENT_USER
01
HKEY_LOCAL_MACHINE
02
HKEY_USERS
03
pcchOutPathBufferDWORD*inoutoptionalszPathBuf パラメーターが指すバッファーのサイズ (TCHAR 単位) を受け取る場所へのポインターです。この場所の値は、終端の null 文字を含む文字列の TCHAR 数に設定する必要があります。バッファーのサイズが小さすぎる場合、このパラメーターは終端の null 文字を数に含まない文字列値の長さを受け取ります。

戻り値の型: INSTALLSTATE

公式ドキュメント

インストール済みコンポーネントへのフルパスを返します。(Unicode)

戻り値

MsiGetComponentPathEx 関数は次の値を返します。

意味
INSTALLSTATE_NOTUSED
要求されたコンポーネントはコンピューター上で無効化されています。
INSTALLSTATE_BADCONFIG
構成データが破損しています。
INSTALLSTATE_ABSENT
コンポーネントがインストールされていません。
INSTALLSTATE_INVALIDARG
関数のパラメーターのいずれかが無効です。
INSTALLSTATE_LOCAL
コンポーネントはローカルにインストールされています。
INSTALLSTATE_SOURCE
コンポーネントはソースから実行するようにインストールされています。
INSTALLSTATE_SOURCEABSENT
コンポーネントのソースにアクセスできません。
INSTALLSTATE_UNKNOWN
製品コードまたはコンポーネント ID が不明です。
INSTALLSTATE_BROKEN
コンポーネントが何らかの形で破損または一部欠落しており、修復が必要です。

解説(Remarks)

MsiGetComponentPathEx 関数は、次の理由により INSTALLSTATE_ABSENT または INSTALL_STATE_UNKNOWN を返すことがあります。

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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>

INSTALLSTATE MsiGetComponentPathExW(
    LPCWSTR szProductCode,
    LPCWSTR szComponentCode,
    LPCWSTR szUserSid,   // optional
    MSIINSTALLCONTEXT dwContext,   // optional
    LPWSTR lpOutPathBuffer,   // optional
    DWORD* pcchOutPathBuffer   // optional
);
[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern int MsiGetComponentPathExW(
    [MarshalAs(UnmanagedType.LPWStr)] string szProductCode,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string szComponentCode,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string szUserSid,   // LPCWSTR optional
    int dwContext,   // MSIINSTALLCONTEXT optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpOutPathBuffer,   // LPWSTR optional, out
    IntPtr pcchOutPathBuffer   // DWORD* optional, in/out
);
<DllImport("msi.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MsiGetComponentPathExW(
    <MarshalAs(UnmanagedType.LPWStr)> szProductCode As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> szComponentCode As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> szUserSid As String,   ' LPCWSTR optional
    dwContext As Integer,   ' MSIINSTALLCONTEXT optional
    <MarshalAs(UnmanagedType.LPWStr)> lpOutPathBuffer As System.Text.StringBuilder,   ' LPWSTR optional, out
    pcchOutPathBuffer As IntPtr   ' DWORD* optional, in/out
) As Integer
End Function
' szProductCode : LPCWSTR
' szComponentCode : LPCWSTR
' szUserSid : LPCWSTR optional
' dwContext : MSIINSTALLCONTEXT optional
' lpOutPathBuffer : LPWSTR optional, out
' pcchOutPathBuffer : DWORD* optional, in/out
Declare PtrSafe Function MsiGetComponentPathExW Lib "msi" ( _
    ByVal szProductCode As LongPtr, _
    ByVal szComponentCode As LongPtr, _
    ByVal szUserSid As LongPtr, _
    ByVal dwContext As Long, _
    ByVal lpOutPathBuffer As LongPtr, _
    ByVal pcchOutPathBuffer 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

MsiGetComponentPathExW = ctypes.windll.msi.MsiGetComponentPathExW
MsiGetComponentPathExW.restype = ctypes.c_int
MsiGetComponentPathExW.argtypes = [
    wintypes.LPCWSTR,  # szProductCode : LPCWSTR
    wintypes.LPCWSTR,  # szComponentCode : LPCWSTR
    wintypes.LPCWSTR,  # szUserSid : LPCWSTR optional
    ctypes.c_int,  # dwContext : MSIINSTALLCONTEXT optional
    wintypes.LPWSTR,  # lpOutPathBuffer : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchOutPathBuffer : DWORD* optional, in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	msi = windows.NewLazySystemDLL("msi.dll")
	procMsiGetComponentPathExW = msi.NewProc("MsiGetComponentPathExW")
)

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

extern "msi" fn MsiGetComponentPathExW(
    szProductCode: [*c]const u16, // LPCWSTR
    szComponentCode: [*c]const u16, // LPCWSTR
    szUserSid: [*c]const u16, // LPCWSTR optional
    dwContext: i32, // MSIINSTALLCONTEXT optional
    lpOutPathBuffer: [*c]u16, // LPWSTR optional, out
    pcchOutPathBuffer: [*c]u32 // DWORD* optional, in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc MsiGetComponentPathExW(
    szProductCode: WideCString,  # LPCWSTR
    szComponentCode: WideCString,  # LPCWSTR
    szUserSid: WideCString,  # LPCWSTR optional
    dwContext: int32,  # MSIINSTALLCONTEXT optional
    lpOutPathBuffer: ptr uint16,  # LPWSTR optional, out
    pcchOutPathBuffer: ptr uint32  # DWORD* optional, in/out
): int32 {.importc: "MsiGetComponentPathExW", stdcall, dynlib: "msi.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "msi");
extern(Windows)
int MsiGetComponentPathExW(
    const(wchar)* szProductCode,   // LPCWSTR
    const(wchar)* szComponentCode,   // LPCWSTR
    const(wchar)* szUserSid,   // LPCWSTR optional
    int dwContext,   // MSIINSTALLCONTEXT optional
    wchar* lpOutPathBuffer,   // LPWSTR optional, out
    uint* pcchOutPathBuffer   // DWORD* optional, in/out
);
ccall((:MsiGetComponentPathExW, "msi.dll"), stdcall, Int32,
      (Cwstring, Cwstring, Cwstring, Int32, Ptr{UInt16}, Ptr{UInt32}),
      szProductCode, szComponentCode, szUserSid, dwContext, lpOutPathBuffer, pcchOutPathBuffer)
# szProductCode : LPCWSTR -> Cwstring
# szComponentCode : LPCWSTR -> Cwstring
# szUserSid : LPCWSTR optional -> Cwstring
# dwContext : MSIINSTALLCONTEXT optional -> Int32
# lpOutPathBuffer : LPWSTR optional, out -> Ptr{UInt16}
# pcchOutPathBuffer : DWORD* optional, in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t MsiGetComponentPathExW(
    const uint16_t* szProductCode,
    const uint16_t* szComponentCode,
    const uint16_t* szUserSid,
    int32_t dwContext,
    uint16_t* lpOutPathBuffer,
    uint32_t* pcchOutPathBuffer);
]]
local msi = ffi.load("msi")
-- msi.MsiGetComponentPathExW(szProductCode, szComponentCode, szUserSid, dwContext, lpOutPathBuffer, pcchOutPathBuffer)
-- szProductCode : LPCWSTR
-- szComponentCode : LPCWSTR
-- szUserSid : LPCWSTR optional
-- dwContext : MSIINSTALLCONTEXT optional
-- lpOutPathBuffer : LPWSTR optional, out
-- pcchOutPathBuffer : DWORD* optional, in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('msi.dll');
const MsiGetComponentPathExW = lib.func('__stdcall', 'MsiGetComponentPathExW', 'int32_t', ['str16', 'str16', 'str16', 'int32_t', 'uint16_t *', 'uint32_t *']);
// MsiGetComponentPathExW(szProductCode, szComponentCode, szUserSid, dwContext, lpOutPathBuffer, pcchOutPathBuffer)
// szProductCode : LPCWSTR -> 'str16'
// szComponentCode : LPCWSTR -> 'str16'
// szUserSid : LPCWSTR optional -> 'str16'
// dwContext : MSIINSTALLCONTEXT optional -> 'int32_t'
// lpOutPathBuffer : LPWSTR optional, out -> 'uint16_t *'
// pcchOutPathBuffer : DWORD* optional, in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("msi.dll", {
  MsiGetComponentPathExW: { parameters: ["buffer", "buffer", "buffer", "i32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.MsiGetComponentPathExW(szProductCode, szComponentCode, szUserSid, dwContext, lpOutPathBuffer, pcchOutPathBuffer)
// szProductCode : LPCWSTR -> "buffer"
// szComponentCode : LPCWSTR -> "buffer"
// szUserSid : LPCWSTR optional -> "buffer"
// dwContext : MSIINSTALLCONTEXT optional -> "i32"
// lpOutPathBuffer : LPWSTR optional, out -> "buffer"
// pcchOutPathBuffer : DWORD* optional, in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t MsiGetComponentPathExW(
    const uint16_t* szProductCode,
    const uint16_t* szComponentCode,
    const uint16_t* szUserSid,
    int32_t dwContext,
    uint16_t* lpOutPathBuffer,
    uint32_t* pcchOutPathBuffer);
C, "msi.dll");
// $ffi->MsiGetComponentPathExW(szProductCode, szComponentCode, szUserSid, dwContext, lpOutPathBuffer, pcchOutPathBuffer);
// szProductCode : LPCWSTR
// szComponentCode : LPCWSTR
// szUserSid : LPCWSTR optional
// dwContext : MSIINSTALLCONTEXT optional
// lpOutPathBuffer : LPWSTR optional, out
// pcchOutPathBuffer : DWORD* optional, in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Msi extends StdCallLibrary {
    Msi INSTANCE = Native.load("msi", Msi.class, W32APIOptions.UNICODE_OPTIONS);
    int MsiGetComponentPathExW(
        WString szProductCode,   // LPCWSTR
        WString szComponentCode,   // LPCWSTR
        WString szUserSid,   // LPCWSTR optional
        int dwContext,   // MSIINSTALLCONTEXT optional
        char[] lpOutPathBuffer,   // LPWSTR optional, out
        IntByReference pcchOutPathBuffer   // DWORD* optional, in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("msi")]
lib Libmsi
  fun MsiGetComponentPathExW = MsiGetComponentPathExW(
    szProductCode : UInt16*,   # LPCWSTR
    szComponentCode : UInt16*,   # LPCWSTR
    szUserSid : UInt16*,   # LPCWSTR optional
    dwContext : Int32,   # MSIINSTALLCONTEXT optional
    lpOutPathBuffer : UInt16*,   # LPWSTR optional, out
    pcchOutPathBuffer : UInt32*   # DWORD* optional, in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MsiGetComponentPathExWNative = Int32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Int32, Pointer<Utf16>, Pointer<Uint32>);
typedef MsiGetComponentPathExWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Uint32>);
final MsiGetComponentPathExW = DynamicLibrary.open('msi.dll')
    .lookupFunction<MsiGetComponentPathExWNative, MsiGetComponentPathExWDart>('MsiGetComponentPathExW');
// szProductCode : LPCWSTR -> Pointer<Utf16>
// szComponentCode : LPCWSTR -> Pointer<Utf16>
// szUserSid : LPCWSTR optional -> Pointer<Utf16>
// dwContext : MSIINSTALLCONTEXT optional -> Int32
// lpOutPathBuffer : LPWSTR optional, out -> Pointer<Utf16>
// pcchOutPathBuffer : DWORD* optional, in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MsiGetComponentPathExW(
  szProductCode: PWideChar;   // LPCWSTR
  szComponentCode: PWideChar;   // LPCWSTR
  szUserSid: PWideChar;   // LPCWSTR optional
  dwContext: Integer;   // MSIINSTALLCONTEXT optional
  lpOutPathBuffer: PWideChar;   // LPWSTR optional, out
  pcchOutPathBuffer: Pointer   // DWORD* optional, in/out
): Integer; stdcall;
  external 'msi.dll' name 'MsiGetComponentPathExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MsiGetComponentPathExW"
  c_MsiGetComponentPathExW :: CWString -> CWString -> CWString -> Int32 -> CWString -> Ptr Word32 -> IO Int32
-- szProductCode : LPCWSTR -> CWString
-- szComponentCode : LPCWSTR -> CWString
-- szUserSid : LPCWSTR optional -> CWString
-- dwContext : MSIINSTALLCONTEXT optional -> Int32
-- lpOutPathBuffer : LPWSTR optional, out -> CWString
-- pcchOutPathBuffer : DWORD* optional, in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let msigetcomponentpathexw =
  foreign "MsiGetComponentPathExW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> int32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* szProductCode : LPCWSTR -> (ptr uint16_t) *)
(* szComponentCode : LPCWSTR -> (ptr uint16_t) *)
(* szUserSid : LPCWSTR optional -> (ptr uint16_t) *)
(* dwContext : MSIINSTALLCONTEXT optional -> int32_t *)
(* lpOutPathBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchOutPathBuffer : DWORD* optional, in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library msi (t "msi.dll"))
(cffi:use-foreign-library msi)

(cffi:defcfun ("MsiGetComponentPathExW" msi-get-component-path-ex-w :convention :stdcall) :int32
  (sz-product-code (:string :encoding :utf-16le))   ; LPCWSTR
  (sz-component-code (:string :encoding :utf-16le))   ; LPCWSTR
  (sz-user-sid (:string :encoding :utf-16le))   ; LPCWSTR optional
  (dw-context :int32)   ; MSIINSTALLCONTEXT optional
  (lp-out-path-buffer :pointer)   ; LPWSTR optional, out
  (pcch-out-path-buffer :pointer))   ; DWORD* optional, in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MsiGetComponentPathExW = Win32::API::More->new('msi',
    'int MsiGetComponentPathExW(LPCWSTR szProductCode, LPCWSTR szComponentCode, LPCWSTR szUserSid, int dwContext, LPWSTR lpOutPathBuffer, LPVOID pcchOutPathBuffer)');
# my $ret = $MsiGetComponentPathExW->Call($szProductCode, $szComponentCode, $szUserSid, $dwContext, $lpOutPathBuffer, $pcchOutPathBuffer);
# szProductCode : LPCWSTR -> LPCWSTR
# szComponentCode : LPCWSTR -> LPCWSTR
# szUserSid : LPCWSTR optional -> LPCWSTR
# dwContext : MSIINSTALLCONTEXT optional -> int
# lpOutPathBuffer : LPWSTR optional, out -> LPWSTR
# pcchOutPathBuffer : DWORD* optional, in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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