ホーム › System.WindowsProgramming › OpenMutexA
OpenMutexA
関数名前付きミューテックスをANSI名で開く。
シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
HANDLE OpenMutexA(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCSTR lpName
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| dwDesiredAccess | DWORD | in | ミューテックスに要求するアクセス権(MUTEX_ALL_ACCESS等)。 |
| bInheritHandle | BOOL | in | 返されるハンドルを子プロセスへ継承させるか。TRUEで継承可。 |
| lpName | LPCSTR | in | 開く既存ミューテックスの名前(ANSI文字列)。 |
戻り値の型: HANDLE
各言語での呼び出し定義
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
HANDLE OpenMutexA(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCSTR lpName
);[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr OpenMutexA(
uint dwDesiredAccess, // DWORD
bool bInheritHandle, // BOOL
[MarshalAs(UnmanagedType.LPStr)] string lpName // LPCSTR
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function OpenMutexA(
dwDesiredAccess As UInteger, ' DWORD
bInheritHandle As Boolean, ' BOOL
<MarshalAs(UnmanagedType.LPStr)> lpName As String ' LPCSTR
) As IntPtr
End Function' dwDesiredAccess : DWORD
' bInheritHandle : BOOL
' lpName : LPCSTR
Declare PtrSafe Function OpenMutexA Lib "kernel32" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle 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
OpenMutexA = ctypes.windll.kernel32.OpenMutexA
OpenMutexA.restype = ctypes.c_void_p
OpenMutexA.argtypes = [
wintypes.DWORD, # dwDesiredAccess : DWORD
wintypes.BOOL, # bInheritHandle : BOOL
wintypes.LPCSTR, # lpName : LPCSTR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
OpenMutexA = Fiddle::Function.new(
lib['OpenMutexA'],
[
-Fiddle::TYPE_INT, # dwDesiredAccess : DWORD
Fiddle::TYPE_INT, # bInheritHandle : BOOL
Fiddle::TYPE_VOIDP, # lpName : LPCSTR
],
Fiddle::TYPE_VOIDP)#[link(name = "kernel32")]
extern "system" {
fn OpenMutexA(
dwDesiredAccess: u32, // DWORD
bInheritHandle: i32, // BOOL
lpName: *const u8 // LPCSTR
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr OpenMutexA(uint dwDesiredAccess, bool bInheritHandle, [MarshalAs(UnmanagedType.LPStr)] string lpName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_OpenMutexA' -Namespace Win32 -PassThru
# $api::OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)#uselib "KERNEL32.dll"
#func global OpenMutexA "OpenMutexA" sptr, sptr, sptr
; OpenMutexA dwDesiredAccess, bInheritHandle, lpName ; 戻り値は stat
; dwDesiredAccess : DWORD -> "sptr"
; bInheritHandle : BOOL -> "sptr"
; lpName : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global OpenMutexA "OpenMutexA" int, int, str
; res = OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
; dwDesiredAccess : DWORD -> "int"
; bInheritHandle : BOOL -> "int"
; lpName : LPCSTR -> "str"; HANDLE OpenMutexA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
#uselib "KERNEL32.dll"
#cfunc global OpenMutexA "OpenMutexA" int, int, str
; res = OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
; dwDesiredAccess : DWORD -> "int"
; bInheritHandle : BOOL -> "int"
; lpName : LPCSTR -> "str"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procOpenMutexA = kernel32.NewProc("OpenMutexA")
)
// dwDesiredAccess (DWORD), bInheritHandle (BOOL), lpName (LPCSTR)
r1, _, err := procOpenMutexA.Call(
uintptr(dwDesiredAccess),
uintptr(bInheritHandle),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction OpenMutexA(
dwDesiredAccess: DWORD; // DWORD
bInheritHandle: BOOL; // BOOL
lpName: PAnsiChar // LPCSTR
): THandle; stdcall;
external 'KERNEL32.dll' name 'OpenMutexA';result := DllCall("KERNEL32\OpenMutexA"
, "UInt", dwDesiredAccess ; DWORD
, "Int", bInheritHandle ; BOOL
, "AStr", lpName ; LPCSTR
, "Ptr") ; return: HANDLE●OpenMutexA(dwDesiredAccess, bInheritHandle, lpName) = DLL("KERNEL32.dll", "void* OpenMutexA(dword, bool, char*)")
# 呼び出し: OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
# dwDesiredAccess : DWORD -> "dword"
# bInheritHandle : BOOL -> "bool"
# lpName : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn OpenMutexA(
dwDesiredAccess: u32, // DWORD
bInheritHandle: i32, // BOOL
lpName: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc OpenMutexA(
dwDesiredAccess: uint32, # DWORD
bInheritHandle: int32, # BOOL
lpName: cstring # LPCSTR
): pointer {.importc: "OpenMutexA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
void* OpenMutexA(
uint dwDesiredAccess, // DWORD
int bInheritHandle, // BOOL
const(char)* lpName // LPCSTR
);ccall((:OpenMutexA, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
(UInt32, Int32, Cstring),
dwDesiredAccess, bInheritHandle, lpName)
# dwDesiredAccess : DWORD -> UInt32
# bInheritHandle : BOOL -> Int32
# lpName : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* OpenMutexA(
uint32_t dwDesiredAccess,
int32_t bInheritHandle,
const char* lpName);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
-- dwDesiredAccess : DWORD
-- bInheritHandle : BOOL
-- lpName : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const OpenMutexA = lib.func('__stdcall', 'OpenMutexA', 'void *', ['uint32_t', 'int32_t', 'str']);
// OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
// dwDesiredAccess : DWORD -> 'uint32_t'
// bInheritHandle : BOOL -> 'int32_t'
// lpName : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
OpenMutexA: { parameters: ["u32", "i32", "buffer"], result: "pointer" },
});
// lib.symbols.OpenMutexA(dwDesiredAccess, bInheritHandle, lpName)
// dwDesiredAccess : DWORD -> "u32"
// bInheritHandle : BOOL -> "i32"
// lpName : LPCSTR -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* OpenMutexA(
uint32_t dwDesiredAccess,
int32_t bInheritHandle,
const char* lpName);
C, "KERNEL32.dll");
// $ffi->OpenMutexA(dwDesiredAccess, bInheritHandle, lpName);
// dwDesiredAccess : DWORD
// bInheritHandle : BOOL
// lpName : LPCSTR
// 構造体/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.ASCII_OPTIONS);
Pointer OpenMutexA(
int dwDesiredAccess, // DWORD
boolean bInheritHandle, // BOOL
String lpName // LPCSTR
);
}@[Link("kernel32")]
lib LibKERNEL32
fun OpenMutexA = OpenMutexA(
dwDesiredAccess : UInt32, # DWORD
bInheritHandle : Int32, # BOOL
lpName : UInt8* # LPCSTR
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef OpenMutexANative = Pointer<Void> Function(Uint32, Int32, Pointer<Utf8>);
typedef OpenMutexADart = Pointer<Void> Function(int, int, Pointer<Utf8>);
final OpenMutexA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<OpenMutexANative, OpenMutexADart>('OpenMutexA');
// dwDesiredAccess : DWORD -> Uint32
// bInheritHandle : BOOL -> Int32
// lpName : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function OpenMutexA(
dwDesiredAccess: DWORD; // DWORD
bInheritHandle: BOOL; // BOOL
lpName: PAnsiChar // LPCSTR
): THandle; stdcall;
external 'KERNEL32.dll' name 'OpenMutexA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "OpenMutexA"
c_OpenMutexA :: Word32 -> CInt -> CString -> IO (Ptr ())
-- dwDesiredAccess : DWORD -> Word32
-- bInheritHandle : BOOL -> CInt
-- lpName : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let openmutexa =
foreign "OpenMutexA"
(uint32_t @-> int32_t @-> string @-> returning (ptr void))
(* dwDesiredAccess : DWORD -> uint32_t *)
(* bInheritHandle : BOOL -> int32_t *)
(* lpName : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("OpenMutexA" open-mutex-a :convention :stdcall) :pointer
(dw-desired-access :uint32) ; DWORD
(b-inherit-handle :int32) ; BOOL
(lp-name :string)) ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $OpenMutexA = Win32::API::More->new('KERNEL32',
'HANDLE OpenMutexA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)');
# my $ret = $OpenMutexA->Call($dwDesiredAccess, $bInheritHandle, $lpName);
# dwDesiredAccess : DWORD -> DWORD
# bInheritHandle : BOOL -> BOOL
# lpName : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f OpenMutexW (Unicode版) — 既存の名前付きミューテックスを開く(Unicode版)。