WSAAsyncGetHostByAddr
関数シグネチャ
// WS2_32.dll
#include <windows.h>
HANDLE WSAAsyncGetHostByAddr(
HWND hWnd,
DWORD wMsg,
LPCSTR addr,
INT len,
INT type,
LPSTR buf,
INT buflen
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hWnd | HWND | in | 完了時にメッセージを受け取るウィンドウのハンドル。 |
| wMsg | DWORD | in | 操作完了時にウィンドウへポストするメッセージID。 |
| addr | LPCSTR | in | 検索対象のネットワークアドレスへのポインタ(ネットワークバイトオーダー)。 |
| len | INT | in | addrが指すアドレスのバイト長(IPv4なら4)。 |
| type | INT | in | アドレスファミリを指定する(通常AF_INET)。 |
| buf | LPSTR | out | 結果のhostent構造体を格納するバッファへのポインタ。呼び出し側が確保する。 |
| buflen | INT | in | bufバッファのサイズをバイト単位で指定する。MAXGETHOSTSTRUCT推奨。 |
戻り値の型: HANDLE
公式ドキュメント
WSAAsyncGetHostByAddr マクロ関数 (wsipv6ok.h) は、あるアドレスに対応するホスト情報を非同期に取得します。
解説(Remarks)
WSAAsyncGetHostByAddr 関数は gethostbyaddr の非同期版です。ネットワークアドレスに対応するホスト名とアドレス情報を取得するために使用します。Windows Sockets は操作を開始すると直ちに呼び出し元へ戻り、アプリケーションが操作を識別するために使用できる不透明な非同期タスクハンドルを返します。操作が完了すると、結果 (存在する場合) は呼び出し元が用意したバッファーへコピーされ、アプリケーションのウィンドウへメッセージが送信されます。
非同期操作が完了すると、hWnd パラメーターで指定されたアプリケーションウィンドウが wMsg パラメーターのメッセージを受け取ります。wParam パラメーターには、元の関数呼び出しが返した非同期タスクハンドルが格納されます。lParam の上位 16 ビットにはエラーコード (存在する場合) が格納されます。エラーコードは Winsock2.h で定義されている任意のエラーになり得ます。エラーコードがゼロの場合は、非同期操作が正常に完了したことを示します。
正常に完了すると、元の関数呼び出しで指定したバッファーには hostent 構造体が格納されます。この構造体のメンバーにアクセスするには、元のバッファーアドレスを hostent 構造体ポインターにキャストし、適切にアクセスします。
エラーコードが WSAENOBUFS の場合、元の呼び出しで buflen に指定したバッファーのサイズが、結果として得られるすべての情報を格納するには小さすぎたことを意味します。この場合、lParam の下位 16 ビットには、必要なすべての情報を得るために必要なバッファーのサイズが格納されます。アプリケーションが部分的なデータでは不十分と判断した場合は、必要なすべての情報を受け取るのに十分な大きさのバッファー (すなわち lParam の下位 16 ビットを下回らないサイズ) を指定して WSAAsyncGetHostByAddr 関数呼び出しを再発行できます。
この関数に指定するバッファーは、Windows Sockets が同一の hostent 構造体のメンバーから参照されるデータ領域の内容とともに構造体を構築するために使用します。 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 WSAAsyncGetHostByAddr(
HWND hWnd,
DWORD wMsg,
LPCSTR addr,
INT len,
INT type,
LPSTR buf,
INT buflen
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr WSAAsyncGetHostByAddr(
IntPtr hWnd, // HWND
uint wMsg, // DWORD
[MarshalAs(UnmanagedType.LPStr)] string addr, // LPCSTR
int len, // INT
int type, // INT
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, // LPSTR out
int buflen // INT
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAAsyncGetHostByAddr(
hWnd As IntPtr, ' HWND
wMsg As UInteger, ' DWORD
<MarshalAs(UnmanagedType.LPStr)> addr As String, ' LPCSTR
len As Integer, ' INT
type 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
' addr : LPCSTR
' len : INT
' type : INT
' buf : LPSTR out
' buflen : INT
Declare PtrSafe Function WSAAsyncGetHostByAddr Lib "ws2_32" ( _
ByVal hWnd As LongPtr, _
ByVal wMsg As Long, _
ByVal addr As String, _
ByVal len As Long, _
ByVal type 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
WSAAsyncGetHostByAddr = ctypes.windll.ws2_32.WSAAsyncGetHostByAddr
WSAAsyncGetHostByAddr.restype = ctypes.c_void_p
WSAAsyncGetHostByAddr.argtypes = [
wintypes.HANDLE, # hWnd : HWND
wintypes.DWORD, # wMsg : DWORD
wintypes.LPCSTR, # addr : LPCSTR
ctypes.c_int, # len : INT
ctypes.c_int, # type : 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')
WSAAsyncGetHostByAddr = Fiddle::Function.new(
lib['WSAAsyncGetHostByAddr'],
[
Fiddle::TYPE_VOIDP, # hWnd : HWND
-Fiddle::TYPE_INT, # wMsg : DWORD
Fiddle::TYPE_VOIDP, # addr : LPCSTR
Fiddle::TYPE_INT, # len : INT
Fiddle::TYPE_INT, # type : INT
Fiddle::TYPE_VOIDP, # buf : LPSTR out
Fiddle::TYPE_INT, # buflen : INT
],
Fiddle::TYPE_VOIDP)#[link(name = "ws2_32")]
extern "system" {
fn WSAAsyncGetHostByAddr(
hWnd: *mut core::ffi::c_void, // HWND
wMsg: u32, // DWORD
addr: *const u8, // LPCSTR
len: i32, // INT
type: 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 WSAAsyncGetHostByAddr(IntPtr hWnd, uint wMsg, [MarshalAs(UnmanagedType.LPStr)] string addr, int len, int type, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, int buflen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAAsyncGetHostByAddr' -Namespace Win32 -PassThru
# $api::WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen)#uselib "WS2_32.dll"
#func global WSAAsyncGetHostByAddr "WSAAsyncGetHostByAddr" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; WSAAsyncGetHostByAddr hWnd, wMsg, addr, len, type, varptr(buf), buflen ; 戻り値は stat
; hWnd : HWND -> "sptr"
; wMsg : DWORD -> "sptr"
; addr : LPCSTR -> "sptr"
; len : INT -> "sptr"
; type : INT -> "sptr"
; buf : LPSTR out -> "sptr"
; buflen : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WS2_32.dll" #cfunc global WSAAsyncGetHostByAddr "WSAAsyncGetHostByAddr" sptr, int, str, int, int, var, int ; res = WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen) ; hWnd : HWND -> "sptr" ; wMsg : DWORD -> "int" ; addr : LPCSTR -> "str" ; len : INT -> "int" ; type : INT -> "int" ; buf : LPSTR out -> "var" ; buflen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global WSAAsyncGetHostByAddr "WSAAsyncGetHostByAddr" sptr, int, str, int, int, sptr, int ; res = WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, varptr(buf), buflen) ; hWnd : HWND -> "sptr" ; wMsg : DWORD -> "int" ; addr : LPCSTR -> "str" ; len : INT -> "int" ; type : INT -> "int" ; buf : LPSTR out -> "sptr" ; buflen : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; HANDLE WSAAsyncGetHostByAddr(HWND hWnd, DWORD wMsg, LPCSTR addr, INT len, INT type, LPSTR buf, INT buflen) #uselib "WS2_32.dll" #cfunc global WSAAsyncGetHostByAddr "WSAAsyncGetHostByAddr" intptr, int, str, int, int, var, int ; res = WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen) ; hWnd : HWND -> "intptr" ; wMsg : DWORD -> "int" ; addr : LPCSTR -> "str" ; len : INT -> "int" ; type : INT -> "int" ; buf : LPSTR out -> "var" ; buflen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HANDLE WSAAsyncGetHostByAddr(HWND hWnd, DWORD wMsg, LPCSTR addr, INT len, INT type, LPSTR buf, INT buflen) #uselib "WS2_32.dll" #cfunc global WSAAsyncGetHostByAddr "WSAAsyncGetHostByAddr" intptr, int, str, int, int, intptr, int ; res = WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, varptr(buf), buflen) ; hWnd : HWND -> "intptr" ; wMsg : DWORD -> "int" ; addr : LPCSTR -> "str" ; len : INT -> "int" ; type : 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")
procWSAAsyncGetHostByAddr = ws2_32.NewProc("WSAAsyncGetHostByAddr")
)
// hWnd (HWND), wMsg (DWORD), addr (LPCSTR), len (INT), type (INT), buf (LPSTR out), buflen (INT)
r1, _, err := procWSAAsyncGetHostByAddr.Call(
uintptr(hWnd),
uintptr(wMsg),
uintptr(unsafe.Pointer(windows.BytePtrFromString(addr))),
uintptr(len),
uintptr(type),
uintptr(buf),
uintptr(buflen),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction WSAAsyncGetHostByAddr(
hWnd: THandle; // HWND
wMsg: DWORD; // DWORD
addr: PAnsiChar; // LPCSTR
len: Integer; // INT
type: Integer; // INT
buf: PAnsiChar; // LPSTR out
buflen: Integer // INT
): THandle; stdcall;
external 'WS2_32.dll' name 'WSAAsyncGetHostByAddr';result := DllCall("WS2_32\WSAAsyncGetHostByAddr"
, "Ptr", hWnd ; HWND
, "UInt", wMsg ; DWORD
, "AStr", addr ; LPCSTR
, "Int", len ; INT
, "Int", type ; INT
, "Ptr", buf ; LPSTR out
, "Int", buflen ; INT
, "Ptr") ; return: HANDLE●WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen) = DLL("WS2_32.dll", "void* WSAAsyncGetHostByAddr(void*, dword, char*, int, int, char*, int)")
# 呼び出し: WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen)
# hWnd : HWND -> "void*"
# wMsg : DWORD -> "dword"
# addr : LPCSTR -> "char*"
# len : INT -> "int"
# type : 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 WSAAsyncGetHostByAddr(
hWnd: ?*anyopaque, // HWND
wMsg: u32, // DWORD
addr: [*c]const u8, // LPCSTR
len: i32, // INT
type: i32, // INT
buf: [*c]u8, // LPSTR out
buflen: i32 // INT
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc WSAAsyncGetHostByAddr(
hWnd: pointer, # HWND
wMsg: uint32, # DWORD
`addr`: cstring, # LPCSTR
len: int32, # INT
`type`: int32, # INT
buf: ptr char, # LPSTR out
buflen: int32 # INT
): pointer {.importc: "WSAAsyncGetHostByAddr", stdcall, dynlib: "WS2_32.dll".}pragma(lib, "ws2_32");
extern(Windows)
void* WSAAsyncGetHostByAddr(
void* hWnd, // HWND
uint wMsg, // DWORD
const(char)* addr, // LPCSTR
int len, // INT
int type, // INT
char* buf, // LPSTR out
int buflen // INT
);ccall((:WSAAsyncGetHostByAddr, "WS2_32.dll"), stdcall, Ptr{Cvoid},
(Ptr{Cvoid}, UInt32, Cstring, Int32, Int32, Ptr{UInt8}, Int32),
hWnd, wMsg, addr, len, type, buf, buflen)
# hWnd : HWND -> Ptr{Cvoid}
# wMsg : DWORD -> UInt32
# addr : LPCSTR -> Cstring
# len : INT -> Int32
# type : INT -> Int32
# buf : LPSTR out -> Ptr{UInt8}
# buflen : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* WSAAsyncGetHostByAddr(
void* hWnd,
uint32_t wMsg,
const char* addr,
int32_t len,
int32_t type,
char* buf,
int32_t buflen);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen)
-- hWnd : HWND
-- wMsg : DWORD
-- addr : LPCSTR
-- len : INT
-- type : INT
-- buf : LPSTR out
-- buflen : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAAsyncGetHostByAddr = lib.func('__stdcall', 'WSAAsyncGetHostByAddr', 'void *', ['void *', 'uint32_t', 'str', 'int32_t', 'int32_t', 'char *', 'int32_t']);
// WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen)
// hWnd : HWND -> 'void *'
// wMsg : DWORD -> 'uint32_t'
// addr : LPCSTR -> 'str'
// len : INT -> 'int32_t'
// type : INT -> 'int32_t'
// buf : LPSTR out -> 'char *'
// buflen : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WS2_32.dll", {
WSAAsyncGetHostByAddr: { parameters: ["pointer", "u32", "buffer", "i32", "i32", "buffer", "i32"], result: "pointer" },
});
// lib.symbols.WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen)
// hWnd : HWND -> "pointer"
// wMsg : DWORD -> "u32"
// addr : LPCSTR -> "buffer"
// len : INT -> "i32"
// type : INT -> "i32"
// buf : LPSTR out -> "buffer"
// buflen : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* WSAAsyncGetHostByAddr(
void* hWnd,
uint32_t wMsg,
const char* addr,
int32_t len,
int32_t type,
char* buf,
int32_t buflen);
C, "WS2_32.dll");
// $ffi->WSAAsyncGetHostByAddr(hWnd, wMsg, addr, len, type, buf, buflen);
// hWnd : HWND
// wMsg : DWORD
// addr : LPCSTR
// len : INT
// type : 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 WSAAsyncGetHostByAddr(
Pointer hWnd, // HWND
int wMsg, // DWORD
String addr, // LPCSTR
int len, // INT
int type, // INT
byte[] buf, // LPSTR out
int buflen // INT
);
}@[Link("ws2_32")]
lib LibWS2_32
fun WSAAsyncGetHostByAddr = WSAAsyncGetHostByAddr(
hWnd : Void*, # HWND
wMsg : UInt32, # DWORD
addr : UInt8*, # LPCSTR
len : Int32, # INT
type_ : 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 WSAAsyncGetHostByAddrNative = Pointer<Void> Function(Pointer<Void>, Uint32, Pointer<Utf8>, Int32, Int32, Pointer<Utf8>, Int32);
typedef WSAAsyncGetHostByAddrDart = Pointer<Void> Function(Pointer<Void>, int, Pointer<Utf8>, int, int, Pointer<Utf8>, int);
final WSAAsyncGetHostByAddr = DynamicLibrary.open('WS2_32.dll')
.lookupFunction<WSAAsyncGetHostByAddrNative, WSAAsyncGetHostByAddrDart>('WSAAsyncGetHostByAddr');
// hWnd : HWND -> Pointer<Void>
// wMsg : DWORD -> Uint32
// addr : LPCSTR -> Pointer<Utf8>
// len : INT -> Int32
// type : INT -> Int32
// buf : LPSTR out -> Pointer<Utf8>
// buflen : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WSAAsyncGetHostByAddr(
hWnd: THandle; // HWND
wMsg: DWORD; // DWORD
addr: PAnsiChar; // LPCSTR
len: Integer; // INT
type: Integer; // INT
buf: PAnsiChar; // LPSTR out
buflen: Integer // INT
): THandle; stdcall;
external 'WS2_32.dll' name 'WSAAsyncGetHostByAddr';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WSAAsyncGetHostByAddr"
c_WSAAsyncGetHostByAddr :: Ptr () -> Word32 -> CString -> Int32 -> Int32 -> CString -> Int32 -> IO (Ptr ())
-- hWnd : HWND -> Ptr ()
-- wMsg : DWORD -> Word32
-- addr : LPCSTR -> CString
-- len : INT -> Int32
-- type : INT -> Int32
-- buf : LPSTR out -> CString
-- buflen : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wsaasyncgethostbyaddr =
foreign "WSAAsyncGetHostByAddr"
((ptr void) @-> uint32_t @-> string @-> int32_t @-> int32_t @-> string @-> int32_t @-> returning (ptr void))
(* hWnd : HWND -> (ptr void) *)
(* wMsg : DWORD -> uint32_t *)
(* addr : LPCSTR -> string *)
(* len : INT -> int32_t *)
(* type : 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 ("WSAAsyncGetHostByAddr" wsaasync-get-host-by-addr :convention :stdcall) :pointer
(h-wnd :pointer) ; HWND
(w-msg :uint32) ; DWORD
(addr :string) ; LPCSTR
(len :int32) ; INT
(type :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 $WSAAsyncGetHostByAddr = Win32::API::More->new('WS2_32',
'HANDLE WSAAsyncGetHostByAddr(HANDLE hWnd, DWORD wMsg, LPCSTR addr, int len, int type, LPSTR buf, int buflen)');
# my $ret = $WSAAsyncGetHostByAddr->Call($hWnd, $wMsg, $addr, $len, $type, $buf, $buflen);
# hWnd : HWND -> HANDLE
# wMsg : DWORD -> DWORD
# addr : LPCSTR -> LPCSTR
# len : INT -> int
# type : INT -> int
# buf : LPSTR out -> LPSTR
# buflen : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f WSACancelAsyncRequest — 進行中の非同期データベース問い合わせを取り消す。
- f getaddrinfo — ホスト名やサービス名からアドレス情報を解決する。
- f gethostbyaddr — IPアドレスから対応するホスト情報を取得する。
- f getnameinfo — ソケットアドレスからホスト名とサービス名へ逆解決する。
- s HOSTENT