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

DnsHostnameToComputerNameA

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

シグネチャ

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

BOOL DnsHostnameToComputerNameA(
    LPCSTR Hostname,
    LPSTR ComputerName,   // optional
    DWORD* nSize
);

パラメーター

名前方向
HostnameLPCSTRin
ComputerNameLPSTRoutoptional
nSizeDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL DnsHostnameToComputerNameA(
    LPCSTR Hostname,
    LPSTR ComputerName,   // optional
    DWORD* nSize
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool DnsHostnameToComputerNameA(
    [MarshalAs(UnmanagedType.LPStr)] string Hostname,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder ComputerName,   // LPSTR optional, out
    ref uint nSize   // DWORD* in/out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DnsHostnameToComputerNameA(
    <MarshalAs(UnmanagedType.LPStr)> Hostname As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> ComputerName As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef nSize As UInteger   ' DWORD* in/out
) As Boolean
End Function
' Hostname : LPCSTR
' ComputerName : LPSTR optional, out
' nSize : DWORD* in/out
Declare PtrSafe Function DnsHostnameToComputerNameA Lib "kernel32" ( _
    ByVal Hostname As String, _
    ByVal ComputerName As String, _
    ByRef nSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

DnsHostnameToComputerNameA = ctypes.windll.kernel32.DnsHostnameToComputerNameA
DnsHostnameToComputerNameA.restype = wintypes.BOOL
DnsHostnameToComputerNameA.argtypes = [
    wintypes.LPCSTR,  # Hostname : LPCSTR
    wintypes.LPSTR,  # ComputerName : LPSTR 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')
DnsHostnameToComputerNameA = Fiddle::Function.new(
  lib['DnsHostnameToComputerNameA'],
  [
    Fiddle::TYPE_VOIDP,  # Hostname : LPCSTR
    Fiddle::TYPE_VOIDP,  # ComputerName : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # nSize : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn DnsHostnameToComputerNameA(
        Hostname: *const u8,  // LPCSTR
        ComputerName: *mut u8,  // LPSTR 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.Ansi, SetLastError = true)]
public static extern bool DnsHostnameToComputerNameA([MarshalAs(UnmanagedType.LPStr)] string Hostname, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder ComputerName, ref uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_DnsHostnameToComputerNameA' -Namespace Win32 -PassThru
# $api::DnsHostnameToComputerNameA(Hostname, ComputerName, nSize)
#uselib "KERNEL32.dll"
#func global DnsHostnameToComputerNameA "DnsHostnameToComputerNameA" sptr, sptr, sptr
; DnsHostnameToComputerNameA Hostname, varptr(ComputerName), varptr(nSize)   ; 戻り値は stat
; Hostname : LPCSTR -> "sptr"
; ComputerName : LPSTR optional, out -> "sptr"
; nSize : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameA "DnsHostnameToComputerNameA" str, var, var
; res = DnsHostnameToComputerNameA(Hostname, ComputerName, nSize)
; Hostname : LPCSTR -> "str"
; ComputerName : LPSTR optional, out -> "var"
; nSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL DnsHostnameToComputerNameA(LPCSTR Hostname, LPSTR ComputerName, DWORD* nSize)
#uselib "KERNEL32.dll"
#cfunc global DnsHostnameToComputerNameA "DnsHostnameToComputerNameA" str, var, var
; res = DnsHostnameToComputerNameA(Hostname, ComputerName, nSize)
; Hostname : LPCSTR -> "str"
; ComputerName : LPSTR optional, out -> "var"
; nSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procDnsHostnameToComputerNameA = kernel32.NewProc("DnsHostnameToComputerNameA")
)

// Hostname (LPCSTR), ComputerName (LPSTR optional, out), nSize (DWORD* in/out)
r1, _, err := procDnsHostnameToComputerNameA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(Hostname))),
	uintptr(ComputerName),
	uintptr(nSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function DnsHostnameToComputerNameA(
  Hostname: PAnsiChar;   // LPCSTR
  ComputerName: PAnsiChar;   // LPSTR optional, out
  nSize: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'DnsHostnameToComputerNameA';
result := DllCall("KERNEL32\DnsHostnameToComputerNameA"
    , "AStr", Hostname   ; LPCSTR
    , "Ptr", ComputerName   ; LPSTR optional, out
    , "Ptr", nSize   ; DWORD* in/out
    , "Int")   ; return: BOOL
●DnsHostnameToComputerNameA(Hostname, ComputerName, nSize) = DLL("KERNEL32.dll", "bool DnsHostnameToComputerNameA(char*, char*, void*)")
# 呼び出し: DnsHostnameToComputerNameA(Hostname, ComputerName, nSize)
# Hostname : LPCSTR -> "char*"
# ComputerName : LPSTR optional, out -> "char*"
# nSize : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。