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

WinHttpOpen

関数
WinHTTPセッションを初期化しセッションハンドルを返す。
DLLWINHTTP.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

void* WinHttpOpen(
    LPCWSTR pszAgentW,   // optional
    WINHTTP_ACCESS_TYPE dwAccessType,
    LPCWSTR pszProxyW,   // optional
    LPCWSTR pszProxyBypassW,   // optional
    DWORD dwFlags
);

パラメーター

名前方向
pszAgentWLPCWSTRinoptional
dwAccessTypeWINHTTP_ACCESS_TYPEin
pszProxyWLPCWSTRinoptional
pszProxyBypassWLPCWSTRinoptional
dwFlagsDWORDin

戻り値の型: void*

各言語での呼び出し定義

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

void* WinHttpOpen(
    LPCWSTR pszAgentW,   // optional
    WINHTTP_ACCESS_TYPE dwAccessType,
    LPCWSTR pszProxyW,   // optional
    LPCWSTR pszProxyBypassW,   // optional
    DWORD dwFlags
);
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr WinHttpOpen(
    [MarshalAs(UnmanagedType.LPWStr)] string pszAgentW,   // LPCWSTR optional
    uint dwAccessType,   // WINHTTP_ACCESS_TYPE
    [MarshalAs(UnmanagedType.LPWStr)] string pszProxyW,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string pszProxyBypassW,   // LPCWSTR optional
    uint dwFlags   // DWORD
);
<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpOpen(
    <MarshalAs(UnmanagedType.LPWStr)> pszAgentW As String,   ' LPCWSTR optional
    dwAccessType As UInteger,   ' WINHTTP_ACCESS_TYPE
    <MarshalAs(UnmanagedType.LPWStr)> pszProxyW As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pszProxyBypassW As String,   ' LPCWSTR optional
    dwFlags As UInteger   ' DWORD
) As IntPtr
End Function
' pszAgentW : LPCWSTR optional
' dwAccessType : WINHTTP_ACCESS_TYPE
' pszProxyW : LPCWSTR optional
' pszProxyBypassW : LPCWSTR optional
' dwFlags : DWORD
Declare PtrSafe Function WinHttpOpen Lib "winhttp" ( _
    ByVal pszAgentW As LongPtr, _
    ByVal dwAccessType As Long, _
    ByVal pszProxyW As LongPtr, _
    ByVal pszProxyBypassW As LongPtr, _
    ByVal dwFlags As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpOpen = ctypes.windll.winhttp.WinHttpOpen
WinHttpOpen.restype = ctypes.c_void_p
WinHttpOpen.argtypes = [
    wintypes.LPCWSTR,  # pszAgentW : LPCWSTR optional
    wintypes.DWORD,  # dwAccessType : WINHTTP_ACCESS_TYPE
    wintypes.LPCWSTR,  # pszProxyW : LPCWSTR optional
    wintypes.LPCWSTR,  # pszProxyBypassW : LPCWSTR optional
    wintypes.DWORD,  # dwFlags : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpOpen = Fiddle::Function.new(
  lib['WinHttpOpen'],
  [
    Fiddle::TYPE_VOIDP,  # pszAgentW : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwAccessType : WINHTTP_ACCESS_TYPE
    Fiddle::TYPE_VOIDP,  # pszProxyW : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pszProxyBypassW : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "winhttp")]
extern "system" {
    fn WinHttpOpen(
        pszAgentW: *const u16,  // LPCWSTR optional
        dwAccessType: u32,  // WINHTTP_ACCESS_TYPE
        pszProxyW: *const u16,  // LPCWSTR optional
        pszProxyBypassW: *const u16,  // LPCWSTR optional
        dwFlags: u32  // DWORD
    ) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WINHTTP.dll", SetLastError = true)]
public static extern IntPtr WinHttpOpen([MarshalAs(UnmanagedType.LPWStr)] string pszAgentW, uint dwAccessType, [MarshalAs(UnmanagedType.LPWStr)] string pszProxyW, [MarshalAs(UnmanagedType.LPWStr)] string pszProxyBypassW, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpOpen' -Namespace Win32 -PassThru
# $api::WinHttpOpen(pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags)
#uselib "WINHTTP.dll"
#func global WinHttpOpen "WinHttpOpen" sptr, sptr, sptr, sptr, sptr
; WinHttpOpen pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags   ; 戻り値は stat
; pszAgentW : LPCWSTR optional -> "sptr"
; dwAccessType : WINHTTP_ACCESS_TYPE -> "sptr"
; pszProxyW : LPCWSTR optional -> "sptr"
; pszProxyBypassW : LPCWSTR optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WINHTTP.dll"
#cfunc global WinHttpOpen "WinHttpOpen" wstr, int, wstr, wstr, int
; res = WinHttpOpen(pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags)
; pszAgentW : LPCWSTR optional -> "wstr"
; dwAccessType : WINHTTP_ACCESS_TYPE -> "int"
; pszProxyW : LPCWSTR optional -> "wstr"
; pszProxyBypassW : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
; void* WinHttpOpen(LPCWSTR pszAgentW, WINHTTP_ACCESS_TYPE dwAccessType, LPCWSTR pszProxyW, LPCWSTR pszProxyBypassW, DWORD dwFlags)
#uselib "WINHTTP.dll"
#cfunc global WinHttpOpen "WinHttpOpen" wstr, int, wstr, wstr, int
; res = WinHttpOpen(pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags)
; pszAgentW : LPCWSTR optional -> "wstr"
; dwAccessType : WINHTTP_ACCESS_TYPE -> "int"
; pszProxyW : LPCWSTR optional -> "wstr"
; pszProxyBypassW : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpOpen = winhttp.NewProc("WinHttpOpen")
)

// pszAgentW (LPCWSTR optional), dwAccessType (WINHTTP_ACCESS_TYPE), pszProxyW (LPCWSTR optional), pszProxyBypassW (LPCWSTR optional), dwFlags (DWORD)
r1, _, err := procWinHttpOpen.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszAgentW))),
	uintptr(dwAccessType),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszProxyW))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszProxyBypassW))),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function WinHttpOpen(
  pszAgentW: PWideChar;   // LPCWSTR optional
  dwAccessType: DWORD;   // WINHTTP_ACCESS_TYPE
  pszProxyW: PWideChar;   // LPCWSTR optional
  pszProxyBypassW: PWideChar;   // LPCWSTR optional
  dwFlags: DWORD   // DWORD
): Pointer; stdcall;
  external 'WINHTTP.dll' name 'WinHttpOpen';
result := DllCall("WINHTTP\WinHttpOpen"
    , "WStr", pszAgentW   ; LPCWSTR optional
    , "UInt", dwAccessType   ; WINHTTP_ACCESS_TYPE
    , "WStr", pszProxyW   ; LPCWSTR optional
    , "WStr", pszProxyBypassW   ; LPCWSTR optional
    , "UInt", dwFlags   ; DWORD
    , "Ptr")   ; return: void*
●WinHttpOpen(pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags) = DLL("WINHTTP.dll", "void* WinHttpOpen(char*, dword, char*, char*, dword)")
# 呼び出し: WinHttpOpen(pszAgentW, dwAccessType, pszProxyW, pszProxyBypassW, dwFlags)
# pszAgentW : LPCWSTR optional -> "char*"
# dwAccessType : WINHTTP_ACCESS_TYPE -> "dword"
# pszProxyW : LPCWSTR optional -> "char*"
# pszProxyBypassW : LPCWSTR optional -> "char*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。