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

MsiSetComponentStateW

関数
コンポーネントのインストール状態を設定する(Unicode版)。
DLLmsi.dll文字セットUnicode (-W)呼出規約winapi対応OSwindows8.0

シグネチャ

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

DWORD MsiSetComponentStateW(
    MSIHANDLE hInstall,
    LPCWSTR szComponent,
    INSTALLSTATE iState
);

パラメーター

名前方向説明
hInstallMSIHANDLEinDLL カスタムアクションに提供された、または MsiOpenPackageMsiOpenPackageExMsiOpenProduct によって取得されたインストールへのハンドルです。
szComponentLPCWSTRinコンポーネントの名前を指定します。
iStateINSTALLSTATEin

設定する状態を指定します。このパラメーターには、次のいずれかの値を指定できます。

意味
INSTALLSTATE_ABSENT
コンポーネントがアンインストールされました。
INSTALLSTATE_LOCAL
コンポーネントがローカルドライブにインストールされました。
INSTALLSTATE_SOURCE
コンポーネントはソース、CD、またはネットワークから実行されます。

戻り値の型: DWORD

公式ドキュメント

MsiSetComponentState 関数は、コンポーネントを要求された状態に設定します。(Unicode)

戻り値

MsiSetComponentState 関数は、次の値を返します:

解説(Remarks)

MsiSetComponentState 関数は、 Component テーブル内のレコードのアクション状態の変更を要求します。

詳細については、 Calling Database Functions From Programs を参照してください。

関数が失敗した場合は、MsiGetLastErrorRecord を使用して拡張エラー情報を取得できます。

メモ

msiquery.h ヘッダーは、MsiSetComponentState を、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)

各言語での呼び出し定義

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

DWORD MsiSetComponentStateW(
    MSIHANDLE hInstall,
    LPCWSTR szComponent,
    INSTALLSTATE iState
);
[DllImport("msi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint MsiSetComponentStateW(
    uint hInstall,   // MSIHANDLE
    [MarshalAs(UnmanagedType.LPWStr)] string szComponent,   // LPCWSTR
    int iState   // INSTALLSTATE
);
<DllImport("msi.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function MsiSetComponentStateW(
    hInstall As UInteger,   ' MSIHANDLE
    <MarshalAs(UnmanagedType.LPWStr)> szComponent As String,   ' LPCWSTR
    iState As Integer   ' INSTALLSTATE
) As UInteger
End Function
' hInstall : MSIHANDLE
' szComponent : LPCWSTR
' iState : INSTALLSTATE
Declare PtrSafe Function MsiSetComponentStateW Lib "msi" ( _
    ByVal hInstall As Long, _
    ByVal szComponent As LongPtr, _
    ByVal iState 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

MsiSetComponentStateW = ctypes.windll.msi.MsiSetComponentStateW
MsiSetComponentStateW.restype = wintypes.DWORD
MsiSetComponentStateW.argtypes = [
    wintypes.DWORD,  # hInstall : MSIHANDLE
    wintypes.LPCWSTR,  # szComponent : LPCWSTR
    ctypes.c_int,  # iState : INSTALLSTATE
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('msi.dll')
MsiSetComponentStateW = Fiddle::Function.new(
  lib['MsiSetComponentStateW'],
  [
    -Fiddle::TYPE_INT,  # hInstall : MSIHANDLE
    Fiddle::TYPE_VOIDP,  # szComponent : LPCWSTR
    Fiddle::TYPE_INT,  # iState : INSTALLSTATE
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "msi")]
extern "system" {
    fn MsiSetComponentStateW(
        hInstall: u32,  // MSIHANDLE
        szComponent: *const u16,  // LPCWSTR
        iState: i32  // INSTALLSTATE
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
public static extern uint MsiSetComponentStateW(uint hInstall, [MarshalAs(UnmanagedType.LPWStr)] string szComponent, int iState);
"@
$api = Add-Type -MemberDefinition $sig -Name 'msi_MsiSetComponentStateW' -Namespace Win32 -PassThru
# $api::MsiSetComponentStateW(hInstall, szComponent, iState)
#uselib "msi.dll"
#func global MsiSetComponentStateW "MsiSetComponentStateW" wptr, wptr, wptr
; MsiSetComponentStateW hInstall, szComponent, iState   ; 戻り値は stat
; hInstall : MSIHANDLE -> "wptr"
; szComponent : LPCWSTR -> "wptr"
; iState : INSTALLSTATE -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "msi.dll"
#cfunc global MsiSetComponentStateW "MsiSetComponentStateW" int, wstr, int
; res = MsiSetComponentStateW(hInstall, szComponent, iState)
; hInstall : MSIHANDLE -> "int"
; szComponent : LPCWSTR -> "wstr"
; iState : INSTALLSTATE -> "int"
; DWORD MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent, INSTALLSTATE iState)
#uselib "msi.dll"
#cfunc global MsiSetComponentStateW "MsiSetComponentStateW" int, wstr, int
; res = MsiSetComponentStateW(hInstall, szComponent, iState)
; hInstall : MSIHANDLE -> "int"
; szComponent : LPCWSTR -> "wstr"
; iState : INSTALLSTATE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	msi = windows.NewLazySystemDLL("msi.dll")
	procMsiSetComponentStateW = msi.NewProc("MsiSetComponentStateW")
)

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

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

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

foreign import stdcall safe "MsiSetComponentStateW"
  c_MsiSetComponentStateW :: Word32 -> CWString -> Int32 -> IO Word32
-- hInstall : MSIHANDLE -> Word32
-- szComponent : LPCWSTR -> CWString
-- iState : INSTALLSTATE -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let msisetcomponentstatew =
  foreign "MsiSetComponentStateW"
    (uint32_t @-> (ptr uint16_t) @-> int32_t @-> returning uint32_t)
(* hInstall : MSIHANDLE -> uint32_t *)
(* szComponent : LPCWSTR -> (ptr uint16_t) *)
(* iState : INSTALLSTATE -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library msi (t "msi.dll"))
(cffi:use-foreign-library msi)

(cffi:defcfun ("MsiSetComponentStateW" msi-set-component-state-w :convention :stdcall) :uint32
  (h-install :uint32)   ; MSIHANDLE
  (sz-component (:string :encoding :utf-16le))   ; LPCWSTR
  (i-state :int32))   ; INSTALLSTATE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MsiSetComponentStateW = Win32::API::More->new('msi',
    'DWORD MsiSetComponentStateW(DWORD hInstall, LPCWSTR szComponent, int iState)');
# my $ret = $MsiSetComponentStateW->Call($hInstall, $szComponent, $iState);
# hInstall : MSIHANDLE -> DWORD
# szComponent : LPCWSTR -> LPCWSTR
# iState : INSTALLSTATE -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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