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

MsiCloseHandle

関数
開いているMSIハンドルを閉じる。
DLLmsi.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// msi.dll
#include <windows.h>

DWORD MsiCloseHandle(
    MSIHANDLE hAny
);

パラメーター

名前方向説明
hAnyMSIHANDLEin開いている任意のインストールハンドルを指定します。

戻り値の型: DWORD

公式ドキュメント

MsiCloseHandle 関数は、開いているインストールハンドルを閉じます。

戻り値

意味
ERROR_INVALID_HANDLE
無効なハンドルが関数に渡されました。
ERROR_SUCCESS
関数は成功しました。

解説(Remarks)

MsiCloseHandle は、ハンドルの作成を要求したのと同じスレッドから呼び出す必要があります。

次の関数は、使用後に MsiCloseHandle を呼び出して閉じるべきハンドルを返します。

MsiCreateRecord MsiGetActiveDatabase MsiGetLastErrorRecord MsiOpenPackage MsiOpenProduct MsiOpenDatabase MsiDatabaseOpenView MsiViewFetch MsiViewGetColumnInfo MsiDatabaseGetPrimaryKeys MsiGetSummaryInformation MsiEnableUIPreview カスタムアクションを記述する場合は、PMSIHANDLE 型の変数を使用することをお勧めします。MSIHANDLE オブジェクトは MsiCloseHandle を呼び出して閉じる必要があるのに対し、PMSIHANDLE オブジェクトはスコープを外れるとインストーラーによって閉じられるためです。

たとえば、次のようなコードを使用している場合:

MSIHANDLE hRec = MsiCreateRecord(3);

これを次のように変更します:

PMSIHANDLE hRec = MsiCreateRecord(3);

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

各言語での呼び出し定義

// msi.dll
#include <windows.h>

DWORD MsiCloseHandle(
    MSIHANDLE hAny
);
[DllImport("msi.dll", ExactSpelling = true)]
static extern uint MsiCloseHandle(
    uint hAny   // MSIHANDLE
);
<DllImport("msi.dll", ExactSpelling:=True)>
Public Shared Function MsiCloseHandle(
    hAny As UInteger   ' MSIHANDLE
) As UInteger
End Function
' hAny : MSIHANDLE
Declare PtrSafe Function MsiCloseHandle Lib "msi" ( _
    ByVal hAny As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

MsiCloseHandle = ctypes.windll.msi.MsiCloseHandle
MsiCloseHandle.restype = wintypes.DWORD
MsiCloseHandle.argtypes = [
    wintypes.DWORD,  # hAny : MSIHANDLE
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('msi.dll')
MsiCloseHandle = Fiddle::Function.new(
  lib['MsiCloseHandle'],
  [
    -Fiddle::TYPE_INT,  # hAny : MSIHANDLE
  ],
  -Fiddle::TYPE_INT)
#[link(name = "msi")]
extern "system" {
    fn MsiCloseHandle(
        hAny: u32  // MSIHANDLE
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("msi.dll")]
public static extern uint MsiCloseHandle(uint hAny);
"@
$api = Add-Type -MemberDefinition $sig -Name 'msi_MsiCloseHandle' -Namespace Win32 -PassThru
# $api::MsiCloseHandle(hAny)
#uselib "msi.dll"
#func global MsiCloseHandle "MsiCloseHandle" sptr
; MsiCloseHandle hAny   ; 戻り値は stat
; hAny : MSIHANDLE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "msi.dll"
#cfunc global MsiCloseHandle "MsiCloseHandle" int
; res = MsiCloseHandle(hAny)
; hAny : MSIHANDLE -> "int"
; DWORD MsiCloseHandle(MSIHANDLE hAny)
#uselib "msi.dll"
#cfunc global MsiCloseHandle "MsiCloseHandle" int
; res = MsiCloseHandle(hAny)
; hAny : MSIHANDLE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	msi = windows.NewLazySystemDLL("msi.dll")
	procMsiCloseHandle = msi.NewProc("MsiCloseHandle")
)

// hAny (MSIHANDLE)
r1, _, err := procMsiCloseHandle.Call(
	uintptr(hAny),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function MsiCloseHandle(
  hAny: DWORD   // MSIHANDLE
): DWORD; stdcall;
  external 'msi.dll' name 'MsiCloseHandle';
result := DllCall("msi\MsiCloseHandle"
    , "UInt", hAny   ; MSIHANDLE
    , "UInt")   ; return: DWORD
●MsiCloseHandle(hAny) = DLL("msi.dll", "dword MsiCloseHandle(dword)")
# 呼び出し: MsiCloseHandle(hAny)
# hAny : MSIHANDLE -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "msi" fn MsiCloseHandle(
    hAny: u32 // MSIHANDLE
) callconv(std.os.windows.WINAPI) u32;
proc MsiCloseHandle(
    hAny: uint32  # MSIHANDLE
): uint32 {.importc: "MsiCloseHandle", stdcall, dynlib: "msi.dll".}
pragma(lib, "msi");
extern(Windows)
uint MsiCloseHandle(
    uint hAny   // MSIHANDLE
);
ccall((:MsiCloseHandle, "msi.dll"), stdcall, UInt32,
      (UInt32,),
      hAny)
# hAny : MSIHANDLE -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t MsiCloseHandle(
    uint32_t hAny);
]]
local msi = ffi.load("msi")
-- msi.MsiCloseHandle(hAny)
-- hAny : MSIHANDLE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('msi.dll');
const MsiCloseHandle = lib.func('__stdcall', 'MsiCloseHandle', 'uint32_t', ['uint32_t']);
// MsiCloseHandle(hAny)
// hAny : MSIHANDLE -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("msi.dll", {
  MsiCloseHandle: { parameters: ["u32"], result: "u32" },
});
// lib.symbols.MsiCloseHandle(hAny)
// hAny : MSIHANDLE -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t MsiCloseHandle(
    uint32_t hAny);
C, "msi.dll");
// $ffi->MsiCloseHandle(hAny);
// hAny : MSIHANDLE
// 構造体/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);
    int MsiCloseHandle(
        int hAny   // MSIHANDLE
    );
}
@[Link("msi")]
lib Libmsi
  fun MsiCloseHandle = MsiCloseHandle(
    hAny : UInt32   # MSIHANDLE
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MsiCloseHandleNative = Uint32 Function(Uint32);
typedef MsiCloseHandleDart = int Function(int);
final MsiCloseHandle = DynamicLibrary.open('msi.dll')
    .lookupFunction<MsiCloseHandleNative, MsiCloseHandleDart>('MsiCloseHandle');
// hAny : MSIHANDLE -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MsiCloseHandle(
  hAny: DWORD   // MSIHANDLE
): DWORD; stdcall;
  external 'msi.dll' name 'MsiCloseHandle';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MsiCloseHandle"
  c_MsiCloseHandle :: Word32 -> IO Word32
-- hAny : MSIHANDLE -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let msiclosehandle =
  foreign "MsiCloseHandle"
    (uint32_t @-> returning uint32_t)
(* hAny : MSIHANDLE -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library msi (t "msi.dll"))
(cffi:use-foreign-library msi)

(cffi:defcfun ("MsiCloseHandle" msi-close-handle :convention :stdcall) :uint32
  (h-any :uint32))   ; MSIHANDLE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MsiCloseHandle = Win32::API::More->new('msi',
    'DWORD MsiCloseHandle(DWORD hAny)');
# my $ret = $MsiCloseHandle->Call($hAny);
# hAny : MSIHANDLE -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。