ホーム › Networking.WinHttp › WinHttpSetOption
WinHttpSetOption
関数WinHTTPハンドルの指定したオプション値を設定する。
シグネチャ
// WINHTTP.dll
#include <windows.h>
BOOL WinHttpSetOption(
void* hInternet, // optional
DWORD dwOption,
void* lpBuffer, // optional
DWORD dwBufferLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hInternet | void* | inoptional | データを設定する対象の HINTERNET ハンドル。設定するオプションに応じて、これは Session ハンドルまたは Request ハンドルのいずれかになる点に注意してください。特定のオプションを設定する際にどちらのハンドルを使用するのが適切かを判断する方法について詳しくは、Option Flags を参照してください。 |
| dwOption | DWORD | in | 設定するインターネットオプションを格納する符号なし long 整数値。これは Option Flags の値のいずれかを指定できます。 |
| lpBuffer | void* | inoptional | オプション設定値を格納するバッファーへのポインター。 |
| dwBufferLength | DWORD | in | lpBuffer バッファーの長さを格納する符号なし long 整数値。次のオプションではバッファーの長さを文字数で指定します。その他のすべてのオプションでは、長さをバイト数で指定します。 |
戻り値の型: BOOL
公式ドキュメント
WinHttpSetOption 関数は、インターネットオプションを設定します。
戻り値
成功した場合は TRUE を、それ以外の場合は FALSE を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。返されるエラーコードには次のものがあります。
| エラーコード | 説明 |
|---|---|
| 指定されたハンドルが正しい状態にないため、要求された操作を実行できません。 | |
| 指定されたハンドルの種類が、この操作に対して正しくありません。 | |
| 内部エラーが発生しました。 | |
|
WinHttpQueryOption または WinHttpSetOption への要求で、無効なオプション値が指定されました。 |
|
|
パラメーターが有効ではありません。
WINHTTP_OPTION_WEB_SOCKET_KEEPALIVE_INTERVAL が 15000 未満の値に設定された場合、この値が返されます。 |
|
| 要求されたオプションは設定できず、照会のみが可能です。 | |
|
パラメーターが有効ではありません。
WINHTTP_OPTION_WEB_SOCKET_KEEPALIVE_INTERVAL が 15000 未満の値に設定された場合、この値が返されます。 |
|
| 要求された操作を完了するのに十分なメモリがありませんでした。(Windows エラーコード) |
解説(Remarks)
WinHttpSetOption に渡された資格情報は、予期せず平文で送信される可能性があります。資格情報の設定には、WinHttpSetOption ではなく WinHttpQueryAuthSchemes および WinHttpSetCredentials を使用することを強くお勧めします。
注意 ただし、Passport 認証を使用する場合、407 ステータスコードに応答する WinHTTP アプリケーションは、プロキシ資格情報を提供するために WinHttpSetCredentials ではなく WinHttpSetOption を使用する必要があります。これは Passport 認証を使用する場合にのみ当てはまります。その他のすべての状況では WinHttpSetCredentials を使用してください。
設定できないオプションフラグが指定された場合、 GetLastError はエラー ERROR_INVALID_PARAMETER を返します。
WinHttpSetOption の使用方法を示す詳細情報とコード例については、Authentication in WinHTTP を参照してください。
注意 Windows XP および Windows 2000 については、WinHttp スタートページの Run-Time Requirements セクションを参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// WINHTTP.dll
#include <windows.h>
BOOL WinHttpSetOption(
void* hInternet, // optional
DWORD dwOption,
void* lpBuffer, // optional
DWORD dwBufferLength
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpSetOption(
IntPtr hInternet, // void* optional
uint dwOption, // DWORD
IntPtr lpBuffer, // void* optional
uint dwBufferLength // DWORD
);<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpSetOption(
hInternet As IntPtr, ' void* optional
dwOption As UInteger, ' DWORD
lpBuffer As IntPtr, ' void* optional
dwBufferLength As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hInternet : void* optional
' dwOption : DWORD
' lpBuffer : void* optional
' dwBufferLength : DWORD
Declare PtrSafe Function WinHttpSetOption Lib "winhttp" ( _
ByVal hInternet As LongPtr, _
ByVal dwOption As Long, _
ByVal lpBuffer As LongPtr, _
ByVal dwBufferLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WinHttpSetOption = ctypes.windll.winhttp.WinHttpSetOption
WinHttpSetOption.restype = wintypes.BOOL
WinHttpSetOption.argtypes = [
ctypes.POINTER(None), # hInternet : void* optional
wintypes.DWORD, # dwOption : DWORD
ctypes.POINTER(None), # lpBuffer : void* optional
wintypes.DWORD, # dwBufferLength : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpSetOption = Fiddle::Function.new(
lib['WinHttpSetOption'],
[
Fiddle::TYPE_VOIDP, # hInternet : void* optional
-Fiddle::TYPE_INT, # dwOption : DWORD
Fiddle::TYPE_VOIDP, # lpBuffer : void* optional
-Fiddle::TYPE_INT, # dwBufferLength : DWORD
],
Fiddle::TYPE_INT)#[link(name = "winhttp")]
extern "system" {
fn WinHttpSetOption(
hInternet: *mut (), // void* optional
dwOption: u32, // DWORD
lpBuffer: *mut (), // void* optional
dwBufferLength: u32 // DWORD
) -> 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 WinHttpSetOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, uint dwBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpSetOption' -Namespace Win32 -PassThru
# $api::WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)#uselib "WINHTTP.dll"
#func global WinHttpSetOption "WinHttpSetOption" sptr, sptr, sptr, sptr
; WinHttpSetOption hInternet, dwOption, lpBuffer, dwBufferLength ; 戻り値は stat
; hInternet : void* optional -> "sptr"
; dwOption : DWORD -> "sptr"
; lpBuffer : void* optional -> "sptr"
; dwBufferLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WINHTTP.dll"
#cfunc global WinHttpSetOption "WinHttpSetOption" sptr, int, sptr, int
; res = WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
; hInternet : void* optional -> "sptr"
; dwOption : DWORD -> "int"
; lpBuffer : void* optional -> "sptr"
; dwBufferLength : DWORD -> "int"; BOOL WinHttpSetOption(void* hInternet, DWORD dwOption, void* lpBuffer, DWORD dwBufferLength)
#uselib "WINHTTP.dll"
#cfunc global WinHttpSetOption "WinHttpSetOption" intptr, int, intptr, int
; res = WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
; hInternet : void* optional -> "intptr"
; dwOption : DWORD -> "int"
; lpBuffer : void* optional -> "intptr"
; dwBufferLength : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
procWinHttpSetOption = winhttp.NewProc("WinHttpSetOption")
)
// hInternet (void* optional), dwOption (DWORD), lpBuffer (void* optional), dwBufferLength (DWORD)
r1, _, err := procWinHttpSetOption.Call(
uintptr(hInternet),
uintptr(dwOption),
uintptr(lpBuffer),
uintptr(dwBufferLength),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction WinHttpSetOption(
hInternet: Pointer; // void* optional
dwOption: DWORD; // DWORD
lpBuffer: Pointer; // void* optional
dwBufferLength: DWORD // DWORD
): BOOL; stdcall;
external 'WINHTTP.dll' name 'WinHttpSetOption';result := DllCall("WINHTTP\WinHttpSetOption"
, "Ptr", hInternet ; void* optional
, "UInt", dwOption ; DWORD
, "Ptr", lpBuffer ; void* optional
, "UInt", dwBufferLength ; DWORD
, "Int") ; return: BOOL●WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength) = DLL("WINHTTP.dll", "bool WinHttpSetOption(void*, dword, void*, dword)")
# 呼び出し: WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
# hInternet : void* optional -> "void*"
# dwOption : DWORD -> "dword"
# lpBuffer : void* optional -> "void*"
# dwBufferLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "winhttp" fn WinHttpSetOption(
hInternet: ?*anyopaque, // void* optional
dwOption: u32, // DWORD
lpBuffer: ?*anyopaque, // void* optional
dwBufferLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc WinHttpSetOption(
hInternet: pointer, # void* optional
dwOption: uint32, # DWORD
lpBuffer: pointer, # void* optional
dwBufferLength: uint32 # DWORD
): int32 {.importc: "WinHttpSetOption", stdcall, dynlib: "WINHTTP.dll".}pragma(lib, "winhttp");
extern(Windows)
int WinHttpSetOption(
void* hInternet, // void* optional
uint dwOption, // DWORD
void* lpBuffer, // void* optional
uint dwBufferLength // DWORD
);ccall((:WinHttpSetOption, "WINHTTP.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32, Ptr{Cvoid}, UInt32),
hInternet, dwOption, lpBuffer, dwBufferLength)
# hInternet : void* optional -> Ptr{Cvoid}
# dwOption : DWORD -> UInt32
# lpBuffer : void* optional -> Ptr{Cvoid}
# dwBufferLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t WinHttpSetOption(
void* hInternet,
uint32_t dwOption,
void* lpBuffer,
uint32_t dwBufferLength);
]]
local winhttp = ffi.load("winhttp")
-- winhttp.WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
-- hInternet : void* optional
-- dwOption : DWORD
-- lpBuffer : void* optional
-- dwBufferLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WINHTTP.dll');
const WinHttpSetOption = lib.func('__stdcall', 'WinHttpSetOption', 'int32_t', ['void *', 'uint32_t', 'void *', 'uint32_t']);
// WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
// hInternet : void* optional -> 'void *'
// dwOption : DWORD -> 'uint32_t'
// lpBuffer : void* optional -> 'void *'
// dwBufferLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WINHTTP.dll", {
WinHttpSetOption: { parameters: ["pointer", "u32", "pointer", "u32"], result: "i32" },
});
// lib.symbols.WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength)
// hInternet : void* optional -> "pointer"
// dwOption : DWORD -> "u32"
// lpBuffer : void* optional -> "pointer"
// dwBufferLength : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t WinHttpSetOption(
void* hInternet,
uint32_t dwOption,
void* lpBuffer,
uint32_t dwBufferLength);
C, "WINHTTP.dll");
// $ffi->WinHttpSetOption(hInternet, dwOption, lpBuffer, dwBufferLength);
// hInternet : void* optional
// dwOption : DWORD
// lpBuffer : void* optional
// dwBufferLength : DWORD
// 構造体/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 WinHttpSetOption(
Pointer hInternet, // void* optional
int dwOption, // DWORD
Pointer lpBuffer, // void* optional
int dwBufferLength // DWORD
);
}@[Link("winhttp")]
lib LibWINHTTP
fun WinHttpSetOption = WinHttpSetOption(
hInternet : Void*, # void* optional
dwOption : UInt32, # DWORD
lpBuffer : Void*, # void* optional
dwBufferLength : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WinHttpSetOptionNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>, Uint32);
typedef WinHttpSetOptionDart = int Function(Pointer<Void>, int, Pointer<Void>, int);
final WinHttpSetOption = DynamicLibrary.open('WINHTTP.dll')
.lookupFunction<WinHttpSetOptionNative, WinHttpSetOptionDart>('WinHttpSetOption');
// hInternet : void* optional -> Pointer<Void>
// dwOption : DWORD -> Uint32
// lpBuffer : void* optional -> Pointer<Void>
// dwBufferLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WinHttpSetOption(
hInternet: Pointer; // void* optional
dwOption: DWORD; // DWORD
lpBuffer: Pointer; // void* optional
dwBufferLength: DWORD // DWORD
): BOOL; stdcall;
external 'WINHTTP.dll' name 'WinHttpSetOption';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WinHttpSetOption"
c_WinHttpSetOption :: Ptr () -> Word32 -> Ptr () -> Word32 -> IO CInt
-- hInternet : void* optional -> Ptr ()
-- dwOption : DWORD -> Word32
-- lpBuffer : void* optional -> Ptr ()
-- dwBufferLength : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let winhttpsetoption =
foreign "WinHttpSetOption"
((ptr void) @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hInternet : void* optional -> (ptr void) *)
(* dwOption : DWORD -> uint32_t *)
(* lpBuffer : void* optional -> (ptr void) *)
(* dwBufferLength : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library winhttp (t "WINHTTP.dll"))
(cffi:use-foreign-library winhttp)
(cffi:defcfun ("WinHttpSetOption" win-http-set-option :convention :stdcall) :int32
(h-internet :pointer) ; void* optional
(dw-option :uint32) ; DWORD
(lp-buffer :pointer) ; void* optional
(dw-buffer-length :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WinHttpSetOption = Win32::API::More->new('WINHTTP',
'BOOL WinHttpSetOption(LPVOID hInternet, DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength)');
# my $ret = $WinHttpSetOption->Call($hInternet, $dwOption, $lpBuffer, $dwBufferLength);
# hInternet : void* optional -> LPVOID
# dwOption : DWORD -> DWORD
# lpBuffer : void* optional -> LPVOID
# dwBufferLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f WinHttpCloseHandle — WinHTTPハンドルを閉じてリソースを解放する。
- f WinHttpOpen — WinHTTPセッションを初期化しセッションハンドルを返す。
- f WinHttpQueryOption — WinHTTPハンドルの指定したオプション値を取得する。