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

CreateSemaphoreW

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

シグネチャ

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

HANDLE CreateSemaphoreW(
    SECURITY_ATTRIBUTES* lpSemaphoreAttributes,   // optional
    INT lInitialCount,
    INT lMaximumCount,
    LPCWSTR lpName   // optional
);

パラメーター

名前方向
lpSemaphoreAttributesSECURITY_ATTRIBUTES*inoptional
lInitialCountINTin
lMaximumCountINTin
lpNameLPCWSTRinoptional

戻り値の型: HANDLE

各言語での呼び出し定義

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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateSemaphoreW = kernel32.NewProc("CreateSemaphoreW")
)

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