WSAAsyncGetProtoByNumber
関数シグネチャ
// WS2_32.dll
#include <windows.h>
HANDLE WSAAsyncGetProtoByNumber(
HWND hWnd,
DWORD wMsg,
INT number,
LPSTR buf,
INT buflen
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hWnd | HWND | in | 非同期要求が完了したときにメッセージを受け取るウィンドウのハンドルです。 |
| wMsg | DWORD | in | 非同期要求が完了したときに受け取るメッセージです。 |
| number | INT | in | 解決するプロトコル番号です。ホストバイトオーダーで指定します。 |
| buf | LPSTR | out | protoent データを受け取るデータ領域へのポインターです。Windows Sockets はこのデータ領域に protoent 構造体と、その protoent 構造体のメンバーから参照されるすべてのデータを格納するため、データ領域は protoent 構造体のサイズより大きくする必要があります。MAXGETHOSTSTRUCT バイトのバッファーを使用することを推奨します。 |
| buflen | INT | in | buf パラメーターのデータ領域のサイズ (バイト単位) です。 |
戻り値の型: HANDLE
公式ドキュメント
WSAAsyncGetProtoByNumber 関数 (winsock.h) は、プロトコル番号に対応するプロトコル情報を非同期に取得します。
戻り値
戻り値は、非同期操作が正常に開始されたかどうかを示します。操作自体の成否を意味するものではありません。
エラーが発生しなかった場合、 WSAAsyncGetProtoByNumber は要求の非同期タスクハンドルである、ゼロ以外の HANDLE 型の値を返します (Windows の HTASK と混同しないでください)。この値は 2 通りの方法で使用できます。 WSACancelAsyncRequest を使用して操作を取り消すために使用するか、wParam メッセージパラメーターを調べることで非同期操作と完了メッセージを対応付けるために使用できます。
非同期操作を開始できなかった場合、 WSAAsyncGetProtoByNumber はゼロを返し、 WSAGetLastError を呼び出すことで特定のエラー番号を取得できます。
アプリケーションのウィンドウがメッセージを受け取ったときには、次のエラーコードが設定される場合があります。前述のとおり、これらは応答メッセージの lParam から WSAGETASYNCERROR マクロを使用して取り出すことができます。
| エラーコード | 意味 |
|---|---|
| ネットワークサブシステムが失敗しました。 | |
| 利用可能なバッファー領域が不足しています。 | |
| buf パラメーターがプロセスのアドレス空間の有効な部分にありません。 | |
| 権威のある応答でプロトコルが見つかりませんでした。 | |
| 権威のない応答でプロトコルが見つからなかったか、サーバー障害が発生しました。 | |
| 回復不能なエラーです。プロトコルデータベースにアクセスできません。 | |
| 名前は有効ですが、要求された種類のデータレコードがありません。 |
次のエラーは関数呼び出しの時点で発生する可能性があり、非同期操作を開始できなかったことを示します。
| エラーコード | 意味 |
|---|---|
| WSANOTINITIALISED | この関数を使用する前に、 WSAStartup の呼び出しが成功している必要があります。 |
| WSAENETDOWN | ネットワークサブシステムが失敗しました。 |
| WSAEINPROGRESS | ブロッキング動作の Windows Sockets 1.1 呼び出しが進行中であるか、サービスプロバイダーがコールバック関数を処理中です。 |
| WSAEWOULDBLOCK | Windows Sockets 実装内のリソースその他の制約により、現時点では非同期操作をスケジュールできません。 |
解説(Remarks)
WSAAsyncGetProtoByNumber 関数は getprotobynumber の非同期版であり、プロトコル番号に対応するプロトコル名と番号を取得するために使用します。Windows Sockets は操作を開始するとすぐに呼び出し元に制御を戻し、アプリケーションが操作を識別するために使用できる、不透明な非同期タスクハンドルを返します。操作が完了すると、結果 (存在する場合) が呼び出し元の指定したバッファーにコピーされ、メッセージがアプリケーションのウィンドウに送信されます。
非同期操作が完了すると、hWnd パラメーターで指定したアプリケーションウィンドウが wMsg パラメーターのメッセージを受け取ります。wParam パラメーターには、元の関数呼び出しが返した非同期タスクハンドルが格納されます。lParam の上位 16 ビットにはエラーコードが格納されます。エラーコードには Winsock2.h で定義されている任意のエラーが入る可能性があります。エラーコードがゼロの場合は、非同期操作が正常に完了したことを示します。
正常に完了すると、元の関数呼び出しで指定したバッファーには protoent 構造体が格納されます。この構造体のメンバーにアクセスするには、元のバッファーアドレスを protoent 構造体ポインターにキャストし、適宜アクセスします。
エラーコードが WSAENOBUFS の場合、元の呼び出しで buflen によって指定されたバッファーのサイズが、結果として得られるすべての情報を格納するには小さすぎたことを意味します。この場合、lParam の下位 16 ビットには、必要なすべての情報を提供するために必要なバッファーのサイズが格納されます。アプリケーションが部分的なデータでは不十分と判断した場合は、必要なすべての情報を受け取るのに十分な大きさのバッファー (つまり、lParam の下位 16 ビット以上の大きさ) を用意して WSAAsyncGetProtoByNumber 関数呼び出しを再発行できます。
この関数に指定するバッファーは、Windows Sockets が protoent 構造体を、同じ protoent 構造体のメンバーから参照されるデータ領域の内容とともに構築するために使用します。前述の WSAENOBUFS エラーを避けるため、アプリケーションは少なくとも MAXGETHOSTSTRUCT バイト (Winsock2.h で定義されています) のバッファーを用意する必要があります。
エラーコードとバッファー長は、Winsock2.h で次のように定義されているマクロ WSAGETASYNCERROR および WSAGETASYNCBUFLEN を使用して lParam から取り出す必要があります。
#include <windows.h>
#define WSAGETASYNCBUFLEN(lParam) LOWORD(lParam)
#define WSAGETASYNCERROR(lParam) HIWORD(lParam)
これらのマクロを使用することで、アプリケーションのソースコードの移植性を最大化できます。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
HANDLE WSAAsyncGetProtoByNumber(
HWND hWnd,
DWORD wMsg,
INT number,
LPSTR buf,
INT buflen
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr WSAAsyncGetProtoByNumber(
IntPtr hWnd, // HWND
uint wMsg, // DWORD
int number, // INT
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, // LPSTR out
int buflen // INT
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAAsyncGetProtoByNumber(
hWnd As IntPtr, ' HWND
wMsg As UInteger, ' DWORD
number As Integer, ' INT
<MarshalAs(UnmanagedType.LPStr)> buf As System.Text.StringBuilder, ' LPSTR out
buflen As Integer ' INT
) As IntPtr
End Function' hWnd : HWND
' wMsg : DWORD
' number : INT
' buf : LPSTR out
' buflen : INT
Declare PtrSafe Function WSAAsyncGetProtoByNumber Lib "ws2_32" ( _
ByVal hWnd As LongPtr, _
ByVal wMsg As Long, _
ByVal number As Long, _
ByVal buf As String, _
ByVal buflen As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WSAAsyncGetProtoByNumber = ctypes.windll.ws2_32.WSAAsyncGetProtoByNumber
WSAAsyncGetProtoByNumber.restype = ctypes.c_void_p
WSAAsyncGetProtoByNumber.argtypes = [
wintypes.HANDLE, # hWnd : HWND
wintypes.DWORD, # wMsg : DWORD
ctypes.c_int, # number : INT
wintypes.LPSTR, # buf : LPSTR out
ctypes.c_int, # buflen : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WS2_32.dll')
WSAAsyncGetProtoByNumber = Fiddle::Function.new(
lib['WSAAsyncGetProtoByNumber'],
[
Fiddle::TYPE_VOIDP, # hWnd : HWND
-Fiddle::TYPE_INT, # wMsg : DWORD
Fiddle::TYPE_INT, # number : INT
Fiddle::TYPE_VOIDP, # buf : LPSTR out
Fiddle::TYPE_INT, # buflen : INT
],
Fiddle::TYPE_VOIDP)#[link(name = "ws2_32")]
extern "system" {
fn WSAAsyncGetProtoByNumber(
hWnd: *mut core::ffi::c_void, // HWND
wMsg: u32, // DWORD
number: i32, // INT
buf: *mut u8, // LPSTR out
buflen: i32 // INT
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern IntPtr WSAAsyncGetProtoByNumber(IntPtr hWnd, uint wMsg, int number, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, int buflen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAAsyncGetProtoByNumber' -Namespace Win32 -PassThru
# $api::WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen)#uselib "WS2_32.dll"
#func global WSAAsyncGetProtoByNumber "WSAAsyncGetProtoByNumber" sptr, sptr, sptr, sptr, sptr
; WSAAsyncGetProtoByNumber hWnd, wMsg, number, varptr(buf), buflen ; 戻り値は stat
; hWnd : HWND -> "sptr"
; wMsg : DWORD -> "sptr"
; number : INT -> "sptr"
; buf : LPSTR out -> "sptr"
; buflen : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WS2_32.dll" #cfunc global WSAAsyncGetProtoByNumber "WSAAsyncGetProtoByNumber" sptr, int, int, var, int ; res = WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen) ; hWnd : HWND -> "sptr" ; wMsg : DWORD -> "int" ; number : INT -> "int" ; buf : LPSTR out -> "var" ; buflen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global WSAAsyncGetProtoByNumber "WSAAsyncGetProtoByNumber" sptr, int, int, sptr, int ; res = WSAAsyncGetProtoByNumber(hWnd, wMsg, number, varptr(buf), buflen) ; hWnd : HWND -> "sptr" ; wMsg : DWORD -> "int" ; number : INT -> "int" ; buf : LPSTR out -> "sptr" ; buflen : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HANDLE WSAAsyncGetProtoByNumber(HWND hWnd, DWORD wMsg, INT number, LPSTR buf, INT buflen) #uselib "WS2_32.dll" #cfunc global WSAAsyncGetProtoByNumber "WSAAsyncGetProtoByNumber" intptr, int, int, var, int ; res = WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen) ; hWnd : HWND -> "intptr" ; wMsg : DWORD -> "int" ; number : INT -> "int" ; buf : LPSTR out -> "var" ; buflen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HANDLE WSAAsyncGetProtoByNumber(HWND hWnd, DWORD wMsg, INT number, LPSTR buf, INT buflen) #uselib "WS2_32.dll" #cfunc global WSAAsyncGetProtoByNumber "WSAAsyncGetProtoByNumber" intptr, int, int, intptr, int ; res = WSAAsyncGetProtoByNumber(hWnd, wMsg, number, varptr(buf), buflen) ; hWnd : HWND -> "intptr" ; wMsg : DWORD -> "int" ; number : INT -> "int" ; buf : LPSTR out -> "intptr" ; buflen : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procWSAAsyncGetProtoByNumber = ws2_32.NewProc("WSAAsyncGetProtoByNumber")
)
// hWnd (HWND), wMsg (DWORD), number (INT), buf (LPSTR out), buflen (INT)
r1, _, err := procWSAAsyncGetProtoByNumber.Call(
uintptr(hWnd),
uintptr(wMsg),
uintptr(number),
uintptr(buf),
uintptr(buflen),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction WSAAsyncGetProtoByNumber(
hWnd: THandle; // HWND
wMsg: DWORD; // DWORD
number: Integer; // INT
buf: PAnsiChar; // LPSTR out
buflen: Integer // INT
): THandle; stdcall;
external 'WS2_32.dll' name 'WSAAsyncGetProtoByNumber';result := DllCall("WS2_32\WSAAsyncGetProtoByNumber"
, "Ptr", hWnd ; HWND
, "UInt", wMsg ; DWORD
, "Int", number ; INT
, "Ptr", buf ; LPSTR out
, "Int", buflen ; INT
, "Ptr") ; return: HANDLE●WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen) = DLL("WS2_32.dll", "void* WSAAsyncGetProtoByNumber(void*, dword, int, char*, int)")
# 呼び出し: WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen)
# hWnd : HWND -> "void*"
# wMsg : DWORD -> "dword"
# number : INT -> "int"
# buf : LPSTR out -> "char*"
# buflen : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ws2_32" fn WSAAsyncGetProtoByNumber(
hWnd: ?*anyopaque, // HWND
wMsg: u32, // DWORD
number: i32, // INT
buf: [*c]u8, // LPSTR out
buflen: i32 // INT
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc WSAAsyncGetProtoByNumber(
hWnd: pointer, # HWND
wMsg: uint32, # DWORD
number: int32, # INT
buf: ptr char, # LPSTR out
buflen: int32 # INT
): pointer {.importc: "WSAAsyncGetProtoByNumber", stdcall, dynlib: "WS2_32.dll".}pragma(lib, "ws2_32");
extern(Windows)
void* WSAAsyncGetProtoByNumber(
void* hWnd, // HWND
uint wMsg, // DWORD
int number, // INT
char* buf, // LPSTR out
int buflen // INT
);ccall((:WSAAsyncGetProtoByNumber, "WS2_32.dll"), stdcall, Ptr{Cvoid},
(Ptr{Cvoid}, UInt32, Int32, Ptr{UInt8}, Int32),
hWnd, wMsg, number, buf, buflen)
# hWnd : HWND -> Ptr{Cvoid}
# wMsg : DWORD -> UInt32
# number : INT -> Int32
# buf : LPSTR out -> Ptr{UInt8}
# buflen : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* WSAAsyncGetProtoByNumber(
void* hWnd,
uint32_t wMsg,
int32_t number,
char* buf,
int32_t buflen);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen)
-- hWnd : HWND
-- wMsg : DWORD
-- number : INT
-- buf : LPSTR out
-- buflen : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAAsyncGetProtoByNumber = lib.func('__stdcall', 'WSAAsyncGetProtoByNumber', 'void *', ['void *', 'uint32_t', 'int32_t', 'char *', 'int32_t']);
// WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen)
// hWnd : HWND -> 'void *'
// wMsg : DWORD -> 'uint32_t'
// number : INT -> 'int32_t'
// buf : LPSTR out -> 'char *'
// buflen : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WS2_32.dll", {
WSAAsyncGetProtoByNumber: { parameters: ["pointer", "u32", "i32", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen)
// hWnd : HWND -> "pointer"
// wMsg : DWORD -> "u32"
// number : INT -> "i32"
// buf : LPSTR out -> "buffer"
// buflen : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* WSAAsyncGetProtoByNumber(
void* hWnd,
uint32_t wMsg,
int32_t number,
char* buf,
int32_t buflen);
C, "WS2_32.dll");
// $ffi->WSAAsyncGetProtoByNumber(hWnd, wMsg, number, buf, buflen);
// hWnd : HWND
// wMsg : DWORD
// number : INT
// buf : LPSTR out
// buflen : INT
// 構造体/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 Ws2_32 extends StdCallLibrary {
Ws2_32 INSTANCE = Native.load("ws2_32", Ws2_32.class);
Pointer WSAAsyncGetProtoByNumber(
Pointer hWnd, // HWND
int wMsg, // DWORD
int number, // INT
byte[] buf, // LPSTR out
int buflen // INT
);
}@[Link("ws2_32")]
lib LibWS2_32
fun WSAAsyncGetProtoByNumber = WSAAsyncGetProtoByNumber(
hWnd : Void*, # HWND
wMsg : UInt32, # DWORD
number : Int32, # INT
buf : UInt8*, # LPSTR out
buflen : Int32 # INT
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WSAAsyncGetProtoByNumberNative = Pointer<Void> Function(Pointer<Void>, Uint32, Int32, Pointer<Utf8>, Int32);
typedef WSAAsyncGetProtoByNumberDart = Pointer<Void> Function(Pointer<Void>, int, int, Pointer<Utf8>, int);
final WSAAsyncGetProtoByNumber = DynamicLibrary.open('WS2_32.dll')
.lookupFunction<WSAAsyncGetProtoByNumberNative, WSAAsyncGetProtoByNumberDart>('WSAAsyncGetProtoByNumber');
// hWnd : HWND -> Pointer<Void>
// wMsg : DWORD -> Uint32
// number : INT -> Int32
// buf : LPSTR out -> Pointer<Utf8>
// buflen : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WSAAsyncGetProtoByNumber(
hWnd: THandle; // HWND
wMsg: DWORD; // DWORD
number: Integer; // INT
buf: PAnsiChar; // LPSTR out
buflen: Integer // INT
): THandle; stdcall;
external 'WS2_32.dll' name 'WSAAsyncGetProtoByNumber';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WSAAsyncGetProtoByNumber"
c_WSAAsyncGetProtoByNumber :: Ptr () -> Word32 -> Int32 -> CString -> Int32 -> IO (Ptr ())
-- hWnd : HWND -> Ptr ()
-- wMsg : DWORD -> Word32
-- number : INT -> Int32
-- buf : LPSTR out -> CString
-- buflen : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wsaasyncgetprotobynumber =
foreign "WSAAsyncGetProtoByNumber"
((ptr void) @-> uint32_t @-> int32_t @-> string @-> int32_t @-> returning (ptr void))
(* hWnd : HWND -> (ptr void) *)
(* wMsg : DWORD -> uint32_t *)
(* number : INT -> int32_t *)
(* buf : LPSTR out -> string *)
(* buflen : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ws2_32 (t "WS2_32.dll"))
(cffi:use-foreign-library ws2_32)
(cffi:defcfun ("WSAAsyncGetProtoByNumber" wsaasync-get-proto-by-number :convention :stdcall) :pointer
(h-wnd :pointer) ; HWND
(w-msg :uint32) ; DWORD
(number :int32) ; INT
(buf :pointer) ; LPSTR out
(buflen :int32)) ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WSAAsyncGetProtoByNumber = Win32::API::More->new('WS2_32',
'HANDLE WSAAsyncGetProtoByNumber(HANDLE hWnd, DWORD wMsg, int number, LPSTR buf, int buflen)');
# my $ret = $WSAAsyncGetProtoByNumber->Call($hWnd, $wMsg, $number, $buf, $buflen);
# hWnd : HWND -> HANDLE
# wMsg : DWORD -> DWORD
# number : INT -> int
# buf : LPSTR out -> LPSTR
# buflen : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f WSACancelAsyncRequest — 進行中の非同期データベース問い合わせを取り消す。
- f getprotobynumber — プロトコル番号に対応するプロトコル情報を取得する。