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

CreateMutexA

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

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

HANDLE CreateMutexA(
    SECURITY_ATTRIBUTES* lpMutexAttributes,   // optional
    BOOL bInitialOwner,
    LPCSTR lpName   // optional
);

パラメーター

名前方向
lpMutexAttributesSECURITY_ATTRIBUTES*inoptional
bInitialOwnerBOOLin
lpNameLPCSTRinoptional

戻り値の型: HANDLE

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

HANDLE CreateMutexA(
    SECURITY_ATTRIBUTES* lpMutexAttributes,   // optional
    BOOL bInitialOwner,
    LPCSTR lpName   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateMutexA(
    IntPtr lpMutexAttributes,   // SECURITY_ATTRIBUTES* optional
    bool bInitialOwner,   // BOOL
    [MarshalAs(UnmanagedType.LPStr)] string lpName   // LPCSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateMutexA(
    lpMutexAttributes As IntPtr,   ' SECURITY_ATTRIBUTES* optional
    bInitialOwner As Boolean,   ' BOOL
    <MarshalAs(UnmanagedType.LPStr)> lpName As String   ' LPCSTR optional
) As IntPtr
End Function
' lpMutexAttributes : SECURITY_ATTRIBUTES* optional
' bInitialOwner : BOOL
' lpName : LPCSTR optional
Declare PtrSafe Function CreateMutexA Lib "kernel32" ( _
    ByVal lpMutexAttributes As LongPtr, _
    ByVal bInitialOwner As Long, _
    ByVal lpName As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateMutexA = ctypes.windll.kernel32.CreateMutexA
CreateMutexA.restype = ctypes.c_void_p
CreateMutexA.argtypes = [
    ctypes.c_void_p,  # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
    wintypes.BOOL,  # bInitialOwner : BOOL
    wintypes.LPCSTR,  # lpName : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateMutexA = Fiddle::Function.new(
  lib['CreateMutexA'],
  [
    Fiddle::TYPE_VOIDP,  # lpMutexAttributes : SECURITY_ATTRIBUTES* optional
    Fiddle::TYPE_INT,  # bInitialOwner : BOOL
    Fiddle::TYPE_VOIDP,  # lpName : LPCSTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn CreateMutexA(
        lpMutexAttributes: *mut SECURITY_ATTRIBUTES,  // SECURITY_ATTRIBUTES* optional
        bInitialOwner: i32,  // BOOL
        lpName: *const u8  // LPCSTR optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr CreateMutexA(IntPtr lpMutexAttributes, bool bInitialOwner, [MarshalAs(UnmanagedType.LPStr)] string lpName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateMutexA' -Namespace Win32 -PassThru
# $api::CreateMutexA(lpMutexAttributes, bInitialOwner, lpName)
#uselib "KERNEL32.dll"
#func global CreateMutexA "CreateMutexA" sptr, sptr, sptr
; CreateMutexA varptr(lpMutexAttributes), bInitialOwner, lpName   ; 戻り値は stat
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "sptr"
; bInitialOwner : BOOL -> "sptr"
; lpName : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateMutexA "CreateMutexA" var, int, str
; res = CreateMutexA(lpMutexAttributes, bInitialOwner, lpName)
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; bInitialOwner : BOOL -> "int"
; lpName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HANDLE CreateMutexA(SECURITY_ATTRIBUTES* lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName)
#uselib "KERNEL32.dll"
#cfunc global CreateMutexA "CreateMutexA" var, int, str
; res = CreateMutexA(lpMutexAttributes, bInitialOwner, lpName)
; lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; bInitialOwner : BOOL -> "int"
; lpName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateMutexA = kernel32.NewProc("CreateMutexA")
)

// lpMutexAttributes (SECURITY_ATTRIBUTES* optional), bInitialOwner (BOOL), lpName (LPCSTR optional)
r1, _, err := procCreateMutexA.Call(
	uintptr(lpMutexAttributes),
	uintptr(bInitialOwner),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpName))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HANDLE
function CreateMutexA(
  lpMutexAttributes: Pointer;   // SECURITY_ATTRIBUTES* optional
  bInitialOwner: BOOL;   // BOOL
  lpName: PAnsiChar   // LPCSTR optional
): THandle; stdcall;
  external 'KERNEL32.dll' name 'CreateMutexA';
result := DllCall("KERNEL32\CreateMutexA"
    , "Ptr", lpMutexAttributes   ; SECURITY_ATTRIBUTES* optional
    , "Int", bInitialOwner   ; BOOL
    , "AStr", lpName   ; LPCSTR optional
    , "Ptr")   ; return: HANDLE
●CreateMutexA(lpMutexAttributes, bInitialOwner, lpName) = DLL("KERNEL32.dll", "void* CreateMutexA(void*, bool, char*)")
# 呼び出し: CreateMutexA(lpMutexAttributes, bInitialOwner, lpName)
# lpMutexAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# bInitialOwner : BOOL -> "bool"
# lpName : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。