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

InternetConnectW

関数
指定サーバーへの接続を確立し接続ハンドルを返す(Unicode)。
DLLWININET.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

void* InternetConnectW(
    void* hInternet,
    LPCWSTR lpszServerName,
    WORD nServerPort,
    LPCWSTR lpszUserName,   // optional
    LPCWSTR lpszPassword,   // optional
    DWORD dwService,
    DWORD dwFlags,
    UINT_PTR dwContext   // optional
);

パラメーター

名前方向
hInternetvoid*in
lpszServerNameLPCWSTRin
nServerPortWORDin
lpszUserNameLPCWSTRinoptional
lpszPasswordLPCWSTRinoptional
dwServiceDWORDin
dwFlagsDWORDin
dwContextUINT_PTRinoptional

戻り値の型: void*

各言語での呼び出し定義

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

void* InternetConnectW(
    void* hInternet,
    LPCWSTR lpszServerName,
    WORD nServerPort,
    LPCWSTR lpszUserName,   // optional
    LPCWSTR lpszPassword,   // optional
    DWORD dwService,
    DWORD dwFlags,
    UINT_PTR dwContext   // optional
);
[DllImport("WININET.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr InternetConnectW(
    IntPtr hInternet,   // void*
    [MarshalAs(UnmanagedType.LPWStr)] string lpszServerName,   // LPCWSTR
    ushort nServerPort,   // WORD
    [MarshalAs(UnmanagedType.LPWStr)] string lpszUserName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpszPassword,   // LPCWSTR optional
    uint dwService,   // DWORD
    uint dwFlags,   // DWORD
    UIntPtr dwContext   // UINT_PTR optional
);
<DllImport("WININET.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function InternetConnectW(
    hInternet As IntPtr,   ' void*
    <MarshalAs(UnmanagedType.LPWStr)> lpszServerName As String,   ' LPCWSTR
    nServerPort As UShort,   ' WORD
    <MarshalAs(UnmanagedType.LPWStr)> lpszUserName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpszPassword As String,   ' LPCWSTR optional
    dwService As UInteger,   ' DWORD
    dwFlags As UInteger,   ' DWORD
    dwContext As UIntPtr   ' UINT_PTR optional
) As IntPtr
End Function
' hInternet : void*
' lpszServerName : LPCWSTR
' nServerPort : WORD
' lpszUserName : LPCWSTR optional
' lpszPassword : LPCWSTR optional
' dwService : DWORD
' dwFlags : DWORD
' dwContext : UINT_PTR optional
Declare PtrSafe Function InternetConnectW Lib "wininet" ( _
    ByVal hInternet As LongPtr, _
    ByVal lpszServerName As LongPtr, _
    ByVal nServerPort As Integer, _
    ByVal lpszUserName As LongPtr, _
    ByVal lpszPassword As LongPtr, _
    ByVal dwService As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As LongPtr) As LongPtr
' 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

InternetConnectW = ctypes.windll.wininet.InternetConnectW
InternetConnectW.restype = ctypes.c_void_p
InternetConnectW.argtypes = [
    ctypes.POINTER(None),  # hInternet : void*
    wintypes.LPCWSTR,  # lpszServerName : LPCWSTR
    ctypes.c_ushort,  # nServerPort : WORD
    wintypes.LPCWSTR,  # lpszUserName : LPCWSTR optional
    wintypes.LPCWSTR,  # lpszPassword : LPCWSTR optional
    wintypes.DWORD,  # dwService : DWORD
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.c_size_t,  # dwContext : UINT_PTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WININET.dll')
InternetConnectW = Fiddle::Function.new(
  lib['InternetConnectW'],
  [
    Fiddle::TYPE_VOIDP,  # hInternet : void*
    Fiddle::TYPE_VOIDP,  # lpszServerName : LPCWSTR
    -Fiddle::TYPE_SHORT,  # nServerPort : WORD
    Fiddle::TYPE_VOIDP,  # lpszUserName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpszPassword : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwService : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_UINTPTR_T,  # dwContext : UINT_PTR optional
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "wininet")]
extern "system" {
    fn InternetConnectW(
        hInternet: *mut (),  // void*
        lpszServerName: *const u16,  // LPCWSTR
        nServerPort: u16,  // WORD
        lpszUserName: *const u16,  // LPCWSTR optional
        lpszPassword: *const u16,  // LPCWSTR optional
        dwService: u32,  // DWORD
        dwFlags: u32,  // DWORD
        dwContext: usize  // UINT_PTR optional
    ) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WININET.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr InternetConnectW(IntPtr hInternet, [MarshalAs(UnmanagedType.LPWStr)] string lpszServerName, ushort nServerPort, [MarshalAs(UnmanagedType.LPWStr)] string lpszUserName, [MarshalAs(UnmanagedType.LPWStr)] string lpszPassword, uint dwService, uint dwFlags, UIntPtr dwContext);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WININET_InternetConnectW' -Namespace Win32 -PassThru
# $api::InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
#uselib "WININET.dll"
#func global InternetConnectW "InternetConnectW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; InternetConnectW hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext   ; 戻り値は stat
; hInternet : void* -> "wptr"
; lpszServerName : LPCWSTR -> "wptr"
; nServerPort : WORD -> "wptr"
; lpszUserName : LPCWSTR optional -> "wptr"
; lpszPassword : LPCWSTR optional -> "wptr"
; dwService : DWORD -> "wptr"
; dwFlags : DWORD -> "wptr"
; dwContext : UINT_PTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WININET.dll"
#cfunc global InternetConnectW "InternetConnectW" sptr, wstr, int, wstr, wstr, int, int, sptr
; res = InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
; hInternet : void* -> "sptr"
; lpszServerName : LPCWSTR -> "wstr"
; nServerPort : WORD -> "int"
; lpszUserName : LPCWSTR optional -> "wstr"
; lpszPassword : LPCWSTR optional -> "wstr"
; dwService : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwContext : UINT_PTR optional -> "sptr"
; void* InternetConnectW(void* hInternet, LPCWSTR lpszServerName, WORD nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, UINT_PTR dwContext)
#uselib "WININET.dll"
#cfunc global InternetConnectW "InternetConnectW" intptr, wstr, int, wstr, wstr, int, int, intptr
; res = InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
; hInternet : void* -> "intptr"
; lpszServerName : LPCWSTR -> "wstr"
; nServerPort : WORD -> "int"
; lpszUserName : LPCWSTR optional -> "wstr"
; lpszPassword : LPCWSTR optional -> "wstr"
; dwService : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwContext : UINT_PTR optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wininet = windows.NewLazySystemDLL("WININET.dll")
	procInternetConnectW = wininet.NewProc("InternetConnectW")
)

// hInternet (void*), lpszServerName (LPCWSTR), nServerPort (WORD), lpszUserName (LPCWSTR optional), lpszPassword (LPCWSTR optional), dwService (DWORD), dwFlags (DWORD), dwContext (UINT_PTR optional)
r1, _, err := procInternetConnectW.Call(
	uintptr(hInternet),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszServerName))),
	uintptr(nServerPort),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszUserName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszPassword))),
	uintptr(dwService),
	uintptr(dwFlags),
	uintptr(dwContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function InternetConnectW(
  hInternet: Pointer;   // void*
  lpszServerName: PWideChar;   // LPCWSTR
  nServerPort: Word;   // WORD
  lpszUserName: PWideChar;   // LPCWSTR optional
  lpszPassword: PWideChar;   // LPCWSTR optional
  dwService: DWORD;   // DWORD
  dwFlags: DWORD;   // DWORD
  dwContext: NativeUInt   // UINT_PTR optional
): Pointer; stdcall;
  external 'WININET.dll' name 'InternetConnectW';
result := DllCall("WININET\InternetConnectW"
    , "Ptr", hInternet   ; void*
    , "WStr", lpszServerName   ; LPCWSTR
    , "UShort", nServerPort   ; WORD
    , "WStr", lpszUserName   ; LPCWSTR optional
    , "WStr", lpszPassword   ; LPCWSTR optional
    , "UInt", dwService   ; DWORD
    , "UInt", dwFlags   ; DWORD
    , "UPtr", dwContext   ; UINT_PTR optional
    , "Ptr")   ; return: void*
●InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext) = DLL("WININET.dll", "void* InternetConnectW(void*, char*, int, char*, char*, dword, dword, int)")
# 呼び出し: InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
# hInternet : void* -> "void*"
# lpszServerName : LPCWSTR -> "char*"
# nServerPort : WORD -> "int"
# lpszUserName : LPCWSTR optional -> "char*"
# lpszPassword : LPCWSTR optional -> "char*"
# dwService : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# dwContext : UINT_PTR optional -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。