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

GetHostNameW

関数
ローカルホスト名をUnicode文字列で取得する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

// WS2_32.dll
#include <windows.h>

INT GetHostNameW(
    LPWSTR name,
    INT namelen
);

パラメーター

名前方向
nameLPWSTRout
namelenINTin

戻り値の型: INT

各言語での呼び出し定義

// WS2_32.dll
#include <windows.h>

INT GetHostNameW(
    LPWSTR name,
    INT namelen
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int GetHostNameW(
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder name,   // LPWSTR out
    int namelen   // INT
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetHostNameW(
    <MarshalAs(UnmanagedType.LPWStr)> name As System.Text.StringBuilder,   ' LPWSTR out
    namelen As Integer   ' INT
) As Integer
End Function
' name : LPWSTR out
' namelen : INT
Declare PtrSafe Function GetHostNameW Lib "ws2_32" ( _
    ByVal name As LongPtr, _
    ByVal namelen As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetHostNameW = ctypes.windll.ws2_32.GetHostNameW
GetHostNameW.restype = ctypes.c_int
GetHostNameW.argtypes = [
    wintypes.LPWSTR,  # name : LPWSTR out
    ctypes.c_int,  # namelen : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
GetHostNameW = Fiddle::Function.new(
  lib['GetHostNameW'],
  [
    Fiddle::TYPE_VOIDP,  # name : LPWSTR out
    Fiddle::TYPE_INT,  # namelen : INT
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn GetHostNameW(
        name: *mut u16,  // LPWSTR out
        namelen: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int GetHostNameW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder name, int namelen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_GetHostNameW' -Namespace Win32 -PassThru
# $api::GetHostNameW(name, namelen)
#uselib "WS2_32.dll"
#func global GetHostNameW "GetHostNameW" sptr, sptr
; GetHostNameW varptr(name), namelen   ; 戻り値は stat
; name : LPWSTR out -> "sptr"
; namelen : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global GetHostNameW "GetHostNameW" var, int
; res = GetHostNameW(name, namelen)
; name : LPWSTR out -> "var"
; namelen : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT GetHostNameW(LPWSTR name, INT namelen)
#uselib "WS2_32.dll"
#cfunc global GetHostNameW "GetHostNameW" var, int
; res = GetHostNameW(name, namelen)
; name : LPWSTR out -> "var"
; namelen : INT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procGetHostNameW = ws2_32.NewProc("GetHostNameW")
)

// name (LPWSTR out), namelen (INT)
r1, _, err := procGetHostNameW.Call(
	uintptr(name),
	uintptr(namelen),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function GetHostNameW(
  name: PWideChar;   // LPWSTR out
  namelen: Integer   // INT
): Integer; stdcall;
  external 'WS2_32.dll' name 'GetHostNameW';
result := DllCall("WS2_32\GetHostNameW"
    , "Ptr", name   ; LPWSTR out
    , "Int", namelen   ; INT
    , "Int")   ; return: INT
●GetHostNameW(name, namelen) = DLL("WS2_32.dll", "int GetHostNameW(char*, int)")
# 呼び出し: GetHostNameW(name, namelen)
# name : LPWSTR out -> "char*"
# namelen : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。