Win32 API 日本語リファレンス
ホームNetworking.WinHttp › WinHttpQueryOption

WinHttpQueryOption

関数
WinHTTPハンドルの指定したオプション値を取得する。
DLLWINHTTP.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL WinHttpQueryOption(
    void* hInternet,
    DWORD dwOption,
    void* lpBuffer,   // optional
    DWORD* lpdwBufferLength
);

パラメーター

名前方向説明
hInternetvoid*inout情報を照会する HINTERNET ハンドル。照会するオプションに応じて、これはセッションハンドルとリクエストハンドルのいずれかになります。特定のオプションを照会する際にどちらのハンドルを使用するのが適切かを判断するには、Option Flags のトピックを参照してください。
dwOptionDWORDin照会するインターネットオプションを格納する unsigned long 整数値。これは Option Flags の値のいずれかになります。
lpBuffervoid*outoptionalオプション設定を受け取るバッファへのポインター。 WinHttpQueryOption 関数が返す文字列はグローバルに割り当てられるため、呼び出し元のアプリケーションは使用を終えたらその文字列をグローバルに解放する必要があります。このパラメーターを NULL に設定すると、この関数は FALSE を返します。続けて GetLastError を呼び出すと ERROR_INSUFFICIENT_BUFFER が返され、 lpdwBufferLength には要求された情報を保持するために必要なバイト数が格納されます。
lpdwBufferLengthDWORD*inoutlpBuffer の長さ(バイト単位)を格納する unsigned long 整数変数へのポインター。関数が返ると、この変数には lpBuffer に格納されたデータの長さが設定されます。 GetLastErrorERROR_INSUFFICIENT_BUFFER を返す場合、このパラメーターには要求された情報を保持するために必要なバイト数が設定されます。

戻り値の型: BOOL

公式ドキュメント

WinHttpQueryOption 関数は、指定されたハンドルに対してインターネットオプションを照会します。

戻り値

成功した場合は TRUE を、それ以外の場合は FALSE を返します。具体的なエラーメッセージを取得するには、 GetLastError を呼び出します。返されるエラーコードには次のものがあります。

エラーコード 説明
ERROR_WINHTTP_INCORRECT_HANDLE_STATE
指定されたハンドルが正しい状態にないため、要求された操作を実行できません。
ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
指定されたハンドルの種類がこの操作に対して正しくありません。
ERROR_WINHTTP_INTERNAL_ERROR
内部エラーが発生しました。
ERROR_WINHTTP_INVALID_OPTION
無効なオプション値が指定されました。
ERROR_NOT_ENOUGH_MEMORY
要求された操作を完了するために十分なメモリがありませんでした。(Windows エラーコード)

解説(Remarks)

WinHTTP が非同期モードで使用されている場合(つまり、WinHttpOpenWINHTTP_FLAG_ASYNC が設定されている場合)でも、この関数は同期的に動作します。戻り値は成功または失敗を示します。拡張エラー情報を取得するには、 GetLastError を呼び出します。

指定されたハンドルの種類に対して無効なオプションフラグが dwOption パラメーターに渡された場合、 GetLastErrorERROR_INVALID_PARAMETER を返します。

注意 Windows XP および Windows 2000 については、WinHttp スタートページの Run-Time Requirements セクションを参照してください。

この例では、接続のタイムアウト値を取得する方法を示します。

    DWORD data;
    DWORD dwSize = sizeof(DWORD);

    // Use WinHttpOpen to obtain an HINTERNET handle.
    HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", 
                                    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                    WINHTTP_NO_PROXY_NAME, 
                                    WINHTTP_NO_PROXY_BYPASS, 0);
    if (hSession)
    {


        // Use WinHttpQueryOption to retrieve internet options.
        if (WinHttpQueryOption( hSession, 
                                WINHTTP_OPTION_CONNECT_TIMEOUT, 
                                &data, &dwSize))
        {
            printf("Connection timeout: %u ms\n\n",data);
        }
        else
        {
            printf( "Error %u in WinHttpQueryOption.\n", GetLastError());
        }        
        
        // When finished, release the HINTERNET handle.
        WinHttpCloseHandle(hSession);
    }
    else
    {
        printf("Error %u in WinHttpOpen.\n", GetLastError());
    }
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL WinHttpQueryOption(
    void* hInternet,
    DWORD dwOption,
    void* lpBuffer,   // optional
    DWORD* lpdwBufferLength
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpQueryOption(
    IntPtr hInternet,   // void* in/out
    uint dwOption,   // DWORD
    IntPtr lpBuffer,   // void* optional, out
    ref uint lpdwBufferLength   // DWORD* in/out
);
<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpQueryOption(
    hInternet As IntPtr,   ' void* in/out
    dwOption As UInteger,   ' DWORD
    lpBuffer As IntPtr,   ' void* optional, out
    ByRef lpdwBufferLength As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hInternet : void* in/out
' dwOption : DWORD
' lpBuffer : void* optional, out
' lpdwBufferLength : DWORD* in/out
Declare PtrSafe Function WinHttpQueryOption Lib "winhttp" ( _
    ByVal hInternet As LongPtr, _
    ByVal dwOption As Long, _
    ByVal lpBuffer As LongPtr, _
    ByRef lpdwBufferLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpQueryOption = ctypes.windll.winhttp.WinHttpQueryOption
WinHttpQueryOption.restype = wintypes.BOOL
WinHttpQueryOption.argtypes = [
    ctypes.POINTER(None),  # hInternet : void* in/out
    wintypes.DWORD,  # dwOption : DWORD
    ctypes.POINTER(None),  # lpBuffer : void* optional, out
    ctypes.POINTER(wintypes.DWORD),  # lpdwBufferLength : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpQueryOption = Fiddle::Function.new(
  lib['WinHttpQueryOption'],
  [
    Fiddle::TYPE_VOIDP,  # hInternet : void* in/out
    -Fiddle::TYPE_INT,  # dwOption : DWORD
    Fiddle::TYPE_VOIDP,  # lpBuffer : void* optional, out
    Fiddle::TYPE_VOIDP,  # lpdwBufferLength : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "winhttp")]
extern "system" {
    fn WinHttpQueryOption(
        hInternet: *mut (),  // void* in/out
        dwOption: u32,  // DWORD
        lpBuffer: *mut (),  // void* optional, out
        lpdwBufferLength: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true)]
public static extern bool WinHttpQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref uint lpdwBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpQueryOption' -Namespace Win32 -PassThru
# $api::WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
#uselib "WINHTTP.dll"
#func global WinHttpQueryOption "WinHttpQueryOption" sptr, sptr, sptr, sptr
; WinHttpQueryOption hInternet, dwOption, lpBuffer, varptr(lpdwBufferLength)   ; 戻り値は stat
; hInternet : void* in/out -> "sptr"
; dwOption : DWORD -> "sptr"
; lpBuffer : void* optional, out -> "sptr"
; lpdwBufferLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WINHTTP.dll"
#cfunc global WinHttpQueryOption "WinHttpQueryOption" sptr, int, sptr, var
; res = WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
; hInternet : void* in/out -> "sptr"
; dwOption : DWORD -> "int"
; lpBuffer : void* optional, out -> "sptr"
; lpdwBufferLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL WinHttpQueryOption(void* hInternet, DWORD dwOption, void* lpBuffer, DWORD* lpdwBufferLength)
#uselib "WINHTTP.dll"
#cfunc global WinHttpQueryOption "WinHttpQueryOption" intptr, int, intptr, var
; res = WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
; hInternet : void* in/out -> "intptr"
; dwOption : DWORD -> "int"
; lpBuffer : void* optional, out -> "intptr"
; lpdwBufferLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpQueryOption = winhttp.NewProc("WinHttpQueryOption")
)

// hInternet (void* in/out), dwOption (DWORD), lpBuffer (void* optional, out), lpdwBufferLength (DWORD* in/out)
r1, _, err := procWinHttpQueryOption.Call(
	uintptr(hInternet),
	uintptr(dwOption),
	uintptr(lpBuffer),
	uintptr(lpdwBufferLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WinHttpQueryOption(
  hInternet: Pointer;   // void* in/out
  dwOption: DWORD;   // DWORD
  lpBuffer: Pointer;   // void* optional, out
  lpdwBufferLength: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpQueryOption';
result := DllCall("WINHTTP\WinHttpQueryOption"
    , "Ptr", hInternet   ; void* in/out
    , "UInt", dwOption   ; DWORD
    , "Ptr", lpBuffer   ; void* optional, out
    , "Ptr", lpdwBufferLength   ; DWORD* in/out
    , "Int")   ; return: BOOL
●WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength) = DLL("WINHTTP.dll", "bool WinHttpQueryOption(void*, dword, void*, void*)")
# 呼び出し: WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
# hInternet : void* in/out -> "void*"
# dwOption : DWORD -> "dword"
# lpBuffer : void* optional, out -> "void*"
# lpdwBufferLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "winhttp" fn WinHttpQueryOption(
    hInternet: ?*anyopaque, // void* in/out
    dwOption: u32, // DWORD
    lpBuffer: ?*anyopaque, // void* optional, out
    lpdwBufferLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc WinHttpQueryOption(
    hInternet: pointer,  # void* in/out
    dwOption: uint32,  # DWORD
    lpBuffer: pointer,  # void* optional, out
    lpdwBufferLength: ptr uint32  # DWORD* in/out
): int32 {.importc: "WinHttpQueryOption", stdcall, dynlib: "WINHTTP.dll".}
pragma(lib, "winhttp");
extern(Windows)
int WinHttpQueryOption(
    void* hInternet,   // void* in/out
    uint dwOption,   // DWORD
    void* lpBuffer,   // void* optional, out
    uint* lpdwBufferLength   // DWORD* in/out
);
ccall((:WinHttpQueryOption, "WINHTTP.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Ptr{Cvoid}, Ptr{UInt32}),
      hInternet, dwOption, lpBuffer, lpdwBufferLength)
# hInternet : void* in/out -> Ptr{Cvoid}
# dwOption : DWORD -> UInt32
# lpBuffer : void* optional, out -> Ptr{Cvoid}
# lpdwBufferLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WinHttpQueryOption(
    void* hInternet,
    uint32_t dwOption,
    void* lpBuffer,
    uint32_t* lpdwBufferLength);
]]
local winhttp = ffi.load("winhttp")
-- winhttp.WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
-- hInternet : void* in/out
-- dwOption : DWORD
-- lpBuffer : void* optional, out
-- lpdwBufferLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WINHTTP.dll');
const WinHttpQueryOption = lib.func('__stdcall', 'WinHttpQueryOption', 'int32_t', ['void *', 'uint32_t', 'void *', 'uint32_t *']);
// WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
// hInternet : void* in/out -> 'void *'
// dwOption : DWORD -> 'uint32_t'
// lpBuffer : void* optional, out -> 'void *'
// lpdwBufferLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WINHTTP.dll", {
  WinHttpQueryOption: { parameters: ["pointer", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength)
// hInternet : void* in/out -> "pointer"
// dwOption : DWORD -> "u32"
// lpBuffer : void* optional, out -> "pointer"
// lpdwBufferLength : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WinHttpQueryOption(
    void* hInternet,
    uint32_t dwOption,
    void* lpBuffer,
    uint32_t* lpdwBufferLength);
C, "WINHTTP.dll");
// $ffi->WinHttpQueryOption(hInternet, dwOption, lpBuffer, lpdwBufferLength);
// hInternet : void* in/out
// dwOption : DWORD
// lpBuffer : void* optional, out
// lpdwBufferLength : DWORD* in/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 Winhttp extends StdCallLibrary {
    Winhttp INSTANCE = Native.load("winhttp", Winhttp.class);
    boolean WinHttpQueryOption(
        Pointer hInternet,   // void* in/out
        int dwOption,   // DWORD
        Pointer lpBuffer,   // void* optional, out
        IntByReference lpdwBufferLength   // DWORD* in/out
    );
}
@[Link("winhttp")]
lib LibWINHTTP
  fun WinHttpQueryOption = WinHttpQueryOption(
    hInternet : Void*,   # void* in/out
    dwOption : UInt32,   # DWORD
    lpBuffer : Void*,   # void* optional, out
    lpdwBufferLength : UInt32*   # DWORD* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WinHttpQueryOptionNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>, Pointer<Uint32>);
typedef WinHttpQueryOptionDart = int Function(Pointer<Void>, int, Pointer<Void>, Pointer<Uint32>);
final WinHttpQueryOption = DynamicLibrary.open('WINHTTP.dll')
    .lookupFunction<WinHttpQueryOptionNative, WinHttpQueryOptionDart>('WinHttpQueryOption');
// hInternet : void* in/out -> Pointer<Void>
// dwOption : DWORD -> Uint32
// lpBuffer : void* optional, out -> Pointer<Void>
// lpdwBufferLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WinHttpQueryOption(
  hInternet: Pointer;   // void* in/out
  dwOption: DWORD;   // DWORD
  lpBuffer: Pointer;   // void* optional, out
  lpdwBufferLength: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpQueryOption';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WinHttpQueryOption"
  c_WinHttpQueryOption :: Ptr () -> Word32 -> Ptr () -> Ptr Word32 -> IO CInt
-- hInternet : void* in/out -> Ptr ()
-- dwOption : DWORD -> Word32
-- lpBuffer : void* optional, out -> Ptr ()
-- lpdwBufferLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let winhttpqueryoption =
  foreign "WinHttpQueryOption"
    ((ptr void) @-> uint32_t @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* hInternet : void* in/out -> (ptr void) *)
(* dwOption : DWORD -> uint32_t *)
(* lpBuffer : void* optional, out -> (ptr void) *)
(* lpdwBufferLength : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library winhttp (t "WINHTTP.dll"))
(cffi:use-foreign-library winhttp)

(cffi:defcfun ("WinHttpQueryOption" win-http-query-option :convention :stdcall) :int32
  (h-internet :pointer)   ; void* in/out
  (dw-option :uint32)   ; DWORD
  (lp-buffer :pointer)   ; void* optional, out
  (lpdw-buffer-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WinHttpQueryOption = Win32::API::More->new('WINHTTP',
    'BOOL WinHttpQueryOption(LPVOID hInternet, DWORD dwOption, LPVOID lpBuffer, LPVOID lpdwBufferLength)');
# my $ret = $WinHttpQueryOption->Call($hInternet, $dwOption, $lpBuffer, $lpdwBufferLength);
# hInternet : void* in/out -> LPVOID
# dwOption : DWORD -> DWORD
# lpBuffer : void* optional, out -> LPVOID
# lpdwBufferLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目