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

GetAddressByNameW

関数
サービス名から対応するアドレス情報を取得する。
DLLMSWSOCK.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

INT GetAddressByNameW(
    DWORD dwNameSpace,
    GUID* lpServiceType,
    LPWSTR lpServiceName,   // optional
    INT* lpiProtocols,   // optional
    DWORD dwResolution,
    SERVICE_ASYNC_INFO* lpServiceAsyncInfo,   // optional
    void* lpCsaddrBuffer,
    DWORD* lpdwBufferLength,
    LPWSTR lpAliasBuffer,   // optional
    DWORD* lpdwAliasBufferLength
);

パラメーター

名前方向
dwNameSpaceDWORDin
lpServiceTypeGUID*in
lpServiceNameLPWSTRinoptional
lpiProtocolsINT*inoptional
dwResolutionDWORDin
lpServiceAsyncInfoSERVICE_ASYNC_INFO*inoptional
lpCsaddrBuffervoid*out
lpdwBufferLengthDWORD*inout
lpAliasBufferLPWSTRinoutoptional
lpdwAliasBufferLengthDWORD*inout

戻り値の型: INT

各言語での呼び出し定義

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

INT GetAddressByNameW(
    DWORD dwNameSpace,
    GUID* lpServiceType,
    LPWSTR lpServiceName,   // optional
    INT* lpiProtocols,   // optional
    DWORD dwResolution,
    SERVICE_ASYNC_INFO* lpServiceAsyncInfo,   // optional
    void* lpCsaddrBuffer,
    DWORD* lpdwBufferLength,
    LPWSTR lpAliasBuffer,   // optional
    DWORD* lpdwAliasBufferLength
);
[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int GetAddressByNameW(
    uint dwNameSpace,   // DWORD
    ref Guid lpServiceType,   // GUID*
    [MarshalAs(UnmanagedType.LPWStr)] string lpServiceName,   // LPWSTR optional
    IntPtr lpiProtocols,   // INT* optional
    uint dwResolution,   // DWORD
    IntPtr lpServiceAsyncInfo,   // SERVICE_ASYNC_INFO* optional
    IntPtr lpCsaddrBuffer,   // void* out
    ref uint lpdwBufferLength,   // DWORD* in/out
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpAliasBuffer,   // LPWSTR optional, in/out
    ref uint lpdwAliasBufferLength   // DWORD* in/out
);
<DllImport("MSWSOCK.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetAddressByNameW(
    dwNameSpace As UInteger,   ' DWORD
    ByRef lpServiceType As Guid,   ' GUID*
    <MarshalAs(UnmanagedType.LPWStr)> lpServiceName As String,   ' LPWSTR optional
    lpiProtocols As IntPtr,   ' INT* optional
    dwResolution As UInteger,   ' DWORD
    lpServiceAsyncInfo As IntPtr,   ' SERVICE_ASYNC_INFO* optional
    lpCsaddrBuffer As IntPtr,   ' void* out
    ByRef lpdwBufferLength As UInteger,   ' DWORD* in/out
    <MarshalAs(UnmanagedType.LPWStr)> lpAliasBuffer As System.Text.StringBuilder,   ' LPWSTR optional, in/out
    ByRef lpdwAliasBufferLength As UInteger   ' DWORD* in/out
) As Integer
End Function
' dwNameSpace : DWORD
' lpServiceType : GUID*
' lpServiceName : LPWSTR optional
' lpiProtocols : INT* optional
' dwResolution : DWORD
' lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional
' lpCsaddrBuffer : void* out
' lpdwBufferLength : DWORD* in/out
' lpAliasBuffer : LPWSTR optional, in/out
' lpdwAliasBufferLength : DWORD* in/out
Declare PtrSafe Function GetAddressByNameW Lib "mswsock" ( _
    ByVal dwNameSpace As Long, _
    ByVal lpServiceType As LongPtr, _
    ByVal lpServiceName As LongPtr, _
    ByVal lpiProtocols As LongPtr, _
    ByVal dwResolution As Long, _
    ByVal lpServiceAsyncInfo As LongPtr, _
    ByVal lpCsaddrBuffer As LongPtr, _
    ByRef lpdwBufferLength As Long, _
    ByVal lpAliasBuffer As LongPtr, _
    ByRef lpdwAliasBufferLength As Long) As Long
' 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

GetAddressByNameW = ctypes.windll.mswsock.GetAddressByNameW
GetAddressByNameW.restype = ctypes.c_int
GetAddressByNameW.argtypes = [
    wintypes.DWORD,  # dwNameSpace : DWORD
    ctypes.c_void_p,  # lpServiceType : GUID*
    wintypes.LPCWSTR,  # lpServiceName : LPWSTR optional
    ctypes.POINTER(ctypes.c_int),  # lpiProtocols : INT* optional
    wintypes.DWORD,  # dwResolution : DWORD
    ctypes.c_void_p,  # lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional
    ctypes.POINTER(None),  # lpCsaddrBuffer : void* out
    ctypes.POINTER(wintypes.DWORD),  # lpdwBufferLength : DWORD* in/out
    wintypes.LPWSTR,  # lpAliasBuffer : LPWSTR optional, in/out
    ctypes.POINTER(wintypes.DWORD),  # lpdwAliasBufferLength : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MSWSOCK.dll')
GetAddressByNameW = Fiddle::Function.new(
  lib['GetAddressByNameW'],
  [
    -Fiddle::TYPE_INT,  # dwNameSpace : DWORD
    Fiddle::TYPE_VOIDP,  # lpServiceType : GUID*
    Fiddle::TYPE_VOIDP,  # lpServiceName : LPWSTR optional
    Fiddle::TYPE_VOIDP,  # lpiProtocols : INT* optional
    -Fiddle::TYPE_INT,  # dwResolution : DWORD
    Fiddle::TYPE_VOIDP,  # lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional
    Fiddle::TYPE_VOIDP,  # lpCsaddrBuffer : void* out
    Fiddle::TYPE_VOIDP,  # lpdwBufferLength : DWORD* in/out
    Fiddle::TYPE_VOIDP,  # lpAliasBuffer : LPWSTR optional, in/out
    Fiddle::TYPE_VOIDP,  # lpdwAliasBufferLength : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "mswsock")]
extern "system" {
    fn GetAddressByNameW(
        dwNameSpace: u32,  // DWORD
        lpServiceType: *mut GUID,  // GUID*
        lpServiceName: *mut u16,  // LPWSTR optional
        lpiProtocols: *mut i32,  // INT* optional
        dwResolution: u32,  // DWORD
        lpServiceAsyncInfo: *mut SERVICE_ASYNC_INFO,  // SERVICE_ASYNC_INFO* optional
        lpCsaddrBuffer: *mut (),  // void* out
        lpdwBufferLength: *mut u32,  // DWORD* in/out
        lpAliasBuffer: *mut u16,  // LPWSTR optional, in/out
        lpdwAliasBufferLength: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetAddressByNameW(uint dwNameSpace, ref Guid lpServiceType, [MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, IntPtr lpiProtocols, uint dwResolution, IntPtr lpServiceAsyncInfo, IntPtr lpCsaddrBuffer, ref uint lpdwBufferLength, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpAliasBuffer, ref uint lpdwAliasBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_GetAddressByNameW' -Namespace Win32 -PassThru
# $api::GetAddressByNameW(dwNameSpace, lpServiceType, lpServiceName, lpiProtocols, dwResolution, lpServiceAsyncInfo, lpCsaddrBuffer, lpdwBufferLength, lpAliasBuffer, lpdwAliasBufferLength)
#uselib "MSWSOCK.dll"
#func global GetAddressByNameW "GetAddressByNameW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; GetAddressByNameW dwNameSpace, varptr(lpServiceType), lpServiceName, varptr(lpiProtocols), dwResolution, varptr(lpServiceAsyncInfo), lpCsaddrBuffer, varptr(lpdwBufferLength), varptr(lpAliasBuffer), varptr(lpdwAliasBufferLength)   ; 戻り値は stat
; dwNameSpace : DWORD -> "wptr"
; lpServiceType : GUID* -> "wptr"
; lpServiceName : LPWSTR optional -> "wptr"
; lpiProtocols : INT* optional -> "wptr"
; dwResolution : DWORD -> "wptr"
; lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional -> "wptr"
; lpCsaddrBuffer : void* out -> "wptr"
; lpdwBufferLength : DWORD* in/out -> "wptr"
; lpAliasBuffer : LPWSTR optional, in/out -> "wptr"
; lpdwAliasBufferLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MSWSOCK.dll"
#cfunc global GetAddressByNameW "GetAddressByNameW" int, var, wstr, var, int, var, sptr, var, var, var
; res = GetAddressByNameW(dwNameSpace, lpServiceType, lpServiceName, lpiProtocols, dwResolution, lpServiceAsyncInfo, lpCsaddrBuffer, lpdwBufferLength, lpAliasBuffer, lpdwAliasBufferLength)
; dwNameSpace : DWORD -> "int"
; lpServiceType : GUID* -> "var"
; lpServiceName : LPWSTR optional -> "wstr"
; lpiProtocols : INT* optional -> "var"
; dwResolution : DWORD -> "int"
; lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional -> "var"
; lpCsaddrBuffer : void* out -> "sptr"
; lpdwBufferLength : DWORD* in/out -> "var"
; lpAliasBuffer : LPWSTR optional, in/out -> "var"
; lpdwAliasBufferLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetAddressByNameW(DWORD dwNameSpace, GUID* lpServiceType, LPWSTR lpServiceName, INT* lpiProtocols, DWORD dwResolution, SERVICE_ASYNC_INFO* lpServiceAsyncInfo, void* lpCsaddrBuffer, DWORD* lpdwBufferLength, LPWSTR lpAliasBuffer, DWORD* lpdwAliasBufferLength)
#uselib "MSWSOCK.dll"
#cfunc global GetAddressByNameW "GetAddressByNameW" int, var, wstr, var, int, var, intptr, var, var, var
; res = GetAddressByNameW(dwNameSpace, lpServiceType, lpServiceName, lpiProtocols, dwResolution, lpServiceAsyncInfo, lpCsaddrBuffer, lpdwBufferLength, lpAliasBuffer, lpdwAliasBufferLength)
; dwNameSpace : DWORD -> "int"
; lpServiceType : GUID* -> "var"
; lpServiceName : LPWSTR optional -> "wstr"
; lpiProtocols : INT* optional -> "var"
; dwResolution : DWORD -> "int"
; lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional -> "var"
; lpCsaddrBuffer : void* out -> "intptr"
; lpdwBufferLength : DWORD* in/out -> "var"
; lpAliasBuffer : LPWSTR optional, in/out -> "var"
; lpdwAliasBufferLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procGetAddressByNameW = mswsock.NewProc("GetAddressByNameW")
)

// dwNameSpace (DWORD), lpServiceType (GUID*), lpServiceName (LPWSTR optional), lpiProtocols (INT* optional), dwResolution (DWORD), lpServiceAsyncInfo (SERVICE_ASYNC_INFO* optional), lpCsaddrBuffer (void* out), lpdwBufferLength (DWORD* in/out), lpAliasBuffer (LPWSTR optional, in/out), lpdwAliasBufferLength (DWORD* in/out)
r1, _, err := procGetAddressByNameW.Call(
	uintptr(dwNameSpace),
	uintptr(lpServiceType),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpServiceName))),
	uintptr(lpiProtocols),
	uintptr(dwResolution),
	uintptr(lpServiceAsyncInfo),
	uintptr(lpCsaddrBuffer),
	uintptr(lpdwBufferLength),
	uintptr(lpAliasBuffer),
	uintptr(lpdwAliasBufferLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function GetAddressByNameW(
  dwNameSpace: DWORD;   // DWORD
  lpServiceType: PGUID;   // GUID*
  lpServiceName: PWideChar;   // LPWSTR optional
  lpiProtocols: Pointer;   // INT* optional
  dwResolution: DWORD;   // DWORD
  lpServiceAsyncInfo: Pointer;   // SERVICE_ASYNC_INFO* optional
  lpCsaddrBuffer: Pointer;   // void* out
  lpdwBufferLength: Pointer;   // DWORD* in/out
  lpAliasBuffer: PWideChar;   // LPWSTR optional, in/out
  lpdwAliasBufferLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'GetAddressByNameW';
result := DllCall("MSWSOCK\GetAddressByNameW"
    , "UInt", dwNameSpace   ; DWORD
    , "Ptr", lpServiceType   ; GUID*
    , "WStr", lpServiceName   ; LPWSTR optional
    , "Ptr", lpiProtocols   ; INT* optional
    , "UInt", dwResolution   ; DWORD
    , "Ptr", lpServiceAsyncInfo   ; SERVICE_ASYNC_INFO* optional
    , "Ptr", lpCsaddrBuffer   ; void* out
    , "Ptr", lpdwBufferLength   ; DWORD* in/out
    , "Ptr", lpAliasBuffer   ; LPWSTR optional, in/out
    , "Ptr", lpdwAliasBufferLength   ; DWORD* in/out
    , "Int")   ; return: INT
●GetAddressByNameW(dwNameSpace, lpServiceType, lpServiceName, lpiProtocols, dwResolution, lpServiceAsyncInfo, lpCsaddrBuffer, lpdwBufferLength, lpAliasBuffer, lpdwAliasBufferLength) = DLL("MSWSOCK.dll", "int GetAddressByNameW(dword, void*, char*, void*, dword, void*, void*, void*, char*, void*)")
# 呼び出し: GetAddressByNameW(dwNameSpace, lpServiceType, lpServiceName, lpiProtocols, dwResolution, lpServiceAsyncInfo, lpCsaddrBuffer, lpdwBufferLength, lpAliasBuffer, lpdwAliasBufferLength)
# dwNameSpace : DWORD -> "dword"
# lpServiceType : GUID* -> "void*"
# lpServiceName : LPWSTR optional -> "char*"
# lpiProtocols : INT* optional -> "void*"
# dwResolution : DWORD -> "dword"
# lpServiceAsyncInfo : SERVICE_ASYNC_INFO* optional -> "void*"
# lpCsaddrBuffer : void* out -> "void*"
# lpdwBufferLength : DWORD* in/out -> "void*"
# lpAliasBuffer : LPWSTR optional, in/out -> "char*"
# lpdwAliasBufferLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。