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

ReleaseSemaphore

関数
セマフォのカウントを指定数だけ増加させて解放する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL ReleaseSemaphore(
    HANDLE hSemaphore,
    INT lReleaseCount,
    INT* lpPreviousCount   // optional
);

パラメーター

名前方向
hSemaphoreHANDLEin
lReleaseCountINTin
lpPreviousCountINT*outoptional

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL ReleaseSemaphore(
    HANDLE hSemaphore,
    INT lReleaseCount,
    INT* lpPreviousCount   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool ReleaseSemaphore(
    IntPtr hSemaphore,   // HANDLE
    int lReleaseCount,   // INT
    IntPtr lpPreviousCount   // INT* optional, out
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ReleaseSemaphore(
    hSemaphore As IntPtr,   ' HANDLE
    lReleaseCount As Integer,   ' INT
    lpPreviousCount As IntPtr   ' INT* optional, out
) As Boolean
End Function
' hSemaphore : HANDLE
' lReleaseCount : INT
' lpPreviousCount : INT* optional, out
Declare PtrSafe Function ReleaseSemaphore Lib "kernel32" ( _
    ByVal hSemaphore As LongPtr, _
    ByVal lReleaseCount As Long, _
    ByVal lpPreviousCount As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ReleaseSemaphore = ctypes.windll.kernel32.ReleaseSemaphore
ReleaseSemaphore.restype = wintypes.BOOL
ReleaseSemaphore.argtypes = [
    wintypes.HANDLE,  # hSemaphore : HANDLE
    ctypes.c_int,  # lReleaseCount : INT
    ctypes.POINTER(ctypes.c_int),  # lpPreviousCount : INT* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procReleaseSemaphore = kernel32.NewProc("ReleaseSemaphore")
)

// hSemaphore (HANDLE), lReleaseCount (INT), lpPreviousCount (INT* optional, out)
r1, _, err := procReleaseSemaphore.Call(
	uintptr(hSemaphore),
	uintptr(lReleaseCount),
	uintptr(lpPreviousCount),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function ReleaseSemaphore(
  hSemaphore: THandle;   // HANDLE
  lReleaseCount: Integer;   // INT
  lpPreviousCount: Pointer   // INT* optional, out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'ReleaseSemaphore';
result := DllCall("KERNEL32\ReleaseSemaphore"
    , "Ptr", hSemaphore   ; HANDLE
    , "Int", lReleaseCount   ; INT
    , "Ptr", lpPreviousCount   ; INT* optional, out
    , "Int")   ; return: BOOL
●ReleaseSemaphore(hSemaphore, lReleaseCount, lpPreviousCount) = DLL("KERNEL32.dll", "bool ReleaseSemaphore(void*, int, void*)")
# 呼び出し: ReleaseSemaphore(hSemaphore, lReleaseCount, lpPreviousCount)
# hSemaphore : HANDLE -> "void*"
# lReleaseCount : INT -> "int"
# lpPreviousCount : INT* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。