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

GetTypeByNameA

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

シグネチャ

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

INT GetTypeByNameA(
    LPSTR lpServiceName,
    GUID* lpServiceType
);

パラメーター

名前方向説明
lpServiceNameLPSTRinサービスの名前を一意に表すゼロ終端文字列へのポインターです。例: "MY SNA SERVER."
lpServiceTypeGUID*inout

ネットワークサービスのタイプを指定するグローバル一意識別子(GUID)を受け取る変数へのポインターです。Svcguid.h ヘッダーファイルには、複数の GUID サービスタイプの定義と、それらを操作するためのマクロが含まれています。

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

戻り値の型: INT

公式ドキュメント

GetTypeByName 関数は、名前で指定されたネットワークサービスのサービスタイプ GUID を取得します。(ANSI)

戻り値

関数が成功した場合、戻り値はゼロです。

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

意味
ERROR_SERVICE_DOES_NOT_EXIST
指定されたサービスタイプが不明です。

解説(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)

各言語での呼び出し定義

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

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

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

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procGetTypeByNameA = mswsock.NewProc("GetTypeByNameA")
)

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

extern "mswsock" fn GetTypeByNameA(
    lpServiceName: [*c]const u8, // LPSTR
    lpServiceType: [*c]GUID // GUID* in/out
) callconv(std.os.windows.WINAPI) i32;
proc GetTypeByNameA(
    lpServiceName: cstring,  # LPSTR
    lpServiceType: ptr GUID  # GUID* in/out
): int32 {.importc: "GetTypeByNameA", stdcall, dynlib: "MSWSOCK.dll".}
pragma(lib, "mswsock");
extern(Windows)
int GetTypeByNameA(
    const(char)* lpServiceName,   // LPSTR
    GUID* lpServiceType   // GUID* in/out
);
ccall((:GetTypeByNameA, "MSWSOCK.dll"), stdcall, Int32,
      (Cstring, Ptr{GUID}),
      lpServiceName, lpServiceType)
# lpServiceName : LPSTR -> Cstring
# lpServiceType : GUID* in/out -> Ptr{GUID}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetTypeByNameA(
    const char* lpServiceName,
    void* lpServiceType);
]]
local mswsock = ffi.load("mswsock")
-- mswsock.GetTypeByNameA(lpServiceName, lpServiceType)
-- lpServiceName : LPSTR
-- lpServiceType : GUID* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('MSWSOCK.dll');
const GetTypeByNameA = lib.func('__stdcall', 'GetTypeByNameA', 'int32_t', ['str', 'void *']);
// GetTypeByNameA(lpServiceName, lpServiceType)
// lpServiceName : LPSTR -> 'str'
// lpServiceType : GUID* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("MSWSOCK.dll", {
  GetTypeByNameA: { parameters: ["buffer", "pointer"], result: "i32" },
});
// lib.symbols.GetTypeByNameA(lpServiceName, lpServiceType)
// lpServiceName : LPSTR -> "buffer"
// lpServiceType : GUID* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetTypeByNameA(
    const char* lpServiceName,
    void* lpServiceType);
C, "MSWSOCK.dll");
// $ffi->GetTypeByNameA(lpServiceName, lpServiceType);
// lpServiceName : LPSTR
// 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.ASCII_OPTIONS);
    int GetTypeByNameA(
        String lpServiceName,   // LPSTR
        Pointer lpServiceType   // GUID* in/out
    );
}
@[Link("mswsock")]
lib LibMSWSOCK
  fun GetTypeByNameA = GetTypeByNameA(
    lpServiceName : UInt8*,   # LPSTR
    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 GetTypeByNameANative = Int32 Function(Pointer<Utf8>, Pointer<Void>);
typedef GetTypeByNameADart = int Function(Pointer<Utf8>, Pointer<Void>);
final GetTypeByNameA = DynamicLibrary.open('MSWSOCK.dll')
    .lookupFunction<GetTypeByNameANative, GetTypeByNameADart>('GetTypeByNameA');
// lpServiceName : LPSTR -> Pointer<Utf8>
// lpServiceType : GUID* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetTypeByNameA(
  lpServiceName: PAnsiChar;   // LPSTR
  lpServiceType: PGUID   // GUID* in/out
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'GetTypeByNameA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let gettypebynamea =
  foreign "GetTypeByNameA"
    (string @-> (ptr void) @-> returning int32_t)
(* lpServiceName : LPSTR -> string *)
(* 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 ("GetTypeByNameA" get-type-by-name-a :convention :stdcall) :int32
  (lp-service-name :string)   ; LPSTR
  (lp-service-type :pointer))   ; GUID* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetTypeByNameA = Win32::API::More->new('MSWSOCK',
    'int GetTypeByNameA(LPCSTR lpServiceName, LPVOID lpServiceType)');
# my $ret = $GetTypeByNameA->Call($lpServiceName, $lpServiceType);
# lpServiceName : LPSTR -> LPCSTR
# lpServiceType : GUID* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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