Win32 API 日本語リファレンス
ホームSystem.WindowsProgramming › DnsHostnameToComputerNameW

DnsHostnameToComputerNameW

関数
DNSホスト名をNetBIOSコンピューター名に変換する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL DnsHostnameToComputerNameW(
    LPCWSTR Hostname,
    LPWSTR ComputerName,   // optional
    DWORD* nSize
);

パラメーター

名前方向
HostnameLPCWSTRin
ComputerNameLPWSTRoutoptional
nSizeDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL DnsHostnameToComputerNameW(
    LPCWSTR Hostname,
    LPWSTR ComputerName,   // optional
    DWORD* nSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool DnsHostnameToComputerNameW(
    [MarshalAs(UnmanagedType.LPWStr)] string Hostname,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder ComputerName,   // LPWSTR optional, out
    ref uint nSize   // DWORD* in/out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DnsHostnameToComputerNameW(
    <MarshalAs(UnmanagedType.LPWStr)> Hostname As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> ComputerName As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef nSize As UInteger   ' DWORD* in/out
) As Boolean
End Function
' Hostname : LPCWSTR
' ComputerName : LPWSTR optional, out
' nSize : DWORD* in/out
Declare PtrSafe Function DnsHostnameToComputerNameW Lib "kernel32" ( _
    ByVal Hostname As LongPtr, _
    ByVal ComputerName As LongPtr, _
    ByRef nSize 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

DnsHostnameToComputerNameW = ctypes.windll.kernel32.DnsHostnameToComputerNameW
DnsHostnameToComputerNameW.restype = wintypes.BOOL
DnsHostnameToComputerNameW.argtypes = [
    wintypes.LPCWSTR,  # Hostname : LPCWSTR
    wintypes.LPWSTR,  # ComputerName : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # nSize : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
DnsHostnameToComputerNameW = Fiddle::Function.new(
  lib['DnsHostnameToComputerNameW'],
  [
    Fiddle::TYPE_VOIDP,  # Hostname : LPCWSTR
    Fiddle::TYPE_VOIDP,  # ComputerName : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # nSize : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn DnsHostnameToComputerNameW(
        Hostname: *const u16,  // LPCWSTR
        ComputerName: *mut u16,  // LPWSTR optional, out
        nSize: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool DnsHostnameToComputerNameW([MarshalAs(UnmanagedType.LPWStr)] string Hostname, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder ComputerName, ref uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_DnsHostnameToComputerNameW' -Namespace Win32 -PassThru
# $api::DnsHostnameToComputerNameW(Hostname, ComputerName, nSize)
#uselib "KERNEL32.dll"
#func global DnsHostnameToComputerNameW "DnsHostnameToComputerNameW" wptr, wptr, wptr
; DnsHostnameToComputerNameW Hostname, varptr(ComputerName), varptr(nSize)   ; 戻り値は stat
; Hostname : LPCWSTR -> "wptr"
; ComputerName : LPWSTR optional, out -> "wptr"
; nSize : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameW "DnsHostnameToComputerNameW" wstr, var, var
; res = DnsHostnameToComputerNameW(Hostname, ComputerName, nSize)
; Hostname : LPCWSTR -> "wstr"
; ComputerName : LPWSTR optional, out -> "var"
; nSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL DnsHostnameToComputerNameW(LPCWSTR Hostname, LPWSTR ComputerName, DWORD* nSize)
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameW "DnsHostnameToComputerNameW" wstr, var, var
; res = DnsHostnameToComputerNameW(Hostname, ComputerName, nSize)
; Hostname : LPCWSTR -> "wstr"
; ComputerName : LPWSTR optional, out -> "var"
; nSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procDnsHostnameToComputerNameW = kernel32.NewProc("DnsHostnameToComputerNameW")
)

// Hostname (LPCWSTR), ComputerName (LPWSTR optional, out), nSize (DWORD* in/out)
r1, _, err := procDnsHostnameToComputerNameW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Hostname))),
	uintptr(ComputerName),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function DnsHostnameToComputerNameW(
  Hostname: PWideChar;   // LPCWSTR
  ComputerName: PWideChar;   // LPWSTR optional, out
  nSize: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'DnsHostnameToComputerNameW';
result := DllCall("KERNEL32\DnsHostnameToComputerNameW"
    , "WStr", Hostname   ; LPCWSTR
    , "Ptr", ComputerName   ; LPWSTR optional, out
    , "Ptr", nSize   ; DWORD* in/out
    , "Int")   ; return: BOOL
●DnsHostnameToComputerNameW(Hostname, ComputerName, nSize) = DLL("KERNEL32.dll", "bool DnsHostnameToComputerNameW(char*, char*, void*)")
# 呼び出し: DnsHostnameToComputerNameW(Hostname, ComputerName, nSize)
# Hostname : LPCWSTR -> "char*"
# ComputerName : LPWSTR optional, out -> "char*"
# nSize : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。