CreateMutexW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE CreateMutexW(
SECURITY_ATTRIBUTES* lpMutexAttributes, // optional
BOOL bInitialOwner,
LPCWSTR lpName // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpMutexAttributes | SECURITY_ATTRIBUTES* | inoptional | SECURITY_ATTRIBUTES 構造体へのポインターです。このパラメーターが NULL の場合、ハンドルは子プロセスに継承されません。 構造体の lpSecurityDescriptor メンバーは、新しいミューテックスのセキュリティ記述子を指定します。lpMutexAttributes が NULL の場合、ミューテックスは既定のセキュリティ記述子を取得します。ミューテックスの既定のセキュリティ記述子内の ACL は、作成者のプライマリトークンまたは偽装トークンに由来します。詳細については、Synchronization Object Security and Access Rights を参照してください。 |
| bInitialOwner | BOOL | in | この値が TRUE で、呼び出し元がミューテックスを作成した場合、呼び出し元のスレッドはミューテックスオブジェクトの初期所有権を取得します。それ以外の場合、呼び出し元のスレッドは所有権を取得しません。呼び出し元がミューテックスを作成したかどうかを判断するには、「戻り値」のセクションを参照してください。 |
| lpName | LPCWSTR | inoptional | ミューテックスオブジェクトの名前です。名前は MAX_PATH 文字までに制限されます。名前の比較では大文字と小文字が区別されます。 lpName が既存の名前付きミューテックスオブジェクトの名前と一致する場合、この関数は MUTEX_ALL_ACCESS アクセス権を要求します。この場合、bInitialOwner パラメーターは、作成元のプロセスによって既に設定されているため無視されます。lpMutexAttributes パラメーターが NULL でない場合、ハンドルを継承できるかどうかを決定しますが、そのセキュリティ記述子メンバーは無視されます。 lpName が NULL の場合、ミューテックスオブジェクトは名前なしで作成されます。 lpName が既存のイベント、セマフォ、待機可能タイマー、ジョブ、またはファイルマッピングオブジェクトの名前と一致する場合、関数は失敗し、GetLastError 関数は ERROR_INVALID_HANDLE を返します。これは、これらのオブジェクトが同じ名前空間を共有しているために発生します。 名前には、グローバル名前空間またはセッション名前空間に明示的にオブジェクトを作成するために、"Global" または "Local" のプレフィックスを付けることができます。名前の残りの部分には、バックスラッシュ文字 (\) を除く任意の文字を含めることができます。詳細については、Kernel Object Namespaces を参照してください。ユーザーの簡易切り替えは、ターミナルサービスのセッションを使用して実装されています。カーネルオブジェクト名は、アプリケーションが複数のユーザーをサポートできるように、ターミナルサービスについて示されたガイドラインに従う必要があります。 オブジェクトはプライベート名前空間に作成できます。詳細については、Object Namespaces を参照してください。 |
戻り値の型: HANDLE
公式ドキュメント
名前付きまたは名前なしのミューテックスオブジェクトを作成または開きます。(Unicode)
戻り値
関数が成功した場合、戻り値は新しく作成されたミューテックスオブジェクトへのハンドルです。
関数が失敗した場合、戻り値は NULL です。拡張エラー情報を取得するには、GetLastError を呼び出してください。
ミューテックスが名前付きミューテックスで、この関数呼び出しの前にオブジェクトが既に存在していた場合、戻り値は既存のオブジェクトへのハンドルとなり、GetLastError 関数は ERROR_ALREADY_EXISTS を返します。
解説(Remarks)
CreateMutex が返すハンドルは MUTEX_ALL_ACCESS アクセス権を持ち、呼び出し元にアクセスが許可されていれば、ミューテックスオブジェクトへのハンドルを必要とする任意の関数で使用できます。サービスから、または別のユーザーを偽装しているスレッドからミューテックスを作成する場合、作成時にミューテックスにセキュリティ記述子を適用するか、または作成元プロセスの既定の DACL を変更してその既定のセキュリティ記述子を変更することができます。詳細については、Synchronization Object Security and Access Rights を参照してください。
名前付きミューテックスを使用してアプリケーションを単一インスタンスに制限している場合、悪意のあるユーザーが先にこのミューテックスを作成し、アプリケーションの起動を妨げる可能性があります。この状況を防ぐには、ランダムな名前のミューテックスを作成し、その名前を許可されたユーザーのみが取得できるように保存してください。あるいは、この目的でファイルを使用することもできます。アプリケーションをユーザーごとに 1 インスタンスに制限するには、ユーザーのプロファイルディレクトリにロックされたファイルを作成してください。
呼び出し元プロセスの任意のスレッドは、いずれかの待機関数の呼び出しでミューテックスオブジェクトのハンドルを指定できます。単一オブジェクト待機関数は、指定されたオブジェクトの状態がシグナル状態になると戻ります。複数オブジェクト待機関数は、指定されたオブジェクトのいずれか 1 つがシグナル状態になったとき、またはすべてがシグナル状態になったときに戻るように指示できます。待機関数が戻ると、待機していたスレッドは解放されて実行を継続します。
ミューテックスオブジェクトの状態は、どのスレッドにも所有されていないときにシグナル状態になります。作成元のスレッドは、bInitialOwner フラグを使用してミューテックスの即時所有権を要求できます。それ以外の場合、スレッドは所有権を要求するためにいずれかの待機関数を使用する必要があります。ミューテックスの状態がシグナル状態になると、待機中の 1 つのスレッドに所有権が付与され、ミューテックスの状態は非シグナル状態に変わり、待機関数が戻ります。任意の時点でミューテックスを所有できるスレッドは 1 つだけです。所有しているスレッドは、ReleaseMutex 関数を使用してその所有権を解放します。
ミューテックスを所有しているスレッドは、実行をブロックすることなく、繰り返しの待機関数呼び出しで同じミューテックスを指定できます。通常、同じミューテックスを繰り返し待機することはありませんが、このメカニズムにより、スレッドが既に所有しているミューテックスを待機する際の自己デッドロックを防止できます。ただし、所有権を解放するには、ミューテックスが待機を満たした回数だけ ReleaseMutex を呼び出す必要があります。
2 つ以上のプロセスが CreateMutex を呼び出して、同じ名前付きミューテックスを作成できます。実際にミューテックスを作成するのは最初のプロセスであり、十分なアクセス権を持つ後続のプロセスは、既存のミューテックスへのハンドルを開くだけです。これにより、複数のプロセスが同じミューテックスのハンドルを取得できると同時に、作成元のプロセスが最初に開始されることを保証する責任からユーザーが解放されます。この手法を使用する場合は、bInitialOwner フラグを FALSE に設定する必要があります。そうしないと、どのプロセスが初期所有権を持っているかを確実に判断することが難しくなります。
複数のプロセスが同じミューテックスオブジェクトのハンドルを持つことができ、プロセス間同期にこのオブジェクトを使用できます。次のオブジェクト共有メカニズムが利用可能です。
- CreateProcess 関数によって作成された子プロセスは、CreateMutex の lpMutexAttributes パラメーターで継承が有効になっている場合、ミューテックスオブジェクトへのハンドルを継承できます。このメカニズムは、名前付きミューテックスと名前なしミューテックスの両方で機能します。
- プロセスは、DuplicateHandle 関数の呼び出しでミューテックスオブジェクトへのハンドルを指定して、別のプロセスが使用できる複製ハンドルを作成できます。このメカニズムは、名前付きミューテックスと名前なしミューテックスの両方で機能します。
- プロセスは、[OpenMutex](./nf-synchapi-openmutexw.md) 関数または CreateMutex 関数の呼び出しで名前付きミューテックスを指定して、ミューテックスオブジェクトへのハンドルを取得できます。
例
CreateMutex を使用する例については、Using Mutex Objects を参照してください。
synchapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして CreateMutex を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーまたは実行時エラーを引き起こす不一致につながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE CreateMutexW(
SECURITY_ATTRIBUTES* lpMutexAttributes, // optional
BOOL bInitialOwner,
LPCWSTR lpName // optional
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateMutexW(
IntPtr lpMutexAttributes, // SECURITY_ATTRIBUTES* optional
bool bInitialOwner, // BOOL
[MarshalAs(UnmanagedType.LPWStr)] string lpName // LPCWSTR optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateMutexW(
lpMutexAttributes As IntPtr, ' SECURITY_ATTRIBUTES* optional
bInitialOwner As Boolean, ' BOOL
<MarshalAs(UnmanagedType.LPWStr)> lpName As String ' LPCWSTR optional
) As IntPtr
End Function' lpMutexAttributes : SECURITY_ATTRIBUTES* optional
' bInitialOwner : BOOL
' lpName : LPCWSTR optional
Declare PtrSafe Function CreateMutexW Lib "kernel32" ( _
ByVal lpMutexAttributes As LongPtr, _
ByVal bInitialOwner As Long, _
ByVal lpName As LongPtr) As LongPtr
' 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
CreateMutexW = ctypes.windll.kernel32.CreateMutexW
CreateMutexW.restype = ctypes.c_void_p
CreateMutexW.argtypes = [
ctypes.c_void_p, # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
wintypes.BOOL, # bInitialOwner : BOOL
wintypes.LPCWSTR, # lpName : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
CreateMutexW = Fiddle::Function.new(
lib['CreateMutexW'],
[
Fiddle::TYPE_VOIDP, # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
Fiddle::TYPE_INT, # bInitialOwner : BOOL
Fiddle::TYPE_VOIDP, # lpName : LPCWSTR optional
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn CreateMutexW(
lpMutexAttributes: *mut SECURITY_ATTRIBUTES, // SECURITY_ATTRIBUTES* optional
bInitialOwner: i32, // BOOL
lpName: *const u16 // LPCWSTR optional
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateMutexW(IntPtr lpMutexAttributes, bool bInitialOwner, [MarshalAs(UnmanagedType.LPWStr)] string lpName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateMutexW' -Namespace Win32 -PassThru
# $api::CreateMutexW(lpMutexAttributes, bInitialOwner, lpName)#uselib "KERNEL32.dll"
#func global CreateMutexW "CreateMutexW" wptr, wptr, wptr
; CreateMutexW varptr(lpMutexAttributes), bInitialOwner, lpName ; 戻り値は stat
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; bInitialOwner : BOOL -> "wptr"
; lpName : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global CreateMutexW "CreateMutexW" var, int, wstr ; res = CreateMutexW(lpMutexAttributes, bInitialOwner, lpName) ; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; bInitialOwner : BOOL -> "int" ; lpName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global CreateMutexW "CreateMutexW" sptr, int, wstr ; res = CreateMutexW(varptr(lpMutexAttributes), bInitialOwner, lpName) ; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "sptr" ; bInitialOwner : BOOL -> "int" ; lpName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HANDLE CreateMutexW(SECURITY_ATTRIBUTES* lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName) #uselib "KERNEL32.dll" #cfunc global CreateMutexW "CreateMutexW" var, int, wstr ; res = CreateMutexW(lpMutexAttributes, bInitialOwner, lpName) ; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; bInitialOwner : BOOL -> "int" ; lpName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HANDLE CreateMutexW(SECURITY_ATTRIBUTES* lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName) #uselib "KERNEL32.dll" #cfunc global CreateMutexW "CreateMutexW" intptr, int, wstr ; res = CreateMutexW(varptr(lpMutexAttributes), bInitialOwner, lpName) ; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "intptr" ; bInitialOwner : BOOL -> "int" ; lpName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procCreateMutexW = kernel32.NewProc("CreateMutexW")
)
// lpMutexAttributes (SECURITY_ATTRIBUTES* optional), bInitialOwner (BOOL), lpName (LPCWSTR optional)
r1, _, err := procCreateMutexW.Call(
uintptr(lpMutexAttributes),
uintptr(bInitialOwner),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction CreateMutexW(
lpMutexAttributes: Pointer; // SECURITY_ATTRIBUTES* optional
bInitialOwner: BOOL; // BOOL
lpName: PWideChar // LPCWSTR optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateMutexW';result := DllCall("KERNEL32\CreateMutexW"
, "Ptr", lpMutexAttributes ; SECURITY_ATTRIBUTES* optional
, "Int", bInitialOwner ; BOOL
, "WStr", lpName ; LPCWSTR optional
, "Ptr") ; return: HANDLE●CreateMutexW(lpMutexAttributes, bInitialOwner, lpName) = DLL("KERNEL32.dll", "void* CreateMutexW(void*, bool, char*)")
# 呼び出し: CreateMutexW(lpMutexAttributes, bInitialOwner, lpName)
# lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# bInitialOwner : BOOL -> "bool"
# lpName : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn CreateMutexW(
lpMutexAttributes: [*c]SECURITY_ATTRIBUTES, // SECURITY_ATTRIBUTES* optional
bInitialOwner: i32, // BOOL
lpName: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc CreateMutexW(
lpMutexAttributes: ptr SECURITY_ATTRIBUTES, # SECURITY_ATTRIBUTES* optional
bInitialOwner: int32, # BOOL
lpName: WideCString # LPCWSTR optional
): pointer {.importc: "CreateMutexW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
void* CreateMutexW(
SECURITY_ATTRIBUTES* lpMutexAttributes, // SECURITY_ATTRIBUTES* optional
int bInitialOwner, // BOOL
const(wchar)* lpName // LPCWSTR optional
);ccall((:CreateMutexW, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
(Ptr{SECURITY_ATTRIBUTES}, Int32, Cwstring),
lpMutexAttributes, bInitialOwner, lpName)
# lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> Ptr{SECURITY_ATTRIBUTES}
# bInitialOwner : BOOL -> Int32
# lpName : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
void* CreateMutexW(
void* lpMutexAttributes,
int32_t bInitialOwner,
const uint16_t* lpName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CreateMutexW(lpMutexAttributes, bInitialOwner, lpName)
-- lpMutexAttributes : SECURITY_ATTRIBUTES* optional
-- bInitialOwner : BOOL
-- lpName : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CreateMutexW = lib.func('__stdcall', 'CreateMutexW', 'void *', ['void *', 'int32_t', 'str16']);
// CreateMutexW(lpMutexAttributes, bInitialOwner, lpName)
// lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> 'void *'
// bInitialOwner : BOOL -> 'int32_t'
// lpName : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
CreateMutexW: { parameters: ["pointer", "i32", "buffer"], result: "pointer" },
});
// lib.symbols.CreateMutexW(lpMutexAttributes, bInitialOwner, lpName)
// lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "pointer"
// bInitialOwner : BOOL -> "i32"
// lpName : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* CreateMutexW(
void* lpMutexAttributes,
int32_t bInitialOwner,
const uint16_t* lpName);
C, "KERNEL32.dll");
// $ffi->CreateMutexW(lpMutexAttributes, bInitialOwner, lpName);
// lpMutexAttributes : SECURITY_ATTRIBUTES* optional
// bInitialOwner : BOOL
// lpName : LPCWSTR optional
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Pointer CreateMutexW(
Pointer lpMutexAttributes, // SECURITY_ATTRIBUTES* optional
boolean bInitialOwner, // BOOL
WString lpName // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun CreateMutexW = CreateMutexW(
lpMutexAttributes : SECURITY_ATTRIBUTES*, # SECURITY_ATTRIBUTES* optional
bInitialOwner : Int32, # BOOL
lpName : UInt16* # LPCWSTR optional
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CreateMutexWNative = Pointer<Void> Function(Pointer<Void>, Int32, Pointer<Utf16>);
typedef CreateMutexWDart = Pointer<Void> Function(Pointer<Void>, int, Pointer<Utf16>);
final CreateMutexW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<CreateMutexWNative, CreateMutexWDart>('CreateMutexW');
// lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> Pointer<Void>
// bInitialOwner : BOOL -> Int32
// lpName : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CreateMutexW(
lpMutexAttributes: Pointer; // SECURITY_ATTRIBUTES* optional
bInitialOwner: BOOL; // BOOL
lpName: PWideChar // LPCWSTR optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateMutexW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CreateMutexW"
c_CreateMutexW :: Ptr () -> CInt -> CWString -> IO (Ptr ())
-- lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> Ptr ()
-- bInitialOwner : BOOL -> CInt
-- lpName : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let createmutexw =
foreign "CreateMutexW"
((ptr void) @-> int32_t @-> (ptr uint16_t) @-> returning (ptr void))
(* lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> (ptr void) *)
(* bInitialOwner : BOOL -> int32_t *)
(* lpName : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("CreateMutexW" create-mutex-w :convention :stdcall) :pointer
(lp-mutex-attributes :pointer) ; SECURITY_ATTRIBUTES* optional
(b-initial-owner :int32) ; BOOL
(lp-name (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CreateMutexW = Win32::API::More->new('KERNEL32',
'HANDLE CreateMutexW(LPVOID lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName)');
# my $ret = $CreateMutexW->Call($lpMutexAttributes, $bInitialOwner, $lpName);
# lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> LPVOID
# bInitialOwner : BOOL -> BOOL
# lpName : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f CreateMutexA (ANSI版) — 名前付きまたは無名のミューテックスを作成する(ANSI版)。
- f CreateMutexExW — アクセス権とフラグを指定してミューテックスを作成する(Unicode版)。
- f CloseHandle — オープンされたオブジェクトハンドルを閉じる。
- f CreateProcessW — 新しいプロセスとそのプライマリスレッドを作成する(Unicode版)。
- f DuplicateHandle — オブジェクトハンドルの複製を作成する。
- f ReleaseMutex — ミューテックスの所有権を解放する。