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

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

Svcguid.h ヘッダーファイルは、Winsock2.h ヘッダーファイルから自動的にはインクルードされません。

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

戻り値の型: INT

公式ドキュメント

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

戻り値

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

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

解説(Remarks)

メモ

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

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

各言語での呼び出し定義

// 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 版の利用を推奨。
const std = @import("std");

extern "mswsock" fn GetNameByTypeW(
    lpServiceType: [*c]GUID, // GUID*
    lpServiceName: [*c]u16, // LPWSTR out
    dwNameLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetNameByTypeW(
    lpServiceType: ptr GUID,  # GUID*
    lpServiceName: ptr uint16,  # LPWSTR out
    dwNameLength: uint32  # DWORD
): int32 {.importc: "GetNameByTypeW", stdcall, dynlib: "MSWSOCK.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "mswsock");
extern(Windows)
int GetNameByTypeW(
    GUID* lpServiceType,   // GUID*
    wchar* lpServiceName,   // LPWSTR out
    uint dwNameLength   // DWORD
);
ccall((:GetNameByTypeW, "MSWSOCK.dll"), stdcall, Int32,
      (Ptr{GUID}, Ptr{UInt16}, UInt32),
      lpServiceType, lpServiceName, dwNameLength)
# lpServiceType : GUID* -> Ptr{GUID}
# lpServiceName : LPWSTR out -> Ptr{UInt16}
# dwNameLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetNameByTypeW(
    void* lpServiceType,
    uint16_t* lpServiceName,
    uint32_t dwNameLength);
]]
local mswsock = ffi.load("mswsock")
-- mswsock.GetNameByTypeW(lpServiceType, lpServiceName, dwNameLength)
-- lpServiceType : GUID*
-- lpServiceName : LPWSTR out
-- dwNameLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('MSWSOCK.dll');
const GetNameByTypeW = lib.func('__stdcall', 'GetNameByTypeW', 'int32_t', ['void *', 'uint16_t *', 'uint32_t']);
// GetNameByTypeW(lpServiceType, lpServiceName, dwNameLength)
// lpServiceType : GUID* -> 'void *'
// lpServiceName : LPWSTR out -> 'uint16_t *'
// dwNameLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MSWSOCK.dll", {
  GetNameByTypeW: { parameters: ["pointer", "buffer", "u32"], result: "i32" },
});
// lib.symbols.GetNameByTypeW(lpServiceType, lpServiceName, dwNameLength)
// lpServiceType : GUID* -> "pointer"
// lpServiceName : LPWSTR out -> "buffer"
// dwNameLength : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetNameByTypeW(
    void* lpServiceType,
    uint16_t* lpServiceName,
    uint32_t dwNameLength);
C, "MSWSOCK.dll");
// $ffi->GetNameByTypeW(lpServiceType, lpServiceName, dwNameLength);
// lpServiceType : GUID*
// lpServiceName : LPWSTR 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.UNICODE_OPTIONS);
    int GetNameByTypeW(
        Pointer lpServiceType,   // GUID*
        char[] lpServiceName,   // LPWSTR out
        int dwNameLength   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("mswsock")]
lib LibMSWSOCK
  fun GetNameByTypeW = GetNameByTypeW(
    lpServiceType : GUID*,   # GUID*
    lpServiceName : UInt16*,   # LPWSTR 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 GetNameByTypeWNative = Int32 Function(Pointer<Void>, Pointer<Utf16>, Uint32);
typedef GetNameByTypeWDart = int Function(Pointer<Void>, Pointer<Utf16>, int);
final GetNameByTypeW = DynamicLibrary.open('MSWSOCK.dll')
    .lookupFunction<GetNameByTypeWNative, GetNameByTypeWDart>('GetNameByTypeW');
// lpServiceType : GUID* -> Pointer<Void>
// lpServiceName : LPWSTR out -> Pointer<Utf16>
// dwNameLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetNameByTypeW(
  lpServiceType: PGUID;   // GUID*
  lpServiceName: PWideChar;   // LPWSTR out
  dwNameLength: DWORD   // DWORD
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'GetNameByTypeW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let getnamebytypew =
  foreign "GetNameByTypeW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* lpServiceType : GUID* -> (ptr void) *)
(* lpServiceName : LPWSTR out -> (ptr uint16_t) *)
(* 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 ("GetNameByTypeW" get-name-by-type-w :convention :stdcall) :int32
  (lp-service-type :pointer)   ; GUID*
  (lp-service-name :pointer)   ; LPWSTR out
  (dw-name-length :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetNameByTypeW = Win32::API::More->new('MSWSOCK',
    'int GetNameByTypeW(LPVOID lpServiceType, LPWSTR lpServiceName, DWORD dwNameLength)');
# my $ret = $GetNameByTypeW->Call($lpServiceType, $lpServiceName, $dwNameLength);
# lpServiceType : GUID* -> LPVOID
# lpServiceName : LPWSTR out -> LPWSTR
# dwNameLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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