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

WinHttpSetCredentials

関数
HTTP要求に認証用の資格情報を設定する。
DLLWINHTTP.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL WinHttpSetCredentials(
    void* hRequest,
    DWORD AuthTargets,
    DWORD AuthScheme,
    LPCWSTR pwszUserName,
    LPCWSTR pwszPassword,
    void* pAuthParams
);

パラメーター

名前方向
hRequestvoid*inout
AuthTargetsDWORDin
AuthSchemeDWORDin
pwszUserNameLPCWSTRin
pwszPasswordLPCWSTRin
pAuthParamsvoid*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL WinHttpSetCredentials(
    void* hRequest,
    DWORD AuthTargets,
    DWORD AuthScheme,
    LPCWSTR pwszUserName,
    LPCWSTR pwszPassword,
    void* pAuthParams
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINHTTP.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WinHttpSetCredentials(
    IntPtr hRequest,   // void* in/out
    uint AuthTargets,   // DWORD
    uint AuthScheme,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword,   // LPCWSTR
    IntPtr pAuthParams   // void* in/out
);
<DllImport("WINHTTP.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WinHttpSetCredentials(
    hRequest As IntPtr,   ' void* in/out
    AuthTargets As UInteger,   ' DWORD
    AuthScheme As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pwszUserName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> pwszPassword As String,   ' LPCWSTR
    pAuthParams As IntPtr   ' void* in/out
) As Boolean
End Function
' hRequest : void* in/out
' AuthTargets : DWORD
' AuthScheme : DWORD
' pwszUserName : LPCWSTR
' pwszPassword : LPCWSTR
' pAuthParams : void* in/out
Declare PtrSafe Function WinHttpSetCredentials Lib "winhttp" ( _
    ByVal hRequest As LongPtr, _
    ByVal AuthTargets As Long, _
    ByVal AuthScheme As Long, _
    ByVal pwszUserName As LongPtr, _
    ByVal pwszPassword As LongPtr, _
    ByVal pAuthParams As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpSetCredentials = ctypes.windll.winhttp.WinHttpSetCredentials
WinHttpSetCredentials.restype = wintypes.BOOL
WinHttpSetCredentials.argtypes = [
    ctypes.POINTER(None),  # hRequest : void* in/out
    wintypes.DWORD,  # AuthTargets : DWORD
    wintypes.DWORD,  # AuthScheme : DWORD
    wintypes.LPCWSTR,  # pwszUserName : LPCWSTR
    wintypes.LPCWSTR,  # pwszPassword : LPCWSTR
    ctypes.POINTER(None),  # pAuthParams : void* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpSetCredentials = Fiddle::Function.new(
  lib['WinHttpSetCredentials'],
  [
    Fiddle::TYPE_VOIDP,  # hRequest : void* in/out
    -Fiddle::TYPE_INT,  # AuthTargets : DWORD
    -Fiddle::TYPE_INT,  # AuthScheme : DWORD
    Fiddle::TYPE_VOIDP,  # pwszUserName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pwszPassword : LPCWSTR
    Fiddle::TYPE_VOIDP,  # pAuthParams : void* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "winhttp")]
extern "system" {
    fn WinHttpSetCredentials(
        hRequest: *mut (),  // void* in/out
        AuthTargets: u32,  // DWORD
        AuthScheme: u32,  // DWORD
        pwszUserName: *const u16,  // LPCWSTR
        pwszPassword: *const u16,  // LPCWSTR
        pAuthParams: *mut ()  // void* 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 WinHttpSetCredentials(IntPtr hRequest, uint AuthTargets, uint AuthScheme, [MarshalAs(UnmanagedType.LPWStr)] string pwszUserName, [MarshalAs(UnmanagedType.LPWStr)] string pwszPassword, IntPtr pAuthParams);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpSetCredentials' -Namespace Win32 -PassThru
# $api::WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
#uselib "WINHTTP.dll"
#func global WinHttpSetCredentials "WinHttpSetCredentials" sptr, sptr, sptr, sptr, sptr, sptr
; WinHttpSetCredentials hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams   ; 戻り値は stat
; hRequest : void* in/out -> "sptr"
; AuthTargets : DWORD -> "sptr"
; AuthScheme : DWORD -> "sptr"
; pwszUserName : LPCWSTR -> "sptr"
; pwszPassword : LPCWSTR -> "sptr"
; pAuthParams : void* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WINHTTP.dll"
#cfunc global WinHttpSetCredentials "WinHttpSetCredentials" sptr, int, int, wstr, wstr, sptr
; res = WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
; hRequest : void* in/out -> "sptr"
; AuthTargets : DWORD -> "int"
; AuthScheme : DWORD -> "int"
; pwszUserName : LPCWSTR -> "wstr"
; pwszPassword : LPCWSTR -> "wstr"
; pAuthParams : void* in/out -> "sptr"
; BOOL WinHttpSetCredentials(void* hRequest, DWORD AuthTargets, DWORD AuthScheme, LPCWSTR pwszUserName, LPCWSTR pwszPassword, void* pAuthParams)
#uselib "WINHTTP.dll"
#cfunc global WinHttpSetCredentials "WinHttpSetCredentials" intptr, int, int, wstr, wstr, intptr
; res = WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
; hRequest : void* in/out -> "intptr"
; AuthTargets : DWORD -> "int"
; AuthScheme : DWORD -> "int"
; pwszUserName : LPCWSTR -> "wstr"
; pwszPassword : LPCWSTR -> "wstr"
; pAuthParams : void* in/out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpSetCredentials = winhttp.NewProc("WinHttpSetCredentials")
)

// hRequest (void* in/out), AuthTargets (DWORD), AuthScheme (DWORD), pwszUserName (LPCWSTR), pwszPassword (LPCWSTR), pAuthParams (void* in/out)
r1, _, err := procWinHttpSetCredentials.Call(
	uintptr(hRequest),
	uintptr(AuthTargets),
	uintptr(AuthScheme),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszUserName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pwszPassword))),
	uintptr(pAuthParams),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WinHttpSetCredentials(
  hRequest: Pointer;   // void* in/out
  AuthTargets: DWORD;   // DWORD
  AuthScheme: DWORD;   // DWORD
  pwszUserName: PWideChar;   // LPCWSTR
  pwszPassword: PWideChar;   // LPCWSTR
  pAuthParams: Pointer   // void* in/out
): BOOL; stdcall;
  external 'WINHTTP.dll' name 'WinHttpSetCredentials';
result := DllCall("WINHTTP\WinHttpSetCredentials"
    , "Ptr", hRequest   ; void* in/out
    , "UInt", AuthTargets   ; DWORD
    , "UInt", AuthScheme   ; DWORD
    , "WStr", pwszUserName   ; LPCWSTR
    , "WStr", pwszPassword   ; LPCWSTR
    , "Ptr", pAuthParams   ; void* in/out
    , "Int")   ; return: BOOL
●WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams) = DLL("WINHTTP.dll", "bool WinHttpSetCredentials(void*, dword, dword, char*, char*, void*)")
# 呼び出し: WinHttpSetCredentials(hRequest, AuthTargets, AuthScheme, pwszUserName, pwszPassword, pAuthParams)
# hRequest : void* in/out -> "void*"
# AuthTargets : DWORD -> "dword"
# AuthScheme : DWORD -> "dword"
# pwszUserName : LPCWSTR -> "char*"
# pwszPassword : LPCWSTR -> "char*"
# pAuthParams : void* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。