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

ネットワークサービスの種類を指定するグローバル一意識別子 (GUID) へのポインター。Svcguid.h ヘッダーファイルには、いくつかの GUID サービスの種類の定義と、それらを扱うためのマクロが含まれています。

Svcguid.h ヘッダーファイルは、Winsock2.h ヘッダーファイルによって自動的にインクルードされるわけではありません。

lpServiceNameLPSTRoutネットワークサービスの名前を一意に表す、ゼロ終端文字列を受け取るバッファーへのポインター。
dwNameLengthDWORDin入力時には lpServiceName が指すバッファーのサイズ (バイト単位) を指定し、出力時にはサービス名文字列の実際のサイズ (バイト単位) を格納する変数へのポインター。

戻り値の型: INT

公式ドキュメント

GetNameByType 関数は、指定されたサービスの種類に対応するネットワークサービスの名前を取得します。(ANSI)

戻り値

関数が成功した場合、戻り値は SOCKET_ERROR (–1) 以外になります。

関数が失敗した場合、戻り値は SOCKET_ERROR (–1) になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

メモ

nspapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetNameByType を定義します。エンコード中立のエイリアスの使用を、エンコード中立でないコードと混在させると、不整合が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// 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)。
const std = @import("std");

extern "mswsock" fn GetNameByTypeA(
    lpServiceType: [*c]GUID, // GUID*
    lpServiceName: [*c]u8, // LPSTR out
    dwNameLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc GetNameByTypeA(
    lpServiceType: ptr GUID,  # GUID*
    lpServiceName: ptr char,  # LPSTR out
    dwNameLength: uint32  # DWORD
): int32 {.importc: "GetNameByTypeA", stdcall, dynlib: "MSWSOCK.dll".}
pragma(lib, "mswsock");
extern(Windows)
int GetNameByTypeA(
    GUID* lpServiceType,   // GUID*
    char* lpServiceName,   // LPSTR out
    uint dwNameLength   // DWORD
);
ccall((:GetNameByTypeA, "MSWSOCK.dll"), stdcall, Int32,
      (Ptr{GUID}, Ptr{UInt8}, UInt32),
      lpServiceType, lpServiceName, dwNameLength)
# lpServiceType : GUID* -> Ptr{GUID}
# lpServiceName : LPSTR out -> Ptr{UInt8}
# dwNameLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetNameByTypeA(
    void* lpServiceType,
    char* lpServiceName,
    uint32_t dwNameLength);
]]
local mswsock = ffi.load("mswsock")
-- mswsock.GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
-- lpServiceType : GUID*
-- lpServiceName : LPSTR out
-- dwNameLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('MSWSOCK.dll');
const GetNameByTypeA = lib.func('__stdcall', 'GetNameByTypeA', 'int32_t', ['void *', 'char *', 'uint32_t']);
// GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
// lpServiceType : GUID* -> 'void *'
// lpServiceName : LPSTR out -> 'char *'
// dwNameLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MSWSOCK.dll", {
  GetNameByTypeA: { parameters: ["pointer", "buffer", "u32"], result: "i32" },
});
// lib.symbols.GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength)
// lpServiceType : GUID* -> "pointer"
// lpServiceName : LPSTR out -> "buffer"
// dwNameLength : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetNameByTypeA(
    void* lpServiceType,
    char* lpServiceName,
    uint32_t dwNameLength);
C, "MSWSOCK.dll");
// $ffi->GetNameByTypeA(lpServiceType, lpServiceName, dwNameLength);
// lpServiceType : GUID*
// lpServiceName : LPSTR out
// dwNameLength : DWORD
// 構造体/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 Mswsock extends StdCallLibrary {
    Mswsock INSTANCE = Native.load("mswsock", Mswsock.class, W32APIOptions.ASCII_OPTIONS);
    int GetNameByTypeA(
        Pointer lpServiceType,   // GUID*
        byte[] lpServiceName,   // LPSTR out
        int dwNameLength   // DWORD
    );
}
@[Link("mswsock")]
lib LibMSWSOCK
  fun GetNameByTypeA = GetNameByTypeA(
    lpServiceType : GUID*,   # GUID*
    lpServiceName : UInt8*,   # LPSTR out
    dwNameLength : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetNameByTypeANative = Int32 Function(Pointer<Void>, Pointer<Utf8>, Uint32);
typedef GetNameByTypeADart = int Function(Pointer<Void>, Pointer<Utf8>, int);
final GetNameByTypeA = DynamicLibrary.open('MSWSOCK.dll')
    .lookupFunction<GetNameByTypeANative, GetNameByTypeADart>('GetNameByTypeA');
// lpServiceType : GUID* -> Pointer<Void>
// lpServiceName : LPSTR out -> Pointer<Utf8>
// dwNameLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetNameByTypeA(
  lpServiceType: PGUID;   // GUID*
  lpServiceName: PAnsiChar;   // LPSTR out
  dwNameLength: DWORD   // DWORD
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'GetNameByTypeA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetNameByTypeA"
  c_GetNameByTypeA :: Ptr () -> CString -> Word32 -> IO Int32
-- lpServiceType : GUID* -> Ptr ()
-- lpServiceName : LPSTR out -> CString
-- dwNameLength : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getnamebytypea =
  foreign "GetNameByTypeA"
    ((ptr void) @-> string @-> uint32_t @-> returning int32_t)
(* lpServiceType : GUID* -> (ptr void) *)
(* lpServiceName : LPSTR out -> string *)
(* dwNameLength : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library mswsock (t "MSWSOCK.dll"))
(cffi:use-foreign-library mswsock)

(cffi:defcfun ("GetNameByTypeA" get-name-by-type-a :convention :stdcall) :int32
  (lp-service-type :pointer)   ; GUID*
  (lp-service-name :pointer)   ; LPSTR out
  (dw-name-length :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetNameByTypeA = Win32::API::More->new('MSWSOCK',
    'int GetNameByTypeA(LPVOID lpServiceType, LPSTR lpServiceName, DWORD dwNameLength)');
# my $ret = $GetNameByTypeA->Call($lpServiceType, $lpServiceName, $dwNameLength);
# lpServiceType : GUID* -> LPVOID
# lpServiceName : LPSTR out -> LPSTR
# dwNameLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目