Win32 API 日本語リファレンス
ホームNetworkManagement.Dns › DnsQuery_W

DnsQuery_W

関数
Unicode文字列名でDNS問い合わせを実行しレコードを取得する。
DLLDNSAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR DnsQuery_W(
    LPCWSTR pszName,
    WORD wType,
    DNS_QUERY_OPTIONS Options,
    void* pExtra,   // optional
    DNS_RECORDA** ppQueryResults,
    void** pReserved   // optional
);

パラメーター

名前方向説明
pszNameLPCWSTRin問い合わせるDNS名を表すUnicode文字列へのポインタ。終端NULL文字列。
wTypeWORDin問い合わせるDNSレコード種別を示すWORD値。DNS_TYPE_A等を指定する。
OptionsDNS_QUERY_OPTIONSin問い合わせ動作を制御するDNS_QUERY_OPTIONSフラグ。
pExtravoid*inoutoptionalDNSサーバー一覧などの追加情報へのポインタ。未使用時はNULL可。
ppQueryResultsDNS_RECORDA**out取得したDNSレコードリストを受け取るポインタのアドレス。DnsFreeで解放する。
pReservedvoid**outoptional予約済みパラメータ。NULLを指定する。

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

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

WIN32_ERROR DnsQuery_W(
    LPCWSTR pszName,
    WORD wType,
    DNS_QUERY_OPTIONS Options,
    void* pExtra,   // optional
    DNS_RECORDA** ppQueryResults,
    void** pReserved   // optional
);
[DllImport("DNSAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint DnsQuery_W(
    [MarshalAs(UnmanagedType.LPWStr)] string pszName,   // LPCWSTR
    ushort wType,   // WORD
    uint Options,   // DNS_QUERY_OPTIONS
    IntPtr pExtra,   // void* optional, in/out
    IntPtr ppQueryResults,   // DNS_RECORDA** out
    IntPtr pReserved   // void** optional, out
);
<DllImport("DNSAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function DnsQuery_W(
    <MarshalAs(UnmanagedType.LPWStr)> pszName As String,   ' LPCWSTR
    wType As UShort,   ' WORD
    Options As UInteger,   ' DNS_QUERY_OPTIONS
    pExtra As IntPtr,   ' void* optional, in/out
    ppQueryResults As IntPtr,   ' DNS_RECORDA** out
    pReserved As IntPtr   ' void** optional, out
) As UInteger
End Function
' pszName : LPCWSTR
' wType : WORD
' Options : DNS_QUERY_OPTIONS
' pExtra : void* optional, in/out
' ppQueryResults : DNS_RECORDA** out
' pReserved : void** optional, out
Declare PtrSafe Function DnsQuery_W Lib "dnsapi" ( _
    ByVal pszName As LongPtr, _
    ByVal wType As Integer, _
    ByVal Options As Long, _
    ByVal pExtra As LongPtr, _
    ByVal ppQueryResults As LongPtr, _
    ByVal pReserved As LongPtr) 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

DnsQuery_W = ctypes.windll.dnsapi.DnsQuery_W
DnsQuery_W.restype = wintypes.DWORD
DnsQuery_W.argtypes = [
    wintypes.LPCWSTR,  # pszName : LPCWSTR
    ctypes.c_ushort,  # wType : WORD
    wintypes.DWORD,  # Options : DNS_QUERY_OPTIONS
    ctypes.POINTER(None),  # pExtra : void* optional, in/out
    ctypes.c_void_p,  # ppQueryResults : DNS_RECORDA** out
    ctypes.c_void_p,  # pReserved : void** optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('DNSAPI.dll')
DnsQuery_W = Fiddle::Function.new(
  lib['DnsQuery_W'],
  [
    Fiddle::TYPE_VOIDP,  # pszName : LPCWSTR
    -Fiddle::TYPE_SHORT,  # wType : WORD
    -Fiddle::TYPE_INT,  # Options : DNS_QUERY_OPTIONS
    Fiddle::TYPE_VOIDP,  # pExtra : void* optional, in/out
    Fiddle::TYPE_VOIDP,  # ppQueryResults : DNS_RECORDA** out
    Fiddle::TYPE_VOIDP,  # pReserved : void** optional, out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "dnsapi")]
extern "system" {
    fn DnsQuery_W(
        pszName: *const u16,  // LPCWSTR
        wType: u16,  // WORD
        Options: u32,  // DNS_QUERY_OPTIONS
        pExtra: *mut (),  // void* optional, in/out
        ppQueryResults: *mut *mut DNS_RECORDA,  // DNS_RECORDA** out
        pReserved: *mut *mut ()  // void** optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("DNSAPI.dll", CharSet = CharSet.Unicode)]
public static extern uint DnsQuery_W([MarshalAs(UnmanagedType.LPWStr)] string pszName, ushort wType, uint Options, IntPtr pExtra, IntPtr ppQueryResults, IntPtr pReserved);
"@
$api = Add-Type -MemberDefinition $sig -Name 'DNSAPI_DnsQuery_W' -Namespace Win32 -PassThru
# $api::DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
#uselib "DNSAPI.dll"
#func global DnsQuery_W "DnsQuery_W" wptr, wptr, wptr, wptr, wptr, wptr
; DnsQuery_W pszName, wType, Options, pExtra, varptr(ppQueryResults), pReserved   ; 戻り値は stat
; pszName : LPCWSTR -> "wptr"
; wType : WORD -> "wptr"
; Options : DNS_QUERY_OPTIONS -> "wptr"
; pExtra : void* optional, in/out -> "wptr"
; ppQueryResults : DNS_RECORDA** out -> "wptr"
; pReserved : void** optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "DNSAPI.dll"
#cfunc global DnsQuery_W "DnsQuery_W" wstr, int, int, sptr, var, sptr
; res = DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
; pszName : LPCWSTR -> "wstr"
; wType : WORD -> "int"
; Options : DNS_QUERY_OPTIONS -> "int"
; pExtra : void* optional, in/out -> "sptr"
; ppQueryResults : DNS_RECORDA** out -> "var"
; pReserved : void** optional, out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR DnsQuery_W(LPCWSTR pszName, WORD wType, DNS_QUERY_OPTIONS Options, void* pExtra, DNS_RECORDA** ppQueryResults, void** pReserved)
#uselib "DNSAPI.dll"
#cfunc global DnsQuery_W "DnsQuery_W" wstr, int, int, intptr, var, intptr
; res = DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
; pszName : LPCWSTR -> "wstr"
; wType : WORD -> "int"
; Options : DNS_QUERY_OPTIONS -> "int"
; pExtra : void* optional, in/out -> "intptr"
; ppQueryResults : DNS_RECORDA** out -> "var"
; pReserved : void** optional, out -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	dnsapi = windows.NewLazySystemDLL("DNSAPI.dll")
	procDnsQuery_W = dnsapi.NewProc("DnsQuery_W")
)

// pszName (LPCWSTR), wType (WORD), Options (DNS_QUERY_OPTIONS), pExtra (void* optional, in/out), ppQueryResults (DNS_RECORDA** out), pReserved (void** optional, out)
r1, _, err := procDnsQuery_W.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszName))),
	uintptr(wType),
	uintptr(Options),
	uintptr(pExtra),
	uintptr(ppQueryResults),
	uintptr(pReserved),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function DnsQuery_W(
  pszName: PWideChar;   // LPCWSTR
  wType: Word;   // WORD
  Options: DWORD;   // DNS_QUERY_OPTIONS
  pExtra: Pointer;   // void* optional, in/out
  ppQueryResults: Pointer;   // DNS_RECORDA** out
  pReserved: Pointer   // void** optional, out
): DWORD; stdcall;
  external 'DNSAPI.dll' name 'DnsQuery_W';
result := DllCall("DNSAPI\DnsQuery_W"
    , "WStr", pszName   ; LPCWSTR
    , "UShort", wType   ; WORD
    , "UInt", Options   ; DNS_QUERY_OPTIONS
    , "Ptr", pExtra   ; void* optional, in/out
    , "Ptr", ppQueryResults   ; DNS_RECORDA** out
    , "Ptr", pReserved   ; void** optional, out
    , "UInt")   ; return: WIN32_ERROR
●DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved) = DLL("DNSAPI.dll", "dword DnsQuery_W(char*, int, dword, void*, void*, void*)")
# 呼び出し: DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
# pszName : LPCWSTR -> "char*"
# wType : WORD -> "int"
# Options : DNS_QUERY_OPTIONS -> "dword"
# pExtra : void* optional, in/out -> "void*"
# ppQueryResults : DNS_RECORDA** out -> "void*"
# pReserved : void** optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "dnsapi" fn DnsQuery_W(
    pszName: [*c]const u16, // LPCWSTR
    wType: u16, // WORD
    Options: u32, // DNS_QUERY_OPTIONS
    pExtra: ?*anyopaque, // void* optional, in/out
    ppQueryResults: [*c][*c]DNS_RECORDA, // DNS_RECORDA** out
    pReserved: ?*anyopaque // void** optional, out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc DnsQuery_W(
    pszName: WideCString,  # LPCWSTR
    wType: uint16,  # WORD
    Options: uint32,  # DNS_QUERY_OPTIONS
    pExtra: pointer,  # void* optional, in/out
    ppQueryResults: ptr DNS_RECORDA,  # DNS_RECORDA** out
    pReserved: pointer  # void** optional, out
): uint32 {.importc: "DnsQuery_W", stdcall, dynlib: "DNSAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "dnsapi");
extern(Windows)
uint DnsQuery_W(
    const(wchar)* pszName,   // LPCWSTR
    ushort wType,   // WORD
    uint Options,   // DNS_QUERY_OPTIONS
    void* pExtra,   // void* optional, in/out
    DNS_RECORDA** ppQueryResults,   // DNS_RECORDA** out
    void** pReserved   // void** optional, out
);
ccall((:DnsQuery_W, "DNSAPI.dll"), stdcall, UInt32,
      (Cwstring, UInt16, UInt32, Ptr{Cvoid}, Ptr{DNS_RECORDA}, Ptr{Cvoid}),
      pszName, wType, Options, pExtra, ppQueryResults, pReserved)
# pszName : LPCWSTR -> Cwstring
# wType : WORD -> UInt16
# Options : DNS_QUERY_OPTIONS -> UInt32
# pExtra : void* optional, in/out -> Ptr{Cvoid}
# ppQueryResults : DNS_RECORDA** out -> Ptr{DNS_RECORDA}
# pReserved : void** optional, out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DnsQuery_W(
    const uint16_t* pszName,
    uint16_t wType,
    uint32_t Options,
    void* pExtra,
    void* ppQueryResults,
    void** pReserved);
]]
local dnsapi = ffi.load("dnsapi")
-- dnsapi.DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
-- pszName : LPCWSTR
-- wType : WORD
-- Options : DNS_QUERY_OPTIONS
-- pExtra : void* optional, in/out
-- ppQueryResults : DNS_RECORDA** out
-- pReserved : void** optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('DNSAPI.dll');
const DnsQuery_W = lib.func('__stdcall', 'DnsQuery_W', 'uint32_t', ['str16', 'uint16_t', 'uint32_t', 'void *', 'void *', 'void *']);
// DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
// pszName : LPCWSTR -> 'str16'
// wType : WORD -> 'uint16_t'
// Options : DNS_QUERY_OPTIONS -> 'uint32_t'
// pExtra : void* optional, in/out -> 'void *'
// ppQueryResults : DNS_RECORDA** out -> 'void *'
// pReserved : void** optional, out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("DNSAPI.dll", {
  DnsQuery_W: { parameters: ["buffer", "u16", "u32", "pointer", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved)
// pszName : LPCWSTR -> "buffer"
// wType : WORD -> "u16"
// Options : DNS_QUERY_OPTIONS -> "u32"
// pExtra : void* optional, in/out -> "pointer"
// ppQueryResults : DNS_RECORDA** out -> "pointer"
// pReserved : void** optional, out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DnsQuery_W(
    const uint16_t* pszName,
    uint16_t wType,
    uint32_t Options,
    void* pExtra,
    void* ppQueryResults,
    void** pReserved);
C, "DNSAPI.dll");
// $ffi->DnsQuery_W(pszName, wType, Options, pExtra, ppQueryResults, pReserved);
// pszName : LPCWSTR
// wType : WORD
// Options : DNS_QUERY_OPTIONS
// pExtra : void* optional, in/out
// ppQueryResults : DNS_RECORDA** out
// pReserved : void** optional, out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Dnsapi extends StdCallLibrary {
    Dnsapi INSTANCE = Native.load("dnsapi", Dnsapi.class, W32APIOptions.UNICODE_OPTIONS);
    int DnsQuery_W(
        WString pszName,   // LPCWSTR
        short wType,   // WORD
        int Options,   // DNS_QUERY_OPTIONS
        Pointer pExtra,   // void* optional, in/out
        Pointer ppQueryResults,   // DNS_RECORDA** out
        Pointer pReserved   // void** optional, out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("dnsapi")]
lib LibDNSAPI
  fun DnsQuery_W = DnsQuery_W(
    pszName : UInt16*,   # LPCWSTR
    wType : UInt16,   # WORD
    Options : UInt32,   # DNS_QUERY_OPTIONS
    pExtra : Void*,   # void* optional, in/out
    ppQueryResults : DNS_RECORDA**,   # DNS_RECORDA** out
    pReserved : Void**   # void** optional, out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DnsQuery_WNative = Uint32 Function(Pointer<Utf16>, Uint16, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Void>);
typedef DnsQuery_WDart = int Function(Pointer<Utf16>, int, int, Pointer<Void>, Pointer<Void>, Pointer<Void>);
final DnsQuery_W = DynamicLibrary.open('DNSAPI.dll')
    .lookupFunction<DnsQuery_WNative, DnsQuery_WDart>('DnsQuery_W');
// pszName : LPCWSTR -> Pointer<Utf16>
// wType : WORD -> Uint16
// Options : DNS_QUERY_OPTIONS -> Uint32
// pExtra : void* optional, in/out -> Pointer<Void>
// ppQueryResults : DNS_RECORDA** out -> Pointer<Void>
// pReserved : void** optional, out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DnsQuery_W(
  pszName: PWideChar;   // LPCWSTR
  wType: Word;   // WORD
  Options: DWORD;   // DNS_QUERY_OPTIONS
  pExtra: Pointer;   // void* optional, in/out
  ppQueryResults: Pointer;   // DNS_RECORDA** out
  pReserved: Pointer   // void** optional, out
): DWORD; stdcall;
  external 'DNSAPI.dll' name 'DnsQuery_W';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DnsQuery_W"
  c_DnsQuery_W :: CWString -> Word16 -> Word32 -> Ptr () -> Ptr () -> Ptr () -> IO Word32
-- pszName : LPCWSTR -> CWString
-- wType : WORD -> Word16
-- Options : DNS_QUERY_OPTIONS -> Word32
-- pExtra : void* optional, in/out -> Ptr ()
-- ppQueryResults : DNS_RECORDA** out -> Ptr ()
-- pReserved : void** optional, out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let dnsquery_w =
  foreign "DnsQuery_W"
    ((ptr uint16_t) @-> uint16_t @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> returning uint32_t)
(* pszName : LPCWSTR -> (ptr uint16_t) *)
(* wType : WORD -> uint16_t *)
(* Options : DNS_QUERY_OPTIONS -> uint32_t *)
(* pExtra : void* optional, in/out -> (ptr void) *)
(* ppQueryResults : DNS_RECORDA** out -> (ptr void) *)
(* pReserved : void** optional, out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dnsapi (t "DNSAPI.dll"))
(cffi:use-foreign-library dnsapi)

(cffi:defcfun ("DnsQuery_W" dns-query-w :convention :stdcall) :uint32
  (psz-name (:string :encoding :utf-16le))   ; LPCWSTR
  (w-type :uint16)   ; WORD
  (options :uint32)   ; DNS_QUERY_OPTIONS
  (p-extra :pointer)   ; void* optional, in/out
  (pp-query-results :pointer)   ; DNS_RECORDA** out
  (p-reserved :pointer))   ; void** optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DnsQuery_W = Win32::API::More->new('DNSAPI',
    'DWORD DnsQuery_W(LPCWSTR pszName, WORD wType, DWORD Options, LPVOID pExtra, LPVOID ppQueryResults, LPVOID pReserved)');
# my $ret = $DnsQuery_W->Call($pszName, $wType, $Options, $pExtra, $ppQueryResults, $pReserved);
# pszName : LPCWSTR -> LPCWSTR
# wType : WORD -> WORD
# Options : DNS_QUERY_OPTIONS -> DWORD
# pExtra : void* optional, in/out -> LPVOID
# ppQueryResults : DNS_RECORDA** out -> LPVOID
# pReserved : void** optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型