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

DnsHostnameToComputerNameExW

関数
DNSホスト名をNetBIOS形式のコンピューター名へ変換する。
DLLKERNEL32.dll呼出規約winapi

シグネチャ

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

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

パラメーター

名前方向
HostnameLPCWSTRin
ComputerNameLPWSTRoutoptional
nSizeDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL DnsHostnameToComputerNameExW(
    LPCWSTR Hostname,
    LPWSTR ComputerName,   // optional
    DWORD* nSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern bool DnsHostnameToComputerNameExW(
    [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", ExactSpelling:=True)>
Public Shared Function DnsHostnameToComputerNameExW(
    <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 DnsHostnameToComputerNameExW Lib "kernel32" ( _
    ByVal Hostname As LongPtr, _
    ByVal ComputerName As LongPtr, _
    ByRef nSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

lib = Fiddle.dlopen('KERNEL32.dll')
DnsHostnameToComputerNameExW = Fiddle::Function.new(
  lib['DnsHostnameToComputerNameExW'],
  [
    Fiddle::TYPE_VOIDP,  # Hostname : LPCWSTR
    Fiddle::TYPE_VOIDP,  # ComputerName : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # nSize : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn DnsHostnameToComputerNameExW(
        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")]
public static extern bool DnsHostnameToComputerNameExW([MarshalAs(UnmanagedType.LPWStr)] string Hostname, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder ComputerName, ref uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_DnsHostnameToComputerNameExW' -Namespace Win32 -PassThru
# $api::DnsHostnameToComputerNameExW(Hostname, ComputerName, nSize)
#uselib "KERNEL32.dll"
#func global DnsHostnameToComputerNameExW "DnsHostnameToComputerNameExW" sptr, sptr, sptr
; DnsHostnameToComputerNameExW Hostname, varptr(ComputerName), varptr(nSize)   ; 戻り値は stat
; Hostname : LPCWSTR -> "sptr"
; ComputerName : LPWSTR optional, out -> "sptr"
; nSize : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameExW "DnsHostnameToComputerNameExW" wstr, var, var
; res = DnsHostnameToComputerNameExW(Hostname, ComputerName, nSize)
; Hostname : LPCWSTR -> "wstr"
; ComputerName : LPWSTR optional, out -> "var"
; nSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL DnsHostnameToComputerNameExW(LPCWSTR Hostname, LPWSTR ComputerName, DWORD* nSize)
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameExW "DnsHostnameToComputerNameExW" wstr, var, var
; res = DnsHostnameToComputerNameExW(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")
	procDnsHostnameToComputerNameExW = kernel32.NewProc("DnsHostnameToComputerNameExW")
)

// Hostname (LPCWSTR), ComputerName (LPWSTR optional, out), nSize (DWORD* in/out)
r1, _, err := procDnsHostnameToComputerNameExW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Hostname))),
	uintptr(ComputerName),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function DnsHostnameToComputerNameExW(
  Hostname: PWideChar;   // LPCWSTR
  ComputerName: PWideChar;   // LPWSTR optional, out
  nSize: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'DnsHostnameToComputerNameExW';
result := DllCall("KERNEL32\DnsHostnameToComputerNameExW"
    , "WStr", Hostname   ; LPCWSTR
    , "Ptr", ComputerName   ; LPWSTR optional, out
    , "Ptr", nSize   ; DWORD* in/out
    , "Int")   ; return: BOOL
●DnsHostnameToComputerNameExW(Hostname, ComputerName, nSize) = DLL("KERNEL32.dll", "bool DnsHostnameToComputerNameExW(char*, char*, void*)")
# 呼び出し: DnsHostnameToComputerNameExW(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)。