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

CreateMutexW

関数
名前付きまたは無名のミューテックスを作成する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HANDLE CreateMutexW(
    SECURITY_ATTRIBUTES* lpMutexAttributes,   // optional
    BOOL bInitialOwner,
    LPCWSTR lpName   // optional
);

パラメーター

名前方向
lpMutexAttributesSECURITY_ATTRIBUTES*inoptional
bInitialOwnerBOOLin
lpNameLPCWSTRinoptional

戻り値の型: HANDLE

各言語での呼び出し定義

// 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 方式にも切替可。
出力引数:
; 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 方式にも切替可。
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   // HANDLE
function 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 版の利用を推奨。