ホーム › Networking.WinSock › gethostname
gethostname
関数ローカルコンピューターの標準ホスト名を取得する。
シグネチャ
// WS2_32.dll
#include <windows.h>
INT gethostname(
LPSTR name,
INT namelen
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| name | LPSTR | out |
| namelen | INT | in |
戻り値の型: INT
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
INT gethostname(
LPSTR name,
INT namelen
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int gethostname(
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder name, // LPSTR out
int namelen // INT
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function gethostname(
<MarshalAs(UnmanagedType.LPStr)> name As System.Text.StringBuilder, ' LPSTR out
namelen As Integer ' INT
) As Integer
End Function' name : LPSTR out
' namelen : INT
Declare PtrSafe Function gethostname Lib "ws2_32" ( _
ByVal name As String, _
ByVal namelen As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
gethostname = ctypes.windll.ws2_32.gethostname
gethostname.restype = ctypes.c_int
gethostname.argtypes = [
wintypes.LPSTR, # name : LPSTR 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')
gethostname = Fiddle::Function.new(
lib['gethostname'],
[
Fiddle::TYPE_VOIDP, # name : LPSTR out
Fiddle::TYPE_INT, # namelen : INT
],
Fiddle::TYPE_INT)#[link(name = "ws2_32")]
extern "system" {
fn gethostname(
name: *mut u8, // LPSTR 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 gethostname([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder name, int namelen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_gethostname' -Namespace Win32 -PassThru
# $api::gethostname(name, namelen)#uselib "WS2_32.dll"
#func global gethostname "gethostname" sptr, sptr
; gethostname varptr(name), namelen ; 戻り値は stat
; name : LPSTR out -> "sptr"
; namelen : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "WS2_32.dll" #cfunc global gethostname "gethostname" var, int ; res = gethostname(name, namelen) ; name : LPSTR out -> "var" ; namelen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global gethostname "gethostname" sptr, int ; res = gethostname(varptr(name), namelen) ; name : LPSTR out -> "sptr" ; namelen : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT gethostname(LPSTR name, INT namelen) #uselib "WS2_32.dll" #cfunc global gethostname "gethostname" var, int ; res = gethostname(name, namelen) ; name : LPSTR out -> "var" ; namelen : INT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT gethostname(LPSTR name, INT namelen) #uselib "WS2_32.dll" #cfunc global gethostname "gethostname" intptr, int ; res = gethostname(varptr(name), namelen) ; name : LPSTR out -> "intptr" ; namelen : INT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procgethostname = ws2_32.NewProc("gethostname")
)
// name (LPSTR out), namelen (INT)
r1, _, err := procgethostname.Call(
uintptr(name),
uintptr(namelen),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction gethostname(
name: PAnsiChar; // LPSTR out
namelen: Integer // INT
): Integer; stdcall;
external 'WS2_32.dll' name 'gethostname';result := DllCall("WS2_32\gethostname"
, "Ptr", name ; LPSTR out
, "Int", namelen ; INT
, "Int") ; return: INT●gethostname(name, namelen) = DLL("WS2_32.dll", "int gethostname(char*, int)")
# 呼び出し: gethostname(name, namelen)
# name : LPSTR out -> "char*"
# namelen : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。