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

CreateMutexExW

関数
アクセス権とフラグを指定してミューテックスを作成する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

HANDLE CreateMutexExW(
    SECURITY_ATTRIBUTES* lpMutexAttributes,   // optional
    LPCWSTR lpName,   // optional
    DWORD dwFlags,
    DWORD dwDesiredAccess
);

パラメーター

名前方向
lpMutexAttributesSECURITY_ATTRIBUTES*inoptional
lpNameLPCWSTRinoptional
dwFlagsDWORDin
dwDesiredAccessDWORDin

戻り値の型: HANDLE

各言語での呼び出し定義

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

HANDLE CreateMutexExW(
    SECURITY_ATTRIBUTES* lpMutexAttributes,   // optional
    LPCWSTR lpName,   // optional
    DWORD dwFlags,
    DWORD dwDesiredAccess
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateMutexExW(
    IntPtr lpMutexAttributes,   // SECURITY_ATTRIBUTES* optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpName,   // LPCWSTR optional
    uint dwFlags,   // DWORD
    uint dwDesiredAccess   // DWORD
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateMutexExW(
    lpMutexAttributes As IntPtr,   ' SECURITY_ATTRIBUTES* optional
    <MarshalAs(UnmanagedType.LPWStr)> lpName As String,   ' LPCWSTR optional
    dwFlags As UInteger,   ' DWORD
    dwDesiredAccess As UInteger   ' DWORD
) As IntPtr
End Function
' lpMutexAttributes : SECURITY_ATTRIBUTES* optional
' lpName : LPCWSTR optional
' dwFlags : DWORD
' dwDesiredAccess : DWORD
Declare PtrSafe Function CreateMutexExW Lib "kernel32" ( _
    ByVal lpMutexAttributes As LongPtr, _
    ByVal lpName As LongPtr, _
    ByVal dwFlags As Long, _
    ByVal dwDesiredAccess As Long) 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

CreateMutexExW = ctypes.windll.kernel32.CreateMutexExW
CreateMutexExW.restype = ctypes.c_void_p
CreateMutexExW.argtypes = [
    ctypes.c_void_p,  # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
    wintypes.LPCWSTR,  # lpName : LPCWSTR optional
    wintypes.DWORD,  # dwFlags : DWORD
    wintypes.DWORD,  # dwDesiredAccess : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateMutexExW = Fiddle::Function.new(
  lib['CreateMutexExW'],
  [
    Fiddle::TYPE_VOIDP,  # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
    Fiddle::TYPE_VOIDP,  # lpName : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    -Fiddle::TYPE_INT,  # dwDesiredAccess : DWORD
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn CreateMutexExW(
        lpMutexAttributes: *mut SECURITY_ATTRIBUTES,  // SECURITY_ATTRIBUTES* optional
        lpName: *const u16,  // LPCWSTR optional
        dwFlags: u32,  // DWORD
        dwDesiredAccess: u32  // DWORD
    ) -> *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 CreateMutexExW(IntPtr lpMutexAttributes, [MarshalAs(UnmanagedType.LPWStr)] string lpName, uint dwFlags, uint dwDesiredAccess);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateMutexExW' -Namespace Win32 -PassThru
# $api::CreateMutexExW(lpMutexAttributes, lpName, dwFlags, dwDesiredAccess)
#uselib "KERNEL32.dll"
#func global CreateMutexExW "CreateMutexExW" wptr, wptr, wptr, wptr
; CreateMutexExW varptr(lpMutexAttributes), lpName, dwFlags, dwDesiredAccess   ; 戻り値は stat
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; lpName : LPCWSTR optional -> "wptr"
; dwFlags : DWORD -> "wptr"
; dwDesiredAccess : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateMutexExW "CreateMutexExW" var, wstr, int, int
; res = CreateMutexExW(lpMutexAttributes, lpName, dwFlags, dwDesiredAccess)
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; lpName : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
; dwDesiredAccess : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HANDLE CreateMutexExW(SECURITY_ATTRIBUTES* lpMutexAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
#uselib "KERNEL32.dll"
#cfunc global CreateMutexExW "CreateMutexExW" var, wstr, int, int
; res = CreateMutexExW(lpMutexAttributes, lpName, dwFlags, dwDesiredAccess)
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; lpName : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
; dwDesiredAccess : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateMutexExW = kernel32.NewProc("CreateMutexExW")
)

// lpMutexAttributes (SECURITY_ATTRIBUTES* optional), lpName (LPCWSTR optional), dwFlags (DWORD), dwDesiredAccess (DWORD)
r1, _, err := procCreateMutexExW.Call(
	uintptr(lpMutexAttributes),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
	uintptr(dwFlags),
	uintptr(dwDesiredAccess),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HANDLE
function CreateMutexExW(
  lpMutexAttributes: Pointer;   // SECURITY_ATTRIBUTES* optional
  lpName: PWideChar;   // LPCWSTR optional
  dwFlags: DWORD;   // DWORD
  dwDesiredAccess: DWORD   // DWORD
): THandle; stdcall;
  external 'KERNEL32.dll' name 'CreateMutexExW';
result := DllCall("KERNEL32\CreateMutexExW"
    , "Ptr", lpMutexAttributes   ; SECURITY_ATTRIBUTES* optional
    , "WStr", lpName   ; LPCWSTR optional
    , "UInt", dwFlags   ; DWORD
    , "UInt", dwDesiredAccess   ; DWORD
    , "Ptr")   ; return: HANDLE
●CreateMutexExW(lpMutexAttributes, lpName, dwFlags, dwDesiredAccess) = DLL("KERNEL32.dll", "void* CreateMutexExW(void*, char*, dword, dword)")
# 呼び出し: CreateMutexExW(lpMutexAttributes, lpName, dwFlags, dwDesiredAccess)
# lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# lpName : LPCWSTR optional -> "char*"
# dwFlags : DWORD -> "dword"
# dwDesiredAccess : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。