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

GetNameByTypeW

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

シグネチャ

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

INT GetNameByTypeW(
    GUID* lpServiceType,
    LPWSTR lpServiceName,
    DWORD dwNameLength
);

パラメーター

名前方向
lpServiceTypeGUID*in
lpServiceNameLPWSTRout
dwNameLengthDWORDin

戻り値の型: INT

各言語での呼び出し定義

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

INT GetNameByTypeW(
    GUID* lpServiceType,
    LPWSTR lpServiceName,
    DWORD dwNameLength
);
[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int GetNameByTypeW(
    ref Guid lpServiceType,   // GUID*
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpServiceName,   // LPWSTR out
    uint dwNameLength   // DWORD
);
<DllImport("MSWSOCK.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetNameByTypeW(
    ByRef lpServiceType As Guid,   ' GUID*
    <MarshalAs(UnmanagedType.LPWStr)> lpServiceName As System.Text.StringBuilder,   ' LPWSTR out
    dwNameLength As UInteger   ' DWORD
) As Integer
End Function
' lpServiceType : GUID*
' lpServiceName : LPWSTR out
' dwNameLength : DWORD
Declare PtrSafe Function GetNameByTypeW Lib "mswsock" ( _
    ByVal lpServiceType As LongPtr, _
    ByVal lpServiceName As LongPtr, _
    ByVal dwNameLength 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

GetNameByTypeW = ctypes.windll.mswsock.GetNameByTypeW
GetNameByTypeW.restype = ctypes.c_int
GetNameByTypeW.argtypes = [
    ctypes.c_void_p,  # lpServiceType : GUID*
    wintypes.LPWSTR,  # lpServiceName : LPWSTR out
    wintypes.DWORD,  # dwNameLength : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procGetNameByTypeW = mswsock.NewProc("GetNameByTypeW")
)

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