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

MsiEnumProductsExW

関数
コンテキストやユーザーを指定してインストール済み製品を列挙する。
DLLmsi.dll文字セットUnicode (-W)呼出規約winapi対応OSwindows8.0

シグネチャ

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

DWORD MsiEnumProductsExW(
    LPCWSTR szProductCode,   // optional
    LPCWSTR szUserSid,   // optional
    DWORD dwContext,
    DWORD dwIndex,
    LPWSTR szInstalledProductCode,   // optional
    MSIINSTALLCONTEXT* pdwInstalledContext,   // optional
    LPWSTR szSid,   // optional
    DWORD* pcchSid   // optional
);

パラメーター

名前方向説明
szProductCodeLPCWSTRinoptional列挙する製品の ProductCode GUID。szUserSid および dwContext パラメーターで指定したコンテキストの範囲内にある製品インスタンスのみが列挙されます。このパラメーターを NULL に設定すると、指定したコンテキスト内のすべての製品が列挙されます。
szUserSidLPCWSTRinoptional

列挙のコンテキストを制限するセキュリティ識別子 (SID) を指定する、null 終端文字列。特別な SID 文字列 s-1-1-0 (Everyone) は、システム内のすべてのユーザーにわたる列挙を指定します。s-1-1-0 以外の SID 値はユーザー SID と見なされ、列挙を現在のユーザーまたはシステム内の任意のユーザーに制限します。このパラメーターを NULL に設定すると、列挙範囲が現在のユーザーに制限されます。

SID の種類 意味
NULL
現在ログオンしているユーザーを指定します。
User SID
システム内の特定のユーザーについての列挙を指定します。ユーザー SID の例は "S-1-3-64-2415071341-1358098788-3127455600-2561" です。
s-1-1-0
システム内のすべてのユーザーにわたる列挙を指定します。
注意 特別な SID 文字列 s-1-5-18 (System) は、コンピューター単位 (per-machine) でインストールされた製品やパッチの列挙には使用できません。dwContextMSIINSTALLCONTEXT_MACHINE のみに設定した場合、szUserSidNULL でなければなりません。
dwContextDWORDin

列挙をコンテキストに制限します。このパラメーターには、次の表に示す値のいずれか 1 つまたは組み合わせを指定できます。

コンテキスト 意味
MSIINSTALLCONTEXT_USERMANAGED
szUserSid で指定したユーザーについて、すべてのユーザー単位の管理対象 (per-user-managed) インストールに列挙を拡張します。無効な SID の場合は項目が返されません。
MSIINSTALLCONTEXT_USERUNMANAGED
szUserSid で指定したユーザーについて、すべてのユーザー単位の非管理対象 (per-user-unmanaged) インストールに列挙を拡張します。無効な SID の場合は項目が返されません。
MSIINSTALLCONTEXT_MACHINE
すべてのコンピューター単位 (per-machine) インストールに列挙を拡張します。dwInstallContextMSIINSTALLCONTEXT_MACHINE のみに設定した場合、szUserSID パラメーターは NULL でなければなりません。
dwIndexDWORDin取得する製品のインデックスを指定します。MsiEnumProductsEx 関数の最初の呼び出しでは、このパラメーターを 0 にする必要があり、以降の呼び出しで増分します。インデックスは、直前の呼び出しが ERROR_SUCCESS を返した場合にのみ増分してください。製品には順序がないため、新しい製品は任意のインデックスを持ちます。つまり、この関数は製品を任意の順序で返すことがあります。
szInstalledProductCodeLPWSTRoutoptional列挙される製品インスタンスの ProductCode GUID を返す、TCHAR の null 終端文字列。このパラメーターは NULL にできます。
pdwInstalledContextMSIINSTALLCONTEXT*outoptional列挙される製品インスタンスのコンテキストを返します。出力値は MSIINSTALLCONTEXT_USERMANAGEDMSIINSTALLCONTEXT_USERUNMANAGED、または MSIINSTALLCONTEXT_MACHINE のいずれかになります。このパラメーターは NULL にできます。
szSidLPWSTRoutoptional

この製品インスタンスが存在するアカウントの文字列 SID を受け取る出力バッファー。コンピューター単位 (per-machine) コンテキストでインストールされたインスタンスの場合、このバッファーには空の文字列が返されます。

このバッファーは SID を格納できる十分な大きさが必要です。バッファーが小さすぎる場合、関数は ERROR_MORE_DATA を返し、終端の NULL 文字を含まない SID 内の TCHAR 数を *pcchSid に設定します。

szSidNULL に設定し、pcchSid を有効なポインターに設定した場合、関数は ERROR_SUCCESS を返し、終端の NULL を含まない値の TCHAR 数を pcchSid に設定します。その後、pcchSid + 1 文字を格納できる十分な大きさの szSid バッファーを使用して、再度関数を呼び出して値を取得できます。

szSidpcchSid の両方を NULL に設定した場合、関数は値を取得せずに、値が存在すれば ERROR_SUCCESS を返します。

pcchSidDWORD*inoutoptional

関数を呼び出すとき、このパラメーターは szSid バッファー内の TCHAR 数を指定する変数へのポインターである必要があります。関数が戻るとき、このパラメーターは、関数が値を指定したバッファーにコピーしたかどうかにかかわらず、要求された値のサイズに設定されます。サイズは、終端の null 文字を含まない、要求された値の TCHAR 数として返されます。

このパラメーターを NULL に設定できるのは、szSidNULL の場合のみです。そうでない場合、関数は ERROR_INVALID_PARAMETER を返します。

戻り値の型: DWORD

公式ドキュメント

指定したコンテキスト内で現在アドバタイズまたはインストールされている製品の、1 つまたはすべてのインスタンスを列挙します。(Unicode)

戻り値

MsiEnumProductsEx 関数は、次のいずれかの値を返します。

戻り値 説明
ERROR_ACCESS_DENIED
スコープに現在のユーザー以外のユーザーが含まれる場合は、管理者権限が必要です。
ERROR_BAD_CONFIGURATION
構成データが破損しています。
ERROR_INVALID_PARAMETER
無効なパラメーターが関数に渡されました。
ERROR_NO_MORE_ITEMS
列挙する製品がこれ以上ありません。
ERROR_SUCCESS
製品が列挙されました。
ERROR_MORE_DATA
szSid パラメーターが小さすぎて、ユーザー SID を取得できません。
ERROR_UNKNOWN_PRODUCT
指定したコンテキストにおいて、製品がコンピューターにインストールされていません。
ERROR_FUNCTION_FAILED
予期しない内部エラーです。

解説(Remarks)

製品を列挙するには、アプリケーションはまず iIndex パラメーターを 0 に設定して MsiEnumProductsEx 関数を呼び出す必要があります。その後、アプリケーションは iProductIndex パラメーターを増分し、関数が ERROR_NO_MORE_ITEMS を返して列挙する製品がなくなるまで MsiEnumProductsEx を呼び出す必要があります。

MsiEnumProductsEx を複数回呼び出してすべての製品を列挙する場合、各呼び出しは同じスレッドから行う必要があります。

すべてのユーザーアカウント、または現在のユーザーアカウント以外のユーザーアカウントにわたって製品を列挙するには、ユーザーに管理者権限が必要です。すべてのユーザー、または現在のユーザー以外のユーザーにわたって列挙する場合、ユーザー単位の非管理対象 (per-user-unmanaged) コンテキストでアドバタイズのみされている (インストールされていないなど) 製品は、列挙からスキップされます。

MsiEnumProductsEx で列挙された各製品インスタンスの状態やその他の情報を取得するには、MsiGetProductInfoEx を使用します。

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 MsiEnumProductsExW(
    LPCWSTR szProductCode,   // optional
    LPCWSTR szUserSid,   // optional
    DWORD dwContext,
    DWORD dwIndex,
    LPWSTR szInstalledProductCode,   // optional
    MSIINSTALLCONTEXT* pdwInstalledContext,   // optional
    LPWSTR szSid,   // optional
    DWORD* pcchSid   // optional
);
[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint MsiEnumProductsExW(
    [MarshalAs(UnmanagedType.LPWStr)] string szProductCode,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string szUserSid,   // LPCWSTR optional
    uint dwContext,   // DWORD
    uint dwIndex,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szInstalledProductCode,   // LPWSTR optional, out
    IntPtr pdwInstalledContext,   // MSIINSTALLCONTEXT* optional, out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szSid,   // LPWSTR optional, out
    IntPtr pcchSid   // DWORD* optional, in/out
);
<DllImport("msi.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MsiEnumProductsExW(
    <MarshalAs(UnmanagedType.LPWStr)> szProductCode As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> szUserSid As String,   ' LPCWSTR optional
    dwContext As UInteger,   ' DWORD
    dwIndex As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> szInstalledProductCode As System.Text.StringBuilder,   ' LPWSTR optional, out
    pdwInstalledContext As IntPtr,   ' MSIINSTALLCONTEXT* optional, out
    <MarshalAs(UnmanagedType.LPWStr)> szSid As System.Text.StringBuilder,   ' LPWSTR optional, out
    pcchSid As IntPtr   ' DWORD* optional, in/out
) As UInteger
End Function
' szProductCode : LPCWSTR optional
' szUserSid : LPCWSTR optional
' dwContext : DWORD
' dwIndex : DWORD
' szInstalledProductCode : LPWSTR optional, out
' pdwInstalledContext : MSIINSTALLCONTEXT* optional, out
' szSid : LPWSTR optional, out
' pcchSid : DWORD* optional, in/out
Declare PtrSafe Function MsiEnumProductsExW Lib "msi" ( _
    ByVal szProductCode As LongPtr, _
    ByVal szUserSid As LongPtr, _
    ByVal dwContext As Long, _
    ByVal dwIndex As Long, _
    ByVal szInstalledProductCode As LongPtr, _
    ByVal pdwInstalledContext As LongPtr, _
    ByVal szSid As LongPtr, _
    ByVal pcchSid 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

MsiEnumProductsExW = ctypes.windll.msi.MsiEnumProductsExW
MsiEnumProductsExW.restype = wintypes.DWORD
MsiEnumProductsExW.argtypes = [
    wintypes.LPCWSTR,  # szProductCode : LPCWSTR optional
    wintypes.LPCWSTR,  # szUserSid : LPCWSTR optional
    wintypes.DWORD,  # dwContext : DWORD
    wintypes.DWORD,  # dwIndex : DWORD
    wintypes.LPWSTR,  # szInstalledProductCode : LPWSTR optional, out
    ctypes.c_void_p,  # pdwInstalledContext : MSIINSTALLCONTEXT* optional, out
    wintypes.LPWSTR,  # szSid : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchSid : DWORD* optional, in/out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	msi = windows.NewLazySystemDLL("msi.dll")
	procMsiEnumProductsExW = msi.NewProc("MsiEnumProductsExW")
)

// szProductCode (LPCWSTR optional), szUserSid (LPCWSTR optional), dwContext (DWORD), dwIndex (DWORD), szInstalledProductCode (LPWSTR optional, out), pdwInstalledContext (MSIINSTALLCONTEXT* optional, out), szSid (LPWSTR optional, out), pcchSid (DWORD* optional, in/out)
r1, _, err := procMsiEnumProductsExW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szProductCode))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szUserSid))),
	uintptr(dwContext),
	uintptr(dwIndex),
	uintptr(szInstalledProductCode),
	uintptr(pdwInstalledContext),
	uintptr(szSid),
	uintptr(pcchSid),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function MsiEnumProductsExW(
  szProductCode: PWideChar;   // LPCWSTR optional
  szUserSid: PWideChar;   // LPCWSTR optional
  dwContext: DWORD;   // DWORD
  dwIndex: DWORD;   // DWORD
  szInstalledProductCode: PWideChar;   // LPWSTR optional, out
  pdwInstalledContext: Pointer;   // MSIINSTALLCONTEXT* optional, out
  szSid: PWideChar;   // LPWSTR optional, out
  pcchSid: Pointer   // DWORD* optional, in/out
): DWORD; stdcall;
  external 'msi.dll' name 'MsiEnumProductsExW';
result := DllCall("msi\MsiEnumProductsExW"
    , "WStr", szProductCode   ; LPCWSTR optional
    , "WStr", szUserSid   ; LPCWSTR optional
    , "UInt", dwContext   ; DWORD
    , "UInt", dwIndex   ; DWORD
    , "Ptr", szInstalledProductCode   ; LPWSTR optional, out
    , "Ptr", pdwInstalledContext   ; MSIINSTALLCONTEXT* optional, out
    , "Ptr", szSid   ; LPWSTR optional, out
    , "Ptr", pcchSid   ; DWORD* optional, in/out
    , "UInt")   ; return: DWORD
●MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid) = DLL("msi.dll", "dword MsiEnumProductsExW(char*, char*, dword, dword, char*, void*, char*, void*)")
# 呼び出し: MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid)
# szProductCode : LPCWSTR optional -> "char*"
# szUserSid : LPCWSTR optional -> "char*"
# dwContext : DWORD -> "dword"
# dwIndex : DWORD -> "dword"
# szInstalledProductCode : LPWSTR optional, out -> "char*"
# pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> "void*"
# szSid : LPWSTR optional, out -> "char*"
# pcchSid : 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 MsiEnumProductsExW(
    szProductCode: [*c]const u16, // LPCWSTR optional
    szUserSid: [*c]const u16, // LPCWSTR optional
    dwContext: u32, // DWORD
    dwIndex: u32, // DWORD
    szInstalledProductCode: [*c]u16, // LPWSTR optional, out
    pdwInstalledContext: [*c]i32, // MSIINSTALLCONTEXT* optional, out
    szSid: [*c]u16, // LPWSTR optional, out
    pcchSid: [*c]u32 // DWORD* optional, in/out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc MsiEnumProductsExW(
    szProductCode: WideCString,  # LPCWSTR optional
    szUserSid: WideCString,  # LPCWSTR optional
    dwContext: uint32,  # DWORD
    dwIndex: uint32,  # DWORD
    szInstalledProductCode: ptr uint16,  # LPWSTR optional, out
    pdwInstalledContext: ptr int32,  # MSIINSTALLCONTEXT* optional, out
    szSid: ptr uint16,  # LPWSTR optional, out
    pcchSid: ptr uint32  # DWORD* optional, in/out
): uint32 {.importc: "MsiEnumProductsExW", stdcall, dynlib: "msi.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "msi");
extern(Windows)
uint MsiEnumProductsExW(
    const(wchar)* szProductCode,   // LPCWSTR optional
    const(wchar)* szUserSid,   // LPCWSTR optional
    uint dwContext,   // DWORD
    uint dwIndex,   // DWORD
    wchar* szInstalledProductCode,   // LPWSTR optional, out
    int* pdwInstalledContext,   // MSIINSTALLCONTEXT* optional, out
    wchar* szSid,   // LPWSTR optional, out
    uint* pcchSid   // DWORD* optional, in/out
);
ccall((:MsiEnumProductsExW, "msi.dll"), stdcall, UInt32,
      (Cwstring, Cwstring, UInt32, UInt32, Ptr{UInt16}, Ptr{Int32}, Ptr{UInt16}, Ptr{UInt32}),
      szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid)
# szProductCode : LPCWSTR optional -> Cwstring
# szUserSid : LPCWSTR optional -> Cwstring
# dwContext : DWORD -> UInt32
# dwIndex : DWORD -> UInt32
# szInstalledProductCode : LPWSTR optional, out -> Ptr{UInt16}
# pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> Ptr{Int32}
# szSid : LPWSTR optional, out -> Ptr{UInt16}
# pcchSid : DWORD* optional, in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t MsiEnumProductsExW(
    const uint16_t* szProductCode,
    const uint16_t* szUserSid,
    uint32_t dwContext,
    uint32_t dwIndex,
    uint16_t* szInstalledProductCode,
    int32_t* pdwInstalledContext,
    uint16_t* szSid,
    uint32_t* pcchSid);
]]
local msi = ffi.load("msi")
-- msi.MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid)
-- szProductCode : LPCWSTR optional
-- szUserSid : LPCWSTR optional
-- dwContext : DWORD
-- dwIndex : DWORD
-- szInstalledProductCode : LPWSTR optional, out
-- pdwInstalledContext : MSIINSTALLCONTEXT* optional, out
-- szSid : LPWSTR optional, out
-- pcchSid : 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 MsiEnumProductsExW = lib.func('__stdcall', 'MsiEnumProductsExW', 'uint32_t', ['str16', 'str16', 'uint32_t', 'uint32_t', 'uint16_t *', 'int32_t *', 'uint16_t *', 'uint32_t *']);
// MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid)
// szProductCode : LPCWSTR optional -> 'str16'
// szUserSid : LPCWSTR optional -> 'str16'
// dwContext : DWORD -> 'uint32_t'
// dwIndex : DWORD -> 'uint32_t'
// szInstalledProductCode : LPWSTR optional, out -> 'uint16_t *'
// pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> 'int32_t *'
// szSid : LPWSTR optional, out -> 'uint16_t *'
// pcchSid : DWORD* optional, in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("msi.dll", {
  MsiEnumProductsExW: { parameters: ["buffer", "buffer", "u32", "u32", "buffer", "pointer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid)
// szProductCode : LPCWSTR optional -> "buffer"
// szUserSid : LPCWSTR optional -> "buffer"
// dwContext : DWORD -> "u32"
// dwIndex : DWORD -> "u32"
// szInstalledProductCode : LPWSTR optional, out -> "buffer"
// pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> "pointer"
// szSid : LPWSTR optional, out -> "buffer"
// pcchSid : DWORD* optional, in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t MsiEnumProductsExW(
    const uint16_t* szProductCode,
    const uint16_t* szUserSid,
    uint32_t dwContext,
    uint32_t dwIndex,
    uint16_t* szInstalledProductCode,
    int32_t* pdwInstalledContext,
    uint16_t* szSid,
    uint32_t* pcchSid);
C, "msi.dll");
// $ffi->MsiEnumProductsExW(szProductCode, szUserSid, dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext, szSid, pcchSid);
// szProductCode : LPCWSTR optional
// szUserSid : LPCWSTR optional
// dwContext : DWORD
// dwIndex : DWORD
// szInstalledProductCode : LPWSTR optional, out
// pdwInstalledContext : MSIINSTALLCONTEXT* optional, out
// szSid : LPWSTR optional, out
// pcchSid : 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 MsiEnumProductsExW(
        WString szProductCode,   // LPCWSTR optional
        WString szUserSid,   // LPCWSTR optional
        int dwContext,   // DWORD
        int dwIndex,   // DWORD
        char[] szInstalledProductCode,   // LPWSTR optional, out
        IntByReference pdwInstalledContext,   // MSIINSTALLCONTEXT* optional, out
        char[] szSid,   // LPWSTR optional, out
        IntByReference pcchSid   // DWORD* optional, in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("msi")]
lib Libmsi
  fun MsiEnumProductsExW = MsiEnumProductsExW(
    szProductCode : UInt16*,   # LPCWSTR optional
    szUserSid : UInt16*,   # LPCWSTR optional
    dwContext : UInt32,   # DWORD
    dwIndex : UInt32,   # DWORD
    szInstalledProductCode : UInt16*,   # LPWSTR optional, out
    pdwInstalledContext : Int32*,   # MSIINSTALLCONTEXT* optional, out
    szSid : UInt16*,   # LPWSTR optional, out
    pcchSid : UInt32*   # DWORD* optional, in/out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

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

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

let msienumproductsexw =
  foreign "MsiEnumProductsExW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> (ptr uint16_t) @-> (ptr int32_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* szProductCode : LPCWSTR optional -> (ptr uint16_t) *)
(* szUserSid : LPCWSTR optional -> (ptr uint16_t) *)
(* dwContext : DWORD -> uint32_t *)
(* dwIndex : DWORD -> uint32_t *)
(* szInstalledProductCode : LPWSTR optional, out -> (ptr uint16_t) *)
(* pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> (ptr int32_t) *)
(* szSid : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchSid : 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 ("MsiEnumProductsExW" msi-enum-products-ex-w :convention :stdcall) :uint32
  (sz-product-code (:string :encoding :utf-16le))   ; LPCWSTR optional
  (sz-user-sid (:string :encoding :utf-16le))   ; LPCWSTR optional
  (dw-context :uint32)   ; DWORD
  (dw-index :uint32)   ; DWORD
  (sz-installed-product-code :pointer)   ; LPWSTR optional, out
  (pdw-installed-context :pointer)   ; MSIINSTALLCONTEXT* optional, out
  (sz-sid :pointer)   ; LPWSTR optional, out
  (pcch-sid :pointer))   ; DWORD* optional, in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MsiEnumProductsExW = Win32::API::More->new('msi',
    'DWORD MsiEnumProductsExW(LPCWSTR szProductCode, LPCWSTR szUserSid, DWORD dwContext, DWORD dwIndex, LPWSTR szInstalledProductCode, LPVOID pdwInstalledContext, LPWSTR szSid, LPVOID pcchSid)');
# my $ret = $MsiEnumProductsExW->Call($szProductCode, $szUserSid, $dwContext, $dwIndex, $szInstalledProductCode, $pdwInstalledContext, $szSid, $pcchSid);
# szProductCode : LPCWSTR optional -> LPCWSTR
# szUserSid : LPCWSTR optional -> LPCWSTR
# dwContext : DWORD -> DWORD
# dwIndex : DWORD -> DWORD
# szInstalledProductCode : LPWSTR optional, out -> LPWSTR
# pdwInstalledContext : MSIINSTALLCONTEXT* optional, out -> LPVOID
# szSid : LPWSTR optional, out -> LPWSTR
# pcchSid : DWORD* optional, in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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