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

GetNameByTypeA

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

シグネチャ

// MSWSOCK.dll  (ANSI / -A)
#include <windows.h>

INT GetNameByTypeA(
    GUID* lpServiceType,
    LPSTR lpServiceName,
    DWORD dwNameLength
);

パラメーター

名前方向
lpServiceTypeGUID*in
lpServiceNameLPSTRout
dwNameLengthDWORDin

戻り値の型: INT

各言語での呼び出し定義

// MSWSOCK.dll  (ANSI / -A)
#include <windows.h>

INT GetNameByTypeA(
    GUID* lpServiceType,
    LPSTR lpServiceName,
    DWORD dwNameLength
);
[DllImport("MSWSOCK.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern int GetNameByTypeA(
    ref Guid lpServiceType,   // GUID*
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpServiceName,   // LPSTR out
    uint dwNameLength   // DWORD
);
<DllImport("MSWSOCK.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetNameByTypeA(
    ByRef lpServiceType As Guid,   ' GUID*
    <MarshalAs(UnmanagedType.LPStr)> lpServiceName As System.Text.StringBuilder,   ' LPSTR out
    dwNameLength As UInteger   ' DWORD
) As Integer
End Function
' lpServiceType : GUID*
' lpServiceName : LPSTR out
' dwNameLength : DWORD
Declare PtrSafe Function GetNameByTypeA Lib "mswsock" ( _
    ByVal lpServiceType As LongPtr, _
    ByVal lpServiceName As String, _
    ByVal dwNameLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetNameByTypeA = ctypes.windll.mswsock.GetNameByTypeA
GetNameByTypeA.restype = ctypes.c_int
GetNameByTypeA.argtypes = [
    ctypes.c_void_p,  # lpServiceType : GUID*
    wintypes.LPSTR,  # lpServiceName : LPSTR 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')
GetNameByTypeA = Fiddle::Function.new(
  lib['GetNameByTypeA'],
  [
    Fiddle::TYPE_VOIDP,  # lpServiceType : GUID*
    Fiddle::TYPE_VOIDP,  # lpServiceName : LPSTR out
    -Fiddle::TYPE_INT,  # dwNameLength : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "mswsock")]
extern "system" {
    fn GetNameByTypeA(
        lpServiceType: *mut GUID,  // GUID*
        lpServiceName: *mut u8,  // LPSTR out
        dwNameLength: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MSWSOCK.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int GetNameByTypeA(ref Guid lpServiceType, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpServiceName, uint dwNameLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_GetNameByTypeA' -Namespace Win32 -PassThru
# $api::GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
#uselib "MSWSOCK.dll"
#func global GetNameByTypeA "GetNameByTypeA" sptr, sptr, sptr
; GetNameByTypeA varptr(lpServiceType), varptr(lpServiceName), dwNameLength   ; 戻り値は stat
; lpServiceType : GUID* -> "sptr"
; lpServiceName : LPSTR out -> "sptr"
; dwNameLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MSWSOCK.dll"
#cfunc global GetNameByTypeA "GetNameByTypeA" var, var, int
; res = GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
; lpServiceType : GUID* -> "var"
; lpServiceName : LPSTR out -> "var"
; dwNameLength : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetNameByTypeA(GUID* lpServiceType, LPSTR lpServiceName, DWORD dwNameLength)
#uselib "MSWSOCK.dll"
#cfunc global GetNameByTypeA "GetNameByTypeA" var, var, int
; res = GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
; lpServiceType : GUID* -> "var"
; lpServiceName : LPSTR out -> "var"
; dwNameLength : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procGetNameByTypeA = mswsock.NewProc("GetNameByTypeA")
)

// lpServiceType (GUID*), lpServiceName (LPSTR out), dwNameLength (DWORD)
r1, _, err := procGetNameByTypeA.Call(
	uintptr(lpServiceType),
	uintptr(lpServiceName),
	uintptr(dwNameLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function GetNameByTypeA(
  lpServiceType: PGUID;   // GUID*
  lpServiceName: PAnsiChar;   // LPSTR out
  dwNameLength: DWORD   // DWORD
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'GetNameByTypeA';
result := DllCall("MSWSOCK\GetNameByTypeA"
    , "Ptr", lpServiceType   ; GUID*
    , "Ptr", lpServiceName   ; LPSTR out
    , "UInt", dwNameLength   ; DWORD
    , "Int")   ; return: INT
●GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength) = DLL("MSWSOCK.dll", "int GetNameByTypeA(void*, char*, dword)")
# 呼び出し: GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
# lpServiceType : GUID* -> "void*"
# lpServiceName : LPSTR out -> "char*"
# dwNameLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。