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

CreateSemaphoreA

関数
名前付きセマフォオブジェクトを作成または取得する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HANDLE CreateSemaphoreA(
    SECURITY_ATTRIBUTES* lpSemaphoreAttributes,   // optional
    INT lInitialCount,
    INT lMaximumCount,
    LPCSTR lpName   // optional
);

パラメーター

名前方向
lpSemaphoreAttributesSECURITY_ATTRIBUTES*inoptional
lInitialCountINTin
lMaximumCountINTin
lpNameLPCSTRinoptional

戻り値の型: HANDLE

各言語での呼び出し定義

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

HANDLE CreateSemaphoreA(
    SECURITY_ATTRIBUTES* lpSemaphoreAttributes,   // optional
    INT lInitialCount,
    INT lMaximumCount,
    LPCSTR lpName   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateSemaphoreA(
    IntPtr lpSemaphoreAttributes,   // SECURITY_ATTRIBUTES* optional
    int lInitialCount,   // INT
    int lMaximumCount,   // INT
    [MarshalAs(UnmanagedType.LPStr)] string lpName   // LPCSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateSemaphoreA(
    lpSemaphoreAttributes As IntPtr,   ' SECURITY_ATTRIBUTES* optional
    lInitialCount As Integer,   ' INT
    lMaximumCount As Integer,   ' INT
    <MarshalAs(UnmanagedType.LPStr)> lpName As String   ' LPCSTR optional
) As IntPtr
End Function
' lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional
' lInitialCount : INT
' lMaximumCount : INT
' lpName : LPCSTR optional
Declare PtrSafe Function CreateSemaphoreA Lib "kernel32" ( _
    ByVal lpSemaphoreAttributes As LongPtr, _
    ByVal lInitialCount As Long, _
    ByVal lMaximumCount 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

CreateSemaphoreA = ctypes.windll.kernel32.CreateSemaphoreA
CreateSemaphoreA.restype = ctypes.c_void_p
CreateSemaphoreA.argtypes = [
    ctypes.c_void_p,  # lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional
    ctypes.c_int,  # lInitialCount : INT
    ctypes.c_int,  # lMaximumCount : INT
    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')
CreateSemaphoreA = Fiddle::Function.new(
  lib['CreateSemaphoreA'],
  [
    Fiddle::TYPE_VOIDP,  # lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional
    Fiddle::TYPE_INT,  # lInitialCount : INT
    Fiddle::TYPE_INT,  # lMaximumCount : INT
    Fiddle::TYPE_VOIDP,  # lpName : LPCSTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn CreateSemaphoreA(
        lpSemaphoreAttributes: *mut SECURITY_ATTRIBUTES,  // SECURITY_ATTRIBUTES* optional
        lInitialCount: i32,  // INT
        lMaximumCount: i32,  // INT
        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 CreateSemaphoreA(IntPtr lpSemaphoreAttributes, int lInitialCount, int lMaximumCount, [MarshalAs(UnmanagedType.LPStr)] string lpName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateSemaphoreA' -Namespace Win32 -PassThru
# $api::CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName)
#uselib "KERNEL32.dll"
#func global CreateSemaphoreA "CreateSemaphoreA" sptr, sptr, sptr, sptr
; CreateSemaphoreA varptr(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName   ; 戻り値は stat
; lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional -> "sptr"
; lInitialCount : INT -> "sptr"
; lMaximumCount : INT -> "sptr"
; lpName : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateSemaphoreA "CreateSemaphoreA" var, int, int, str
; res = CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName)
; lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; lInitialCount : INT -> "int"
; lMaximumCount : INT -> "int"
; lpName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HANDLE CreateSemaphoreA(SECURITY_ATTRIBUTES* lpSemaphoreAttributes, INT lInitialCount, INT lMaximumCount, LPCSTR lpName)
#uselib "KERNEL32.dll"
#cfunc global CreateSemaphoreA "CreateSemaphoreA" var, int, int, str
; res = CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName)
; lpSemaphoreAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; lInitialCount : INT -> "int"
; lMaximumCount : INT -> "int"
; lpName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateSemaphoreA = kernel32.NewProc("CreateSemaphoreA")
)

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