ホーム › Networking.WinSock › GetTypeByNameW
GetTypeByNameW
関数サービス名から対応するサービス種別GUIDを取得する。
シグネチャ
// MSWSOCK.dll (Unicode / -W)
#include <windows.h>
INT GetTypeByNameW(
LPWSTR lpServiceName,
GUID* lpServiceType
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpServiceName | LPWSTR | in | サービス名を一意に表す、ゼロ終端文字列へのポインターです。たとえば "MY SNA SERVER." などです。 |
| lpServiceType | GUID* | inout | ネットワークサービスのタイプを指定するグローバル一意識別子 (GUID) を受け取る変数へのポインターです。Svcguid.h ヘッダーファイルには、いくつかの GUID サービスタイプの定義と、それらを扱うためのマクロが含まれています。 Svcguid.h ヘッダーファイルは、Winsock2.h ヘッダーファイルによって自動的にはインクルードされません。 |
戻り値の型: INT
公式ドキュメント
GetTypeByName 関数は、名前で指定されたネットワークサービスのサービスタイプ GUID を取得します。(Unicode)
戻り値
関数が成功した場合、戻り値はゼロです。
関数が失敗した場合、戻り値は SOCKET_ERROR( – 1) です。拡張エラー情報を取得するには、 GetLastError を呼び出します。これは次の拡張エラー値を返します。
| 値 | 意味 |
|---|---|
| 指定されたサービスタイプが不明です。 |
解説(Remarks)
メモ
nspapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetTypeByName を定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不一致が生じ、コンパイルエラーやランタイムエラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// MSWSOCK.dll (Unicode / -W)
#include <windows.h>
INT GetTypeByNameW(
LPWSTR lpServiceName,
GUID* lpServiceType
);[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int GetTypeByNameW(
[MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, // LPWSTR
ref Guid lpServiceType // GUID* in/out
);<DllImport("MSWSOCK.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetTypeByNameW(
<MarshalAs(UnmanagedType.LPWStr)> lpServiceName As String, ' LPWSTR
ByRef lpServiceType As Guid ' GUID* in/out
) As Integer
End Function' lpServiceName : LPWSTR
' lpServiceType : GUID* in/out
Declare PtrSafe Function GetTypeByNameW Lib "mswsock" ( _
ByVal lpServiceName As LongPtr, _
ByVal lpServiceType As LongPtr) 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
GetTypeByNameW = ctypes.windll.mswsock.GetTypeByNameW
GetTypeByNameW.restype = ctypes.c_int
GetTypeByNameW.argtypes = [
wintypes.LPCWSTR, # lpServiceName : LPWSTR
ctypes.c_void_p, # lpServiceType : GUID* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('MSWSOCK.dll')
GetTypeByNameW = Fiddle::Function.new(
lib['GetTypeByNameW'],
[
Fiddle::TYPE_VOIDP, # lpServiceName : LPWSTR
Fiddle::TYPE_VOIDP, # lpServiceType : GUID* in/out
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "mswsock")]
extern "system" {
fn GetTypeByNameW(
lpServiceName: *mut u16, // LPWSTR
lpServiceType: *mut GUID // GUID* 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 GetTypeByNameW([MarshalAs(UnmanagedType.LPWStr)] string lpServiceName, ref Guid lpServiceType);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_GetTypeByNameW' -Namespace Win32 -PassThru
# $api::GetTypeByNameW(lpServiceName, lpServiceType)#uselib "MSWSOCK.dll"
#func global GetTypeByNameW "GetTypeByNameW" wptr, wptr
; GetTypeByNameW lpServiceName, varptr(lpServiceType) ; 戻り値は stat
; lpServiceName : LPWSTR -> "wptr"
; lpServiceType : GUID* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "MSWSOCK.dll" #cfunc global GetTypeByNameW "GetTypeByNameW" wstr, var ; res = GetTypeByNameW(lpServiceName, lpServiceType) ; lpServiceName : LPWSTR -> "wstr" ; lpServiceType : GUID* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "MSWSOCK.dll" #cfunc global GetTypeByNameW "GetTypeByNameW" wstr, sptr ; res = GetTypeByNameW(lpServiceName, varptr(lpServiceType)) ; lpServiceName : LPWSTR -> "wstr" ; lpServiceType : GUID* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT GetTypeByNameW(LPWSTR lpServiceName, GUID* lpServiceType) #uselib "MSWSOCK.dll" #cfunc global GetTypeByNameW "GetTypeByNameW" wstr, var ; res = GetTypeByNameW(lpServiceName, lpServiceType) ; lpServiceName : LPWSTR -> "wstr" ; lpServiceType : GUID* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT GetTypeByNameW(LPWSTR lpServiceName, GUID* lpServiceType) #uselib "MSWSOCK.dll" #cfunc global GetTypeByNameW "GetTypeByNameW" wstr, intptr ; res = GetTypeByNameW(lpServiceName, varptr(lpServiceType)) ; lpServiceName : LPWSTR -> "wstr" ; lpServiceType : GUID* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
procGetTypeByNameW = mswsock.NewProc("GetTypeByNameW")
)
// lpServiceName (LPWSTR), lpServiceType (GUID* in/out)
r1, _, err := procGetTypeByNameW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpServiceName))),
uintptr(lpServiceType),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction GetTypeByNameW(
lpServiceName: PWideChar; // LPWSTR
lpServiceType: PGUID // GUID* in/out
): Integer; stdcall;
external 'MSWSOCK.dll' name 'GetTypeByNameW';result := DllCall("MSWSOCK\GetTypeByNameW"
, "WStr", lpServiceName ; LPWSTR
, "Ptr", lpServiceType ; GUID* in/out
, "Int") ; return: INT●GetTypeByNameW(lpServiceName, lpServiceType) = DLL("MSWSOCK.dll", "int GetTypeByNameW(char*, void*)")
# 呼び出し: GetTypeByNameW(lpServiceName, lpServiceType)
# lpServiceName : LPWSTR -> "char*"
# lpServiceType : GUID* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "mswsock" fn GetTypeByNameW(
lpServiceName: [*c]const u16, // LPWSTR
lpServiceType: [*c]GUID // GUID* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc GetTypeByNameW(
lpServiceName: WideCString, # LPWSTR
lpServiceType: ptr GUID # GUID* in/out
): int32 {.importc: "GetTypeByNameW", stdcall, dynlib: "MSWSOCK.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "mswsock");
extern(Windows)
int GetTypeByNameW(
const(wchar)* lpServiceName, // LPWSTR
GUID* lpServiceType // GUID* in/out
);ccall((:GetTypeByNameW, "MSWSOCK.dll"), stdcall, Int32,
(Cwstring, Ptr{GUID}),
lpServiceName, lpServiceType)
# lpServiceName : LPWSTR -> Cwstring
# lpServiceType : GUID* in/out -> Ptr{GUID}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t GetTypeByNameW(
const uint16_t* lpServiceName,
void* lpServiceType);
]]
local mswsock = ffi.load("mswsock")
-- mswsock.GetTypeByNameW(lpServiceName, lpServiceType)
-- lpServiceName : LPWSTR
-- lpServiceType : GUID* in/out
-- 構造体/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 GetTypeByNameW = lib.func('__stdcall', 'GetTypeByNameW', 'int32_t', ['str16', 'void *']);
// GetTypeByNameW(lpServiceName, lpServiceType)
// lpServiceName : LPWSTR -> 'str16'
// lpServiceType : GUID* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("MSWSOCK.dll", {
GetTypeByNameW: { parameters: ["buffer", "pointer"], result: "i32" },
});
// lib.symbols.GetTypeByNameW(lpServiceName, lpServiceType)
// lpServiceName : LPWSTR -> "buffer"
// lpServiceType : GUID* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t GetTypeByNameW(
const uint16_t* lpServiceName,
void* lpServiceType);
C, "MSWSOCK.dll");
// $ffi->GetTypeByNameW(lpServiceName, lpServiceType);
// lpServiceName : LPWSTR
// lpServiceType : GUID* in/out
// 構造体/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 GetTypeByNameW(
WString lpServiceName, // LPWSTR
Pointer lpServiceType // GUID* in/out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("mswsock")]
lib LibMSWSOCK
fun GetTypeByNameW = GetTypeByNameW(
lpServiceName : UInt16*, # LPWSTR
lpServiceType : GUID* # GUID* in/out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetTypeByNameWNative = Int32 Function(Pointer<Utf16>, Pointer<Void>);
typedef GetTypeByNameWDart = int Function(Pointer<Utf16>, Pointer<Void>);
final GetTypeByNameW = DynamicLibrary.open('MSWSOCK.dll')
.lookupFunction<GetTypeByNameWNative, GetTypeByNameWDart>('GetTypeByNameW');
// lpServiceName : LPWSTR -> Pointer<Utf16>
// lpServiceType : GUID* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetTypeByNameW(
lpServiceName: PWideChar; // LPWSTR
lpServiceType: PGUID // GUID* in/out
): Integer; stdcall;
external 'MSWSOCK.dll' name 'GetTypeByNameW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetTypeByNameW"
c_GetTypeByNameW :: CWString -> Ptr () -> IO Int32
-- lpServiceName : LPWSTR -> CWString
-- lpServiceType : GUID* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let gettypebynamew =
foreign "GetTypeByNameW"
((ptr uint16_t) @-> (ptr void) @-> returning int32_t)
(* lpServiceName : LPWSTR -> (ptr uint16_t) *)
(* lpServiceType : GUID* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library mswsock (t "MSWSOCK.dll"))
(cffi:use-foreign-library mswsock)
(cffi:defcfun ("GetTypeByNameW" get-type-by-name-w :convention :stdcall) :int32
(lp-service-name (:string :encoding :utf-16le)) ; LPWSTR
(lp-service-type :pointer)) ; GUID* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetTypeByNameW = Win32::API::More->new('MSWSOCK',
'int GetTypeByNameW(LPCWSTR lpServiceName, LPVOID lpServiceType)');
# my $ret = $GetTypeByNameW->Call($lpServiceName, $lpServiceType);
# lpServiceName : LPWSTR -> LPCWSTR
# lpServiceType : GUID* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f GetTypeByNameA (ANSI版) — サービス名から対応するサービス種別GUIDを取得する。
公式の関連項目
- f GetNameByTypeW — サービス種別GUIDから対応するサービス名を取得する。