ホーム › System.ApplicationInstallationAndServicing › MsiGetPropertyW
MsiGetPropertyW
関数実行中のインストールセッションのプロパティ値を取得する。
シグネチャ
// msi.dll (Unicode / -W)
#include <windows.h>
DWORD MsiGetPropertyW(
MSIHANDLE hInstall,
LPCWSTR szName,
LPWSTR szValueBuf, // optional
DWORD* pcchValueBuf // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hInstall | MSIHANDLE | in | DLL カスタムアクションに提供される、または MsiOpenPackage、MsiOpenPackageEx、MsiOpenProduct によって取得されるインストールへのハンドルです。 |
| szName | LPCWSTR | in | プロパティ名を指定する null 終端文字列です。 |
| szValueBuf | LPWSTR | outoptional | プロパティの値を格納する null 終端文字列を受け取るバッファーへのポインターです。szValueBuf に null (value=0) を渡してバッファーのサイズを判定しようとしないでください。バッファーのサイズを取得するには、空文字列(例: "")を渡します。その場合、関数は ERROR_MORE_DATA を返し、pchValueBuf に必要なバッファーサイズが TCHAR 単位(終端の null 文字を含まない)で格納されます。ERROR_SUCCESS が返された場合、pcchValueBuf にはバッファーに書き込まれた TCHAR 数(終端の null 文字を含まない)が格納されます。 |
| pcchValueBuf | DWORD* | inoutoptional | 変数 szValueBuf が指すバッファーのサイズを TCHAR 単位で指定する変数へのポインターです。関数が ERROR_SUCCESS を返すと、この変数には szValueBuf へコピーされたデータのサイズ(終端の null 文字を含まない)が格納されます。szValueBuf が十分な大きさでない場合、関数は ERROR_MORE_DATA を返し、必要なサイズ(終端の null 文字を含まない)を pchValueBuf が指す変数に格納します。 |
戻り値の型: DWORD
公式ドキュメント
MsiGetProperty 関数は、インストーラープロパティの値を取得します。(Unicode)
戻り値
この関数は UINT を返します。
解説(Remarks)
MsiGetProperty 関数で取得するプロパティの値が定義されていない場合、長さ 0 の値と同等に扱われます。これはエラーではありません。
ERROR_MORE_DATA が返された場合、ポインターであるパラメーターには文字列を保持するために必要なバッファーのサイズが格納されます。ERROR_SUCCESS が返された場合は、文字列バッファーに書き込まれた文字数が格納されます。したがって、バッファーを指定するパラメーターに空文字列(例: "")を渡すことで、バッファーのサイズを取得できます。null (value=0) を渡してバッファーのサイズを判定しようとしないでください。
次の例は、DLL カスタムアクションが値バッファーのサイズを動的に判定してプロパティの値にアクセスする方法を示しています。
UINT __stdcall MyCustomAction(MSIHANDLE hInstall)
{
TCHAR* szValueBuf = NULL;
DWORD cchValueBuf = 0;
UINT uiStat = MsiGetProperty(hInstall, TEXT("MyProperty"), TEXT(""), &cchValueBuf);
//cchValueBuf now contains the size of the property's string, without null termination
if (ERROR_MORE_DATA == uiStat)
{
++cchValueBuf; // add 1 for null termination
szValueBuf = new TCHAR[cchValueBuf];
if (szValueBuf)
{
uiStat = MsiGetProperty(hInstall, TEXT("MyProperty"), szValueBuf, &cchValueBuf);
}
}
if (ERROR_SUCCESS != uiStat)
{
if (szValueBuf != NULL)
delete[] szValueBuf;
return ERROR_INSTALL_FAILURE;
}
// custom action uses MyProperty
// ...
delete[] szValueBuf;
return ERROR_SUCCESS;
}
メモ
msiquery.h ヘッダーは MsiGetProperty を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング非依存のエイリアスと、エンコーディング非依存でないコードを混在させると、不一致が生じてコンパイルエラーや実行時エラーの原因となることがあります。詳細については、Conventions for Function Prototypes を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// msi.dll (Unicode / -W)
#include <windows.h>
DWORD MsiGetPropertyW(
MSIHANDLE hInstall,
LPCWSTR szName,
LPWSTR szValueBuf, // optional
DWORD* pcchValueBuf // optional
);[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint MsiGetPropertyW(
uint hInstall, // MSIHANDLE
[MarshalAs(UnmanagedType.LPWStr)] string szName, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szValueBuf, // LPWSTR optional, out
IntPtr pcchValueBuf // DWORD* optional, in/out
);<DllImport("msi.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MsiGetPropertyW(
hInstall As UInteger, ' MSIHANDLE
<MarshalAs(UnmanagedType.LPWStr)> szName As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> szValueBuf As System.Text.StringBuilder, ' LPWSTR optional, out
pcchValueBuf As IntPtr ' DWORD* optional, in/out
) As UInteger
End Function' hInstall : MSIHANDLE
' szName : LPCWSTR
' szValueBuf : LPWSTR optional, out
' pcchValueBuf : DWORD* optional, in/out
Declare PtrSafe Function MsiGetPropertyW Lib "msi" ( _
ByVal hInstall As Long, _
ByVal szName As LongPtr, _
ByVal szValueBuf As LongPtr, _
ByVal pcchValueBuf 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
MsiGetPropertyW = ctypes.windll.msi.MsiGetPropertyW
MsiGetPropertyW.restype = wintypes.DWORD
MsiGetPropertyW.argtypes = [
wintypes.DWORD, # hInstall : MSIHANDLE
wintypes.LPCWSTR, # szName : LPCWSTR
wintypes.LPWSTR, # szValueBuf : LPWSTR optional, out
ctypes.POINTER(wintypes.DWORD), # pcchValueBuf : DWORD* optional, in/out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('msi.dll')
MsiGetPropertyW = Fiddle::Function.new(
lib['MsiGetPropertyW'],
[
-Fiddle::TYPE_INT, # hInstall : MSIHANDLE
Fiddle::TYPE_VOIDP, # szName : LPCWSTR
Fiddle::TYPE_VOIDP, # szValueBuf : LPWSTR optional, out
Fiddle::TYPE_VOIDP, # pcchValueBuf : DWORD* optional, in/out
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "msi")]
extern "system" {
fn MsiGetPropertyW(
hInstall: u32, // MSIHANDLE
szName: *const u16, // LPCWSTR
szValueBuf: *mut u16, // LPWSTR optional, out
pcchValueBuf: *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 MsiGetPropertyW(uint hInstall, [MarshalAs(UnmanagedType.LPWStr)] string szName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder szValueBuf, IntPtr pcchValueBuf);
"@
$api = Add-Type -MemberDefinition $sig -Name 'msi_MsiGetPropertyW' -Namespace Win32 -PassThru
# $api::MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf)#uselib "msi.dll"
#func global MsiGetPropertyW "MsiGetPropertyW" wptr, wptr, wptr, wptr
; MsiGetPropertyW hInstall, szName, varptr(szValueBuf), varptr(pcchValueBuf) ; 戻り値は stat
; hInstall : MSIHANDLE -> "wptr"
; szName : LPCWSTR -> "wptr"
; szValueBuf : LPWSTR optional, out -> "wptr"
; pcchValueBuf : DWORD* optional, in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "msi.dll" #cfunc global MsiGetPropertyW "MsiGetPropertyW" int, wstr, var, var ; res = MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf) ; hInstall : MSIHANDLE -> "int" ; szName : LPCWSTR -> "wstr" ; szValueBuf : LPWSTR optional, out -> "var" ; pcchValueBuf : DWORD* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "msi.dll" #cfunc global MsiGetPropertyW "MsiGetPropertyW" int, wstr, sptr, sptr ; res = MsiGetPropertyW(hInstall, szName, varptr(szValueBuf), varptr(pcchValueBuf)) ; hInstall : MSIHANDLE -> "int" ; szName : LPCWSTR -> "wstr" ; szValueBuf : LPWSTR optional, out -> "sptr" ; pcchValueBuf : DWORD* optional, in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD MsiGetPropertyW(MSIHANDLE hInstall, LPCWSTR szName, LPWSTR szValueBuf, DWORD* pcchValueBuf) #uselib "msi.dll" #cfunc global MsiGetPropertyW "MsiGetPropertyW" int, wstr, var, var ; res = MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf) ; hInstall : MSIHANDLE -> "int" ; szName : LPCWSTR -> "wstr" ; szValueBuf : LPWSTR optional, out -> "var" ; pcchValueBuf : DWORD* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD MsiGetPropertyW(MSIHANDLE hInstall, LPCWSTR szName, LPWSTR szValueBuf, DWORD* pcchValueBuf) #uselib "msi.dll" #cfunc global MsiGetPropertyW "MsiGetPropertyW" int, wstr, intptr, intptr ; res = MsiGetPropertyW(hInstall, szName, varptr(szValueBuf), varptr(pcchValueBuf)) ; hInstall : MSIHANDLE -> "int" ; szName : LPCWSTR -> "wstr" ; szValueBuf : LPWSTR optional, out -> "intptr" ; pcchValueBuf : DWORD* optional, in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
msi = windows.NewLazySystemDLL("msi.dll")
procMsiGetPropertyW = msi.NewProc("MsiGetPropertyW")
)
// hInstall (MSIHANDLE), szName (LPCWSTR), szValueBuf (LPWSTR optional, out), pcchValueBuf (DWORD* optional, in/out)
r1, _, err := procMsiGetPropertyW.Call(
uintptr(hInstall),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szName))),
uintptr(szValueBuf),
uintptr(pcchValueBuf),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction MsiGetPropertyW(
hInstall: DWORD; // MSIHANDLE
szName: PWideChar; // LPCWSTR
szValueBuf: PWideChar; // LPWSTR optional, out
pcchValueBuf: Pointer // DWORD* optional, in/out
): DWORD; stdcall;
external 'msi.dll' name 'MsiGetPropertyW';result := DllCall("msi\MsiGetPropertyW"
, "UInt", hInstall ; MSIHANDLE
, "WStr", szName ; LPCWSTR
, "Ptr", szValueBuf ; LPWSTR optional, out
, "Ptr", pcchValueBuf ; DWORD* optional, in/out
, "UInt") ; return: DWORD●MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf) = DLL("msi.dll", "dword MsiGetPropertyW(dword, char*, char*, void*)")
# 呼び出し: MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf)
# hInstall : MSIHANDLE -> "dword"
# szName : LPCWSTR -> "char*"
# szValueBuf : LPWSTR optional, out -> "char*"
# pcchValueBuf : 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 MsiGetPropertyW(
hInstall: u32, // MSIHANDLE
szName: [*c]const u16, // LPCWSTR
szValueBuf: [*c]u16, // LPWSTR optional, out
pcchValueBuf: [*c]u32 // DWORD* optional, in/out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc MsiGetPropertyW(
hInstall: uint32, # MSIHANDLE
szName: WideCString, # LPCWSTR
szValueBuf: ptr uint16, # LPWSTR optional, out
pcchValueBuf: ptr uint32 # DWORD* optional, in/out
): uint32 {.importc: "MsiGetPropertyW", stdcall, dynlib: "msi.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "msi");
extern(Windows)
uint MsiGetPropertyW(
uint hInstall, // MSIHANDLE
const(wchar)* szName, // LPCWSTR
wchar* szValueBuf, // LPWSTR optional, out
uint* pcchValueBuf // DWORD* optional, in/out
);ccall((:MsiGetPropertyW, "msi.dll"), stdcall, UInt32,
(UInt32, Cwstring, Ptr{UInt16}, Ptr{UInt32}),
hInstall, szName, szValueBuf, pcchValueBuf)
# hInstall : MSIHANDLE -> UInt32
# szName : LPCWSTR -> Cwstring
# szValueBuf : LPWSTR optional, out -> Ptr{UInt16}
# pcchValueBuf : DWORD* optional, in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t MsiGetPropertyW(
uint32_t hInstall,
const uint16_t* szName,
uint16_t* szValueBuf,
uint32_t* pcchValueBuf);
]]
local msi = ffi.load("msi")
-- msi.MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf)
-- hInstall : MSIHANDLE
-- szName : LPCWSTR
-- szValueBuf : LPWSTR optional, out
-- pcchValueBuf : 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 MsiGetPropertyW = lib.func('__stdcall', 'MsiGetPropertyW', 'uint32_t', ['uint32_t', 'str16', 'uint16_t *', 'uint32_t *']);
// MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf)
// hInstall : MSIHANDLE -> 'uint32_t'
// szName : LPCWSTR -> 'str16'
// szValueBuf : LPWSTR optional, out -> 'uint16_t *'
// pcchValueBuf : DWORD* optional, in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("msi.dll", {
MsiGetPropertyW: { parameters: ["u32", "buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf)
// hInstall : MSIHANDLE -> "u32"
// szName : LPCWSTR -> "buffer"
// szValueBuf : LPWSTR optional, out -> "buffer"
// pcchValueBuf : 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 MsiGetPropertyW(
uint32_t hInstall,
const uint16_t* szName,
uint16_t* szValueBuf,
uint32_t* pcchValueBuf);
C, "msi.dll");
// $ffi->MsiGetPropertyW(hInstall, szName, szValueBuf, pcchValueBuf);
// hInstall : MSIHANDLE
// szName : LPCWSTR
// szValueBuf : LPWSTR optional, out
// pcchValueBuf : 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 MsiGetPropertyW(
int hInstall, // MSIHANDLE
WString szName, // LPCWSTR
char[] szValueBuf, // LPWSTR optional, out
IntByReference pcchValueBuf // DWORD* optional, in/out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("msi")]
lib Libmsi
fun MsiGetPropertyW = MsiGetPropertyW(
hInstall : UInt32, # MSIHANDLE
szName : UInt16*, # LPCWSTR
szValueBuf : UInt16*, # LPWSTR optional, out
pcchValueBuf : 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 MsiGetPropertyWNative = Uint32 Function(Uint32, Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
typedef MsiGetPropertyWDart = int Function(int, Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
final MsiGetPropertyW = DynamicLibrary.open('msi.dll')
.lookupFunction<MsiGetPropertyWNative, MsiGetPropertyWDart>('MsiGetPropertyW');
// hInstall : MSIHANDLE -> Uint32
// szName : LPCWSTR -> Pointer<Utf16>
// szValueBuf : LPWSTR optional, out -> Pointer<Utf16>
// pcchValueBuf : DWORD* optional, in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function MsiGetPropertyW(
hInstall: DWORD; // MSIHANDLE
szName: PWideChar; // LPCWSTR
szValueBuf: PWideChar; // LPWSTR optional, out
pcchValueBuf: Pointer // DWORD* optional, in/out
): DWORD; stdcall;
external 'msi.dll' name 'MsiGetPropertyW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "MsiGetPropertyW"
c_MsiGetPropertyW :: Word32 -> CWString -> CWString -> Ptr Word32 -> IO Word32
-- hInstall : MSIHANDLE -> Word32
-- szName : LPCWSTR -> CWString
-- szValueBuf : LPWSTR optional, out -> CWString
-- pcchValueBuf : DWORD* optional, in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let msigetpropertyw =
foreign "MsiGetPropertyW"
(uint32_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* hInstall : MSIHANDLE -> uint32_t *)
(* szName : LPCWSTR -> (ptr uint16_t) *)
(* szValueBuf : LPWSTR optional, out -> (ptr uint16_t) *)
(* pcchValueBuf : 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 ("MsiGetPropertyW" msi-get-property-w :convention :stdcall) :uint32
(h-install :uint32) ; MSIHANDLE
(sz-name (:string :encoding :utf-16le)) ; LPCWSTR
(sz-value-buf :pointer) ; LPWSTR optional, out
(pcch-value-buf :pointer)) ; DWORD* optional, in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $MsiGetPropertyW = Win32::API::More->new('msi',
'DWORD MsiGetPropertyW(DWORD hInstall, LPCWSTR szName, LPWSTR szValueBuf, LPVOID pcchValueBuf)');
# my $ret = $MsiGetPropertyW->Call($hInstall, $szName, $szValueBuf, $pcchValueBuf);
# hInstall : MSIHANDLE -> DWORD
# szName : LPCWSTR -> LPCWSTR
# szValueBuf : LPWSTR optional, out -> LPWSTR
# pcchValueBuf : DWORD* optional, in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f MsiGetPropertyA (ANSI版) — 実行中のインストールセッションのプロパティ値を取得する。