Win32 API 日本語リファレンス
ホームNetworking.WinSock › WSAQuerySocketSecurity

WSAQuerySocketSecurity

関数
ソケットに適用されたセキュリティ情報を照会する。
DLLfwpuclnt.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

INT WSAQuerySocketSecurity(
    SOCKET Socket,
    const SOCKET_SECURITY_QUERY_TEMPLATE* SecurityQueryTemplate,   // optional
    DWORD SecurityQueryTemplateLen,
    SOCKET_SECURITY_QUERY_INFO* SecurityQueryInfo,   // optional
    DWORD* SecurityQueryInfoLen,
    OVERLAPPED* Overlapped,   // optional
    LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine   // optional
);

パラメーター

名前方向説明
SocketSOCKETinセキュリティ情報を照会する socket を識別する記述子。
SecurityQueryTemplateSOCKET_SECURITY_QUERY_TEMPLATE*inoptional

返す照会情報の種類を指定する SOCKET_SECURITY_QUERY_TEMPLATE 構造体へのポインター。

このパラメーターが指す SOCKET_SECURITY_QUERY_TEMPLATE 構造体は、既定のセキュリティ情報を要求するために、すべてのメンバーをゼロにすることができます。正常に返された場合、返される SecurityQueryInfo パラメーター内の SOCKET_SECURITY_QUERY_INFO では Flags メンバーのみが設定されます。

Socket パラメーターが IPPROTO_TCP プロトコルで作成された場合、このパラメーターは NULL ポインターにすることができます。この場合、返される情報は、すべての値をゼロに設定した SOCKET_SECURITY_QUERY_TEMPLATE 構造体を渡した場合と同じになります。IPPROTO_TCP プロトコルの socket で既定のセキュリティ情報を超える情報が必要な場合は、このパラメーターを指定する必要があります。

SOCKET_SECURITY_QUERY_TEMPLATE 構造体で PeerTokenAccessMask メンバーが指定されていない (ゼロに設定された) 場合、WSAQuerySocketSecurity 関数は SOCKET_SECURITY_QUERY_INFO 構造体の PeerApplicationAccessTokenHandle および PeerMachineAccessTokenHandle メンバーを返しません。

Socket パラメーターが IPPROTO_TCP 以外のプロトコルで作成された場合は、SecurityQueryTemplate パラメーターを指定する必要があります。これらの場合、SOCKET_SECURITY_QUERY_TEMPLATE 構造体の PeerAddress メンバーで、AF_INET または AF_INET6 のアドレスファミリと、ピアの IP アドレスおよびポート番号を指定する必要があります。

SecurityQueryTemplateLenDWORDin

SecurityQueryTemplate パラメーターのサイズ (バイト単位)。

Socket パラメーターが IPPROTO_TCP プロトコルで作成された場合、このパラメーターはゼロにすることができます。それ以外の場合、このパラメーターは SOCKET_SECURITY_QUERY_TEMPLATE 構造体のサイズである必要があります。

SecurityQueryInfoSOCKET_SECURITY_QUERY_INFO*outoptional照会された情報を格納する SOCKET_SECURITY_QUERY_INFO 構造体を受け取るバッファーへのポインター。この値を NULL に設定すると、出力バッファーのサイズを照会できます。
SecurityQueryInfoLenDWORD*inout入力時には、SecurityQueryInfo パラメーターのサイズ (バイト単位) へのポインター。バッファーが小さすぎて照会された情報を受け取れない場合、呼び出しは SOCKET_ERROR を返し、照会された情報を返すために必要なバイト数がこのパラメーターが指す値に設定されます。呼び出しが成功した場合は、コピーされたバイト数が返されます。
OverlappedOVERLAPPED*inoptionalWSAOVERLAPPED 構造体へのポインター。このパラメーターは、オーバーラップしない socket では無視されます。
CompletionRoutineLPWSAOVERLAPPED_COMPLETION_ROUTINEinoptional操作が完了したときに呼び出される完了ルーチンへのポインター。このパラメーターは、オーバーラップしない socket では無視されます。

戻り値の型: INT

公式ドキュメント

socket 上の接続に適用されているセキュリティに関する情報を照会します。

戻り値

関数が成功した場合、戻り値はゼロです。それ以外の場合は SOCKET_ERROR が返され、 WSAGetLastError を呼び出すことで具体的なエラーコードを取得できます。

発生し得るエラーコードの一部を以下に示します。

エラーコード 意味
WSAEAFNOSUPPORT
指定されたアドレスファミリはサポートされていません。
WSAECONNRESET
ストリーム socket の場合、仮想回線がリモート側によってリセットされました。アプリケーションはその socket が使用できなくなるため、socket を閉じる必要があります。UDP データグラム socket の場合、このエラーは以前の送信操作が ICMP の "Port Unreachable" メッセージを引き起こしたことを示します。
WSAEFAULT
パラメーターを使用しようとした際に、システムが無効なポインターアドレスを検出しました。このエラーは、SecurityQueryInfoLen パラメーターが NULL ポインターであった場合に返されます。
WSAEINVAL
無効なパラメーターが渡されました。このエラーは、Socket パラメーターで渡された socket が、AF_INET または AF_INET6 のアドレスファミリと、SOCK_DGRAM または SOCK_STREAM の socket 型で作成されていなかった場合に返されます。
WSAEMSGSIZE
渡されたバッファーが小さすぎました。このエラーは、プロトコルが IPPROTO_TCP でない Socket パラメーターに対して、SecurityQueryInfo パラメーターが NULL ポインターであるか、SecurityQueryTemplateLen パラメーターが SOCKET_SECURITY_QUERY_TEMPLATE 構造体のサイズより小さい場合に返されます。
WSAENOTSOCK
Socket パラメーターで渡された記述子が有効な socket ではありません。

解説(Remarks)

WSAQuerySocketSecurity 関数は、socket における現在のセキュリティ設定を照会する方法を提供します。接続が確立された後、WSAQuerySocketSecurity 関数を使用すると、アプリケーションは接続のセキュリティプロパティを照会でき、これにはピアアクセストークンに関する情報を含めることができます。

コネクション指向の socket の場合、接続が確立された直後に WSAQuerySocketSecurity 関数を呼び出すことが推奨されます。コネクションレスの socket の場合、新しいピアアドレスへデータが送信された直後、または新しいピアアドレスから受信された直後に WSAQuerySocketSecurity 関数を呼び出すことが推奨されます。WSAQuerySocketSecurity 関数は、単一の socket に対して複数回呼び出すことができます。

この関数は、dwIoControlCode パラメーターを SIO_QUERY_SECURITY に設定して WSAIoctl 関数を呼び出す必要がある処理を簡素化します。

WSAQuerySocketSecurity 関数は、AF_INET または AF_INET6 のアドレスファミリで作成された Socket パラメーターに対して呼び出すことができます。

Socket パラメーターが IPPROTO_TCP プロトコルで作成された場合、SecurityQueryTemplate パラメーターは NULL に、SecurityQueryTemplateLen パラメーターはゼロにすることができます。それ以外の場合、SecurityQueryTemplate パラメーターは SOCKET_SECURITY_QUERY_TEMPLATE 構造体を指している必要があります。

コネクション指向の socket (IPPROTO_TCP プロトコルで作成された socket) を使用するクライアントアプリケーションの場合、WSAQuerySocketSecurity 関数は connectConnectEx、または WSAConnect 関数が返った後に呼び出す必要があります。コネクション指向の socket (IPPROTO_TCP プロトコル) を使用するサーバーアプリケーションの場合、WSAQuerySocketSecurity 関数は acceptAcceptEx、または WSAAccept 関数が返った後に呼び出す必要があります。

コネクションレスの socket (IPPROTO_UDP プロトコルで作成された socket) の場合、アプリケーションは新しいピアアドレスに対する WSASendTo または WSARecvFrom の呼び出しが返った直後に WSAQuerySocketSecurity 関数を呼び出す必要があります。

次の条件が満たされない場合、エラーが返されます。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

INT WSAQuerySocketSecurity(
    SOCKET Socket,
    const SOCKET_SECURITY_QUERY_TEMPLATE* SecurityQueryTemplate,   // optional
    DWORD SecurityQueryTemplateLen,
    SOCKET_SECURITY_QUERY_INFO* SecurityQueryInfo,   // optional
    DWORD* SecurityQueryInfoLen,
    OVERLAPPED* Overlapped,   // optional
    LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine   // optional
);
[DllImport("fwpuclnt.dll", SetLastError = true, ExactSpelling = true)]
static extern int WSAQuerySocketSecurity(
    UIntPtr Socket,   // SOCKET
    IntPtr SecurityQueryTemplate,   // SOCKET_SECURITY_QUERY_TEMPLATE* optional
    uint SecurityQueryTemplateLen,   // DWORD
    IntPtr SecurityQueryInfo,   // SOCKET_SECURITY_QUERY_INFO* optional, out
    ref uint SecurityQueryInfoLen,   // DWORD* in/out
    IntPtr Overlapped,   // OVERLAPPED* optional
    IntPtr CompletionRoutine   // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
);
<DllImport("fwpuclnt.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAQuerySocketSecurity(
    Socket As UIntPtr,   ' SOCKET
    SecurityQueryTemplate As IntPtr,   ' SOCKET_SECURITY_QUERY_TEMPLATE* optional
    SecurityQueryTemplateLen As UInteger,   ' DWORD
    SecurityQueryInfo As IntPtr,   ' SOCKET_SECURITY_QUERY_INFO* optional, out
    ByRef SecurityQueryInfoLen As UInteger,   ' DWORD* in/out
    Overlapped As IntPtr,   ' OVERLAPPED* optional
    CompletionRoutine As IntPtr   ' LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
) As Integer
End Function
' Socket : SOCKET
' SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional
' SecurityQueryTemplateLen : DWORD
' SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out
' SecurityQueryInfoLen : DWORD* in/out
' Overlapped : OVERLAPPED* optional
' CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
Declare PtrSafe Function WSAQuerySocketSecurity Lib "fwpuclnt" ( _
    ByVal Socket As LongPtr, _
    ByVal SecurityQueryTemplate As LongPtr, _
    ByVal SecurityQueryTemplateLen As Long, _
    ByVal SecurityQueryInfo As LongPtr, _
    ByRef SecurityQueryInfoLen As Long, _
    ByVal Overlapped As LongPtr, _
    ByVal CompletionRoutine As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSAQuerySocketSecurity = ctypes.windll.fwpuclnt.WSAQuerySocketSecurity
WSAQuerySocketSecurity.restype = ctypes.c_int
WSAQuerySocketSecurity.argtypes = [
    ctypes.c_size_t,  # Socket : SOCKET
    ctypes.c_void_p,  # SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional
    wintypes.DWORD,  # SecurityQueryTemplateLen : DWORD
    ctypes.c_void_p,  # SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out
    ctypes.POINTER(wintypes.DWORD),  # SecurityQueryInfoLen : DWORD* in/out
    ctypes.c_void_p,  # Overlapped : OVERLAPPED* optional
    ctypes.c_void_p,  # CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('fwpuclnt.dll')
WSAQuerySocketSecurity = Fiddle::Function.new(
  lib['WSAQuerySocketSecurity'],
  [
    Fiddle::TYPE_UINTPTR_T,  # Socket : SOCKET
    Fiddle::TYPE_VOIDP,  # SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional
    -Fiddle::TYPE_INT,  # SecurityQueryTemplateLen : DWORD
    Fiddle::TYPE_VOIDP,  # SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out
    Fiddle::TYPE_VOIDP,  # SecurityQueryInfoLen : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # Overlapped : OVERLAPPED* optional
    Fiddle::TYPE_VOIDP,  # CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "fwpuclnt")]
extern "system" {
    fn WSAQuerySocketSecurity(
        Socket: usize,  // SOCKET
        SecurityQueryTemplate: *const SOCKET_SECURITY_QUERY_TEMPLATE,  // SOCKET_SECURITY_QUERY_TEMPLATE* optional
        SecurityQueryTemplateLen: u32,  // DWORD
        SecurityQueryInfo: *mut SOCKET_SECURITY_QUERY_INFO,  // SOCKET_SECURITY_QUERY_INFO* optional, out
        SecurityQueryInfoLen: *mut u32,  // DWORD* in/out
        Overlapped: *mut OVERLAPPED,  // OVERLAPPED* optional
        CompletionRoutine: *const core::ffi::c_void  // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("fwpuclnt.dll", SetLastError = true)]
public static extern int WSAQuerySocketSecurity(UIntPtr Socket, IntPtr SecurityQueryTemplate, uint SecurityQueryTemplateLen, IntPtr SecurityQueryInfo, ref uint SecurityQueryInfoLen, IntPtr Overlapped, IntPtr CompletionRoutine);
"@
$api = Add-Type -MemberDefinition $sig -Name 'fwpuclnt_WSAQuerySocketSecurity' -Namespace Win32 -PassThru
# $api::WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
#uselib "fwpuclnt.dll"
#func global WSAQuerySocketSecurity "WSAQuerySocketSecurity" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; WSAQuerySocketSecurity Socket, varptr(SecurityQueryTemplate), SecurityQueryTemplateLen, varptr(SecurityQueryInfo), varptr(SecurityQueryInfoLen), varptr(Overlapped), CompletionRoutine   ; 戻り値は stat
; Socket : SOCKET -> "sptr"
; SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> "sptr"
; SecurityQueryTemplateLen : DWORD -> "sptr"
; SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> "sptr"
; SecurityQueryInfoLen : DWORD* in/out -> "sptr"
; Overlapped : OVERLAPPED* optional -> "sptr"
; CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "fwpuclnt.dll"
#cfunc global WSAQuerySocketSecurity "WSAQuerySocketSecurity" sptr, var, int, var, var, var, sptr
; res = WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
; Socket : SOCKET -> "sptr"
; SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> "var"
; SecurityQueryTemplateLen : DWORD -> "int"
; SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> "var"
; SecurityQueryInfoLen : DWORD* in/out -> "var"
; Overlapped : OVERLAPPED* optional -> "var"
; CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSAQuerySocketSecurity(SOCKET Socket, SOCKET_SECURITY_QUERY_TEMPLATE* SecurityQueryTemplate, DWORD SecurityQueryTemplateLen, SOCKET_SECURITY_QUERY_INFO* SecurityQueryInfo, DWORD* SecurityQueryInfoLen, OVERLAPPED* Overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine)
#uselib "fwpuclnt.dll"
#cfunc global WSAQuerySocketSecurity "WSAQuerySocketSecurity" intptr, var, int, var, var, var, intptr
; res = WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
; Socket : SOCKET -> "intptr"
; SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> "var"
; SecurityQueryTemplateLen : DWORD -> "int"
; SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> "var"
; SecurityQueryInfoLen : DWORD* in/out -> "var"
; Overlapped : OVERLAPPED* optional -> "var"
; CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	fwpuclnt = windows.NewLazySystemDLL("fwpuclnt.dll")
	procWSAQuerySocketSecurity = fwpuclnt.NewProc("WSAQuerySocketSecurity")
)

// Socket (SOCKET), SecurityQueryTemplate (SOCKET_SECURITY_QUERY_TEMPLATE* optional), SecurityQueryTemplateLen (DWORD), SecurityQueryInfo (SOCKET_SECURITY_QUERY_INFO* optional, out), SecurityQueryInfoLen (DWORD* in/out), Overlapped (OVERLAPPED* optional), CompletionRoutine (LPWSAOVERLAPPED_COMPLETION_ROUTINE optional)
r1, _, err := procWSAQuerySocketSecurity.Call(
	uintptr(Socket),
	uintptr(SecurityQueryTemplate),
	uintptr(SecurityQueryTemplateLen),
	uintptr(SecurityQueryInfo),
	uintptr(SecurityQueryInfoLen),
	uintptr(Overlapped),
	uintptr(CompletionRoutine),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function WSAQuerySocketSecurity(
  Socket: NativeUInt;   // SOCKET
  SecurityQueryTemplate: Pointer;   // SOCKET_SECURITY_QUERY_TEMPLATE* optional
  SecurityQueryTemplateLen: DWORD;   // DWORD
  SecurityQueryInfo: Pointer;   // SOCKET_SECURITY_QUERY_INFO* optional, out
  SecurityQueryInfoLen: Pointer;   // DWORD* in/out
  Overlapped: Pointer;   // OVERLAPPED* optional
  CompletionRoutine: Pointer   // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
): Integer; stdcall;
  external 'fwpuclnt.dll' name 'WSAQuerySocketSecurity';
result := DllCall("fwpuclnt\WSAQuerySocketSecurity"
    , "UPtr", Socket   ; SOCKET
    , "Ptr", SecurityQueryTemplate   ; SOCKET_SECURITY_QUERY_TEMPLATE* optional
    , "UInt", SecurityQueryTemplateLen   ; DWORD
    , "Ptr", SecurityQueryInfo   ; SOCKET_SECURITY_QUERY_INFO* optional, out
    , "Ptr", SecurityQueryInfoLen   ; DWORD* in/out
    , "Ptr", Overlapped   ; OVERLAPPED* optional
    , "Ptr", CompletionRoutine   ; LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
    , "Int")   ; return: INT
●WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine) = DLL("fwpuclnt.dll", "int WSAQuerySocketSecurity(int, void*, dword, void*, void*, void*, void*)")
# 呼び出し: WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
# Socket : SOCKET -> "int"
# SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> "void*"
# SecurityQueryTemplateLen : DWORD -> "dword"
# SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> "void*"
# SecurityQueryInfoLen : DWORD* in/out -> "void*"
# Overlapped : OVERLAPPED* optional -> "void*"
# CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "fwpuclnt" fn WSAQuerySocketSecurity(
    Socket: usize, // SOCKET
    SecurityQueryTemplate: [*c]SOCKET_SECURITY_QUERY_TEMPLATE, // SOCKET_SECURITY_QUERY_TEMPLATE* optional
    SecurityQueryTemplateLen: u32, // DWORD
    SecurityQueryInfo: [*c]SOCKET_SECURITY_QUERY_INFO, // SOCKET_SECURITY_QUERY_INFO* optional, out
    SecurityQueryInfoLen: [*c]u32, // DWORD* in/out
    Overlapped: [*c]OVERLAPPED, // OVERLAPPED* optional
    CompletionRoutine: ?*anyopaque // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
) callconv(std.os.windows.WINAPI) i32;
proc WSAQuerySocketSecurity(
    Socket: uint,  # SOCKET
    SecurityQueryTemplate: ptr SOCKET_SECURITY_QUERY_TEMPLATE,  # SOCKET_SECURITY_QUERY_TEMPLATE* optional
    SecurityQueryTemplateLen: uint32,  # DWORD
    SecurityQueryInfo: ptr SOCKET_SECURITY_QUERY_INFO,  # SOCKET_SECURITY_QUERY_INFO* optional, out
    SecurityQueryInfoLen: ptr uint32,  # DWORD* in/out
    Overlapped: ptr OVERLAPPED,  # OVERLAPPED* optional
    CompletionRoutine: pointer  # LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
): int32 {.importc: "WSAQuerySocketSecurity", stdcall, dynlib: "fwpuclnt.dll".}
pragma(lib, "fwpuclnt");
extern(Windows)
int WSAQuerySocketSecurity(
    size_t Socket,   // SOCKET
    SOCKET_SECURITY_QUERY_TEMPLATE* SecurityQueryTemplate,   // SOCKET_SECURITY_QUERY_TEMPLATE* optional
    uint SecurityQueryTemplateLen,   // DWORD
    SOCKET_SECURITY_QUERY_INFO* SecurityQueryInfo,   // SOCKET_SECURITY_QUERY_INFO* optional, out
    uint* SecurityQueryInfoLen,   // DWORD* in/out
    OVERLAPPED* Overlapped,   // OVERLAPPED* optional
    void* CompletionRoutine   // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
);
ccall((:WSAQuerySocketSecurity, "fwpuclnt.dll"), stdcall, Int32,
      (Csize_t, Ptr{SOCKET_SECURITY_QUERY_TEMPLATE}, UInt32, Ptr{SOCKET_SECURITY_QUERY_INFO}, Ptr{UInt32}, Ptr{OVERLAPPED}, Ptr{Cvoid}),
      Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
# Socket : SOCKET -> Csize_t
# SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> Ptr{SOCKET_SECURITY_QUERY_TEMPLATE}
# SecurityQueryTemplateLen : DWORD -> UInt32
# SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> Ptr{SOCKET_SECURITY_QUERY_INFO}
# SecurityQueryInfoLen : DWORD* in/out -> Ptr{UInt32}
# Overlapped : OVERLAPPED* optional -> Ptr{OVERLAPPED}
# CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WSAQuerySocketSecurity(
    uintptr_t Socket,
    void* SecurityQueryTemplate,
    uint32_t SecurityQueryTemplateLen,
    void* SecurityQueryInfo,
    uint32_t* SecurityQueryInfoLen,
    void* Overlapped,
    void* CompletionRoutine);
]]
local fwpuclnt = ffi.load("fwpuclnt")
-- fwpuclnt.WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
-- Socket : SOCKET
-- SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional
-- SecurityQueryTemplateLen : DWORD
-- SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out
-- SecurityQueryInfoLen : DWORD* in/out
-- Overlapped : OVERLAPPED* optional
-- CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('fwpuclnt.dll');
const WSAQuerySocketSecurity = lib.func('__stdcall', 'WSAQuerySocketSecurity', 'int32_t', ['uintptr_t', 'void *', 'uint32_t', 'void *', 'uint32_t *', 'void *', 'void *']);
// WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
// Socket : SOCKET -> 'uintptr_t'
// SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> 'void *'
// SecurityQueryTemplateLen : DWORD -> 'uint32_t'
// SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> 'void *'
// SecurityQueryInfoLen : DWORD* in/out -> 'uint32_t *'
// Overlapped : OVERLAPPED* optional -> 'void *'
// CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("fwpuclnt.dll", {
  WSAQuerySocketSecurity: { parameters: ["usize", "pointer", "u32", "pointer", "pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine)
// Socket : SOCKET -> "usize"
// SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> "pointer"
// SecurityQueryTemplateLen : DWORD -> "u32"
// SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> "pointer"
// SecurityQueryInfoLen : DWORD* in/out -> "pointer"
// Overlapped : OVERLAPPED* optional -> "pointer"
// CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WSAQuerySocketSecurity(
    size_t Socket,
    void* SecurityQueryTemplate,
    uint32_t SecurityQueryTemplateLen,
    void* SecurityQueryInfo,
    uint32_t* SecurityQueryInfoLen,
    void* Overlapped,
    void* CompletionRoutine);
C, "fwpuclnt.dll");
// $ffi->WSAQuerySocketSecurity(Socket, SecurityQueryTemplate, SecurityQueryTemplateLen, SecurityQueryInfo, SecurityQueryInfoLen, Overlapped, CompletionRoutine);
// Socket : SOCKET
// SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional
// SecurityQueryTemplateLen : DWORD
// SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out
// SecurityQueryInfoLen : DWORD* in/out
// Overlapped : OVERLAPPED* optional
// CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
// 構造体/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 Fwpuclnt extends StdCallLibrary {
    Fwpuclnt INSTANCE = Native.load("fwpuclnt", Fwpuclnt.class);
    int WSAQuerySocketSecurity(
        long Socket,   // SOCKET
        Pointer SecurityQueryTemplate,   // SOCKET_SECURITY_QUERY_TEMPLATE* optional
        int SecurityQueryTemplateLen,   // DWORD
        Pointer SecurityQueryInfo,   // SOCKET_SECURITY_QUERY_INFO* optional, out
        IntByReference SecurityQueryInfoLen,   // DWORD* in/out
        Pointer Overlapped,   // OVERLAPPED* optional
        Callback CompletionRoutine   // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
    );
}
@[Link("fwpuclnt")]
lib Libfwpuclnt
  fun WSAQuerySocketSecurity = WSAQuerySocketSecurity(
    Socket : LibC::SizeT,   # SOCKET
    SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE*,   # SOCKET_SECURITY_QUERY_TEMPLATE* optional
    SecurityQueryTemplateLen : UInt32,   # DWORD
    SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO*,   # SOCKET_SECURITY_QUERY_INFO* optional, out
    SecurityQueryInfoLen : UInt32*,   # DWORD* in/out
    Overlapped : OVERLAPPED*,   # OVERLAPPED* optional
    CompletionRoutine : Void*   # LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WSAQuerySocketSecurityNative = Int32 Function(UintPtr, Pointer<Void>, Uint32, Pointer<Void>, Pointer<Uint32>, Pointer<Void>, Pointer<Void>);
typedef WSAQuerySocketSecurityDart = int Function(int, Pointer<Void>, int, Pointer<Void>, Pointer<Uint32>, Pointer<Void>, Pointer<Void>);
final WSAQuerySocketSecurity = DynamicLibrary.open('fwpuclnt.dll')
    .lookupFunction<WSAQuerySocketSecurityNative, WSAQuerySocketSecurityDart>('WSAQuerySocketSecurity');
// Socket : SOCKET -> UintPtr
// SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> Pointer<Void>
// SecurityQueryTemplateLen : DWORD -> Uint32
// SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> Pointer<Void>
// SecurityQueryInfoLen : DWORD* in/out -> Pointer<Uint32>
// Overlapped : OVERLAPPED* optional -> Pointer<Void>
// CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WSAQuerySocketSecurity(
  Socket: NativeUInt;   // SOCKET
  SecurityQueryTemplate: Pointer;   // SOCKET_SECURITY_QUERY_TEMPLATE* optional
  SecurityQueryTemplateLen: DWORD;   // DWORD
  SecurityQueryInfo: Pointer;   // SOCKET_SECURITY_QUERY_INFO* optional, out
  SecurityQueryInfoLen: Pointer;   // DWORD* in/out
  Overlapped: Pointer;   // OVERLAPPED* optional
  CompletionRoutine: Pointer   // LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
): Integer; stdcall;
  external 'fwpuclnt.dll' name 'WSAQuerySocketSecurity';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WSAQuerySocketSecurity"
  c_WSAQuerySocketSecurity :: CUIntPtr -> Ptr () -> Word32 -> Ptr () -> Ptr Word32 -> Ptr () -> Ptr () -> IO Int32
-- Socket : SOCKET -> CUIntPtr
-- SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> Ptr ()
-- SecurityQueryTemplateLen : DWORD -> Word32
-- SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> Ptr ()
-- SecurityQueryInfoLen : DWORD* in/out -> Ptr Word32
-- Overlapped : OVERLAPPED* optional -> Ptr ()
-- CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wsaquerysocketsecurity =
  foreign "WSAQuerySocketSecurity"
    (size_t @-> (ptr void) @-> uint32_t @-> (ptr void) @-> (ptr uint32_t) @-> (ptr void) @-> (ptr void) @-> returning int32_t)
(* Socket : SOCKET -> size_t *)
(* SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> (ptr void) *)
(* SecurityQueryTemplateLen : DWORD -> uint32_t *)
(* SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> (ptr void) *)
(* SecurityQueryInfoLen : DWORD* in/out -> (ptr uint32_t) *)
(* Overlapped : OVERLAPPED* optional -> (ptr void) *)
(* CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library fwpuclnt (t "fwpuclnt.dll"))
(cffi:use-foreign-library fwpuclnt)

(cffi:defcfun ("WSAQuerySocketSecurity" wsaquery-socket-security :convention :stdcall) :int32
  (socket :uint64)   ; SOCKET
  (security-query-template :pointer)   ; SOCKET_SECURITY_QUERY_TEMPLATE* optional
  (security-query-template-len :uint32)   ; DWORD
  (security-query-info :pointer)   ; SOCKET_SECURITY_QUERY_INFO* optional, out
  (security-query-info-len :pointer)   ; DWORD* in/out
  (overlapped :pointer)   ; OVERLAPPED* optional
  (completion-routine :pointer))   ; LPWSAOVERLAPPED_COMPLETION_ROUTINE optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WSAQuerySocketSecurity = Win32::API::More->new('fwpuclnt',
    'int WSAQuerySocketSecurity(WPARAM Socket, LPVOID SecurityQueryTemplate, DWORD SecurityQueryTemplateLen, LPVOID SecurityQueryInfo, LPVOID SecurityQueryInfoLen, LPVOID Overlapped, LPVOID CompletionRoutine)');
# my $ret = $WSAQuerySocketSecurity->Call($Socket, $SecurityQueryTemplate, $SecurityQueryTemplateLen, $SecurityQueryInfo, $SecurityQueryInfoLen, $Overlapped, $CompletionRoutine);
# Socket : SOCKET -> WPARAM
# SecurityQueryTemplate : SOCKET_SECURITY_QUERY_TEMPLATE* optional -> LPVOID
# SecurityQueryTemplateLen : DWORD -> DWORD
# SecurityQueryInfo : SOCKET_SECURITY_QUERY_INFO* optional, out -> LPVOID
# SecurityQueryInfoLen : DWORD* in/out -> LPVOID
# Overlapped : OVERLAPPED* optional -> LPVOID
# CompletionRoutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

公式の関連項目
使用する型