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以前の InternetOpen の呼び出しで返されたハンドル。
lpszServerNameLPCWSTRinインターネットサーバーのホスト名を指定する null 終端文字列へのポインター。あるいは、ASCII のドット区切り 10 進形式 (例: 11.0.1.45) でサイトの IP 番号を含めることもできます。
nServerPortWORDin

サーバー上の TCP/IP (Transmission Control Protocol/Internet Protocol) ポート。これらのフラグは使用するポートのみを設定します。サービスは dwService の値によって設定されます。このパラメーターには次のいずれかの値を指定できます。

Value Meaning
INTERNET_DEFAULT_FTP_PORT
FTP サーバーの既定のポート (ポート 21) を使用します。
INTERNET_DEFAULT_GOPHER_PORT
Gopher サーバーの既定のポート (ポート 70) を使用します。
Note Windows XP および Windows Server 2003 R2 以前のみ。
INTERNET_DEFAULT_HTTP_PORT
HTTP サーバーの既定のポート (ポート 80) を使用します。
INTERNET_DEFAULT_HTTPS_PORT
HTTPS (Secure Hypertext Transfer Protocol) サーバーの既定のポート (ポート 443) を使用します。
INTERNET_DEFAULT_SOCKS_PORT
SOCKS ファイアウォールサーバーの既定のポート (ポート 1080) を使用します。
INTERNET_INVALID_PORT_NUMBER
dwService で指定されたサービスの既定のポートを使用します。
lpszUserNameLPCWSTRinoptionalログオンするユーザーの名前を指定する null 終端文字列へのポインター。このパラメーターが NULL の場合、関数は適切な既定値を使用します。FTP プロトコルの場合、既定値は "anonymous" です。
lpszPasswordLPCWSTRinoptionalログオンに使用するパスワードを含む null 終端文字列へのポインター。 lpszPasswordlpszUsername の両方が NULL の場合、関数は既定の "anonymous" パスワードを使用します。FTP の場合、既定のパスワードはユーザーのメール名です。 lpszPasswordNULLlpszUsernameNULL でない場合、関数は空のパスワードを使用します。
dwServiceDWORDin

アクセスするサービスの種類。このパラメーターには次のいずれかの値を指定できます。

Value Meaning
INTERNET_SERVICE_FTP
FTP サービス。
INTERNET_SERVICE_GOPHER
Gopher サービス。
Note Windows XP および Windows Server 2003 R2 以前のみ。
INTERNET_SERVICE_HTTP
HTTP サービス。
dwFlagsDWORDin使用するサービスに固有のオプション。 dwServiceINTERNET_SERVICE_FTP の場合、 INTERNET_FLAG_PASSIVE を指定すると、アプリケーションはパッシブ FTP セマンティクスを使用します。
dwContextUINT_PTRinoptional返されるハンドルのアプリケーションコンテキストをコールバックで識別するために使用される、アプリケーション定義の値を含む変数へのポインター。

戻り値の型: void*

公式ドキュメント

指定したサイトに対して FTP (File Transfer Protocol) または HTTP のセッションを開きます。(Unicode)

戻り値

接続が成功した場合はセッションへの有効なハンドルを返し、失敗した場合は NULL を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。アプリケーションは InternetGetLastResponseInfo を使用して、サービスへのアクセスが拒否された理由を判別することもできます。

解説(Remarks)

次の表は、 lpszUsernamelpszPassword の 4 通りの設定に対する動作を示しています。

lpszUsername lpszPassword FTP サーバーに送信されるユーザー名 FTP サーバーに送信されるパスワード
NULL NULL "anonymous" ユーザーのメール名
NULL 以外の文字列 NULL lpszUsername ""
NULL NULL 以外の文字列 ERROR ERROR
NULL 以外の文字列 NULL 以外の文字列 lpszUsername lpszPassword

FTP サイトの場合、 InternetConnect は実際にサーバーとの接続を確立します。その他の場合、実際の接続はアプリケーションが特定のトランザクションを要求するまで確立されません。

効率を最大化するため、HTTP プロトコルを使用するアプリケーションは InternetConnect の呼び出しを最小限に抑え、ユーザーが要求するトランザクションごとにこの関数を呼び出すことは避けるべきです。これを実現する 1 つの方法は、 InternetConnect から返されたハンドルの小さなキャッシュを保持することです。ユーザーが以前にアクセスしたサーバーへの要求を行ったとき、そのセッションハンドルがまだ利用可能になります。

呼び出し元のアプリケーションが InternetConnect から返された HINTERNET ハンドルの使用を終えた後は、 InternetCloseHandle 関数を使用してそのハンドルを閉じる必要があります。

Note 要求が非同期モードで送信され (InternetOpendwFlags パラメーターに INTERNET_FLAG_ASYNC が指定されている)、かつ dwContext パラメーターが 0 (INTERNET_NO_CALLBACK) の場合、接続ハンドルに対して InternetSetStatusCallback で設定されたコールバック関数は呼び出されませんが、呼び出しは引き続き非同期モードで実行されます。

InternetConnect の使用例は、次のトピックで確認できます。

WinINet API の他のすべての側面と同様に、この関数は DllMain やグローバルオブジェクトのコンストラクター/デストラクター内から安全に呼び出すことはできません。

Note WinINet はサーバー実装をサポートしていません。また、サービスから使用すべきではありません。サーバー実装やサービスには Microsoft Windows HTTP Services (WinHTTP) を使用してください。
メモ

wininet.h ヘッダーは InternetConnect をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不一致につながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// 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 版の利用を推奨。
const std = @import("std");

extern "wininet" fn InternetConnectW(
    hInternet: ?*anyopaque, // void*
    lpszServerName: [*c]const u16, // LPCWSTR
    nServerPort: u16, // WORD
    lpszUserName: [*c]const u16, // LPCWSTR optional
    lpszPassword: [*c]const u16, // LPCWSTR optional
    dwService: u32, // DWORD
    dwFlags: u32, // DWORD
    dwContext: usize // UINT_PTR optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc InternetConnectW(
    hInternet: pointer,  # void*
    lpszServerName: WideCString,  # LPCWSTR
    nServerPort: uint16,  # WORD
    lpszUserName: WideCString,  # LPCWSTR optional
    lpszPassword: WideCString,  # LPCWSTR optional
    dwService: uint32,  # DWORD
    dwFlags: uint32,  # DWORD
    dwContext: uint  # UINT_PTR optional
): pointer {.importc: "InternetConnectW", stdcall, dynlib: "WININET.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "wininet");
extern(Windows)
void* InternetConnectW(
    void* hInternet,   // void*
    const(wchar)* lpszServerName,   // LPCWSTR
    ushort nServerPort,   // WORD
    const(wchar)* lpszUserName,   // LPCWSTR optional
    const(wchar)* lpszPassword,   // LPCWSTR optional
    uint dwService,   // DWORD
    uint dwFlags,   // DWORD
    size_t dwContext   // UINT_PTR optional
);
ccall((:InternetConnectW, "WININET.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Cwstring, UInt16, Cwstring, Cwstring, UInt32, UInt32, Csize_t),
      hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
# hInternet : void* -> Ptr{Cvoid}
# lpszServerName : LPCWSTR -> Cwstring
# nServerPort : WORD -> UInt16
# lpszUserName : LPCWSTR optional -> Cwstring
# lpszPassword : LPCWSTR optional -> Cwstring
# dwService : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# dwContext : UINT_PTR optional -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* InternetConnectW(
    void* hInternet,
    const uint16_t* lpszServerName,
    uint16_t nServerPort,
    const uint16_t* lpszUserName,
    const uint16_t* lpszPassword,
    uint32_t dwService,
    uint32_t dwFlags,
    uintptr_t dwContext);
]]
local wininet = ffi.load("wininet")
-- wininet.InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
-- hInternet : void*
-- lpszServerName : LPCWSTR
-- nServerPort : WORD
-- lpszUserName : LPCWSTR optional
-- lpszPassword : LPCWSTR optional
-- dwService : DWORD
-- dwFlags : DWORD
-- dwContext : UINT_PTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('WININET.dll');
const InternetConnectW = lib.func('__stdcall', 'InternetConnectW', 'void *', ['void *', 'str16', 'uint16_t', 'str16', 'str16', 'uint32_t', 'uint32_t', 'uintptr_t']);
// InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
// hInternet : void* -> 'void *'
// lpszServerName : LPCWSTR -> 'str16'
// nServerPort : WORD -> 'uint16_t'
// lpszUserName : LPCWSTR optional -> 'str16'
// lpszPassword : LPCWSTR optional -> 'str16'
// dwService : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// dwContext : UINT_PTR optional -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WININET.dll", {
  InternetConnectW: { parameters: ["pointer", "buffer", "u16", "buffer", "buffer", "u32", "u32", "usize"], result: "pointer" },
});
// lib.symbols.InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
// hInternet : void* -> "pointer"
// lpszServerName : LPCWSTR -> "buffer"
// nServerPort : WORD -> "u16"
// lpszUserName : LPCWSTR optional -> "buffer"
// lpszPassword : LPCWSTR optional -> "buffer"
// dwService : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// dwContext : UINT_PTR optional -> "usize"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* InternetConnectW(
    void* hInternet,
    const uint16_t* lpszServerName,
    uint16_t nServerPort,
    const uint16_t* lpszUserName,
    const uint16_t* lpszPassword,
    uint32_t dwService,
    uint32_t dwFlags,
    size_t dwContext);
C, "WININET.dll");
// $ffi->InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
// hInternet : void*
// lpszServerName : LPCWSTR
// nServerPort : WORD
// lpszUserName : LPCWSTR optional
// lpszPassword : LPCWSTR optional
// dwService : DWORD
// dwFlags : DWORD
// dwContext : UINT_PTR optional
// 構造体/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 Wininet extends StdCallLibrary {
    Wininet INSTANCE = Native.load("wininet", Wininet.class, W32APIOptions.UNICODE_OPTIONS);
    Pointer InternetConnectW(
        Pointer hInternet,   // void*
        WString lpszServerName,   // LPCWSTR
        short nServerPort,   // WORD
        WString lpszUserName,   // LPCWSTR optional
        WString lpszPassword,   // LPCWSTR optional
        int dwService,   // DWORD
        int dwFlags,   // DWORD
        long dwContext   // UINT_PTR optional
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("wininet")]
lib LibWININET
  fun InternetConnectW = InternetConnectW(
    hInternet : Void*,   # void*
    lpszServerName : UInt16*,   # LPCWSTR
    nServerPort : UInt16,   # WORD
    lpszUserName : UInt16*,   # LPCWSTR optional
    lpszPassword : UInt16*,   # LPCWSTR optional
    dwService : UInt32,   # DWORD
    dwFlags : UInt32,   # DWORD
    dwContext : LibC::SizeT   # UINT_PTR optional
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef InternetConnectWNative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, Uint16, Pointer<Utf16>, Pointer<Utf16>, Uint32, Uint32, UintPtr);
typedef InternetConnectWDart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Utf16>, Pointer<Utf16>, int, int, int);
final InternetConnectW = DynamicLibrary.open('WININET.dll')
    .lookupFunction<InternetConnectWNative, InternetConnectWDart>('InternetConnectW');
// hInternet : void* -> Pointer<Void>
// lpszServerName : LPCWSTR -> Pointer<Utf16>
// nServerPort : WORD -> Uint16
// lpszUserName : LPCWSTR optional -> Pointer<Utf16>
// lpszPassword : LPCWSTR optional -> Pointer<Utf16>
// dwService : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// dwContext : UINT_PTR optional -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
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';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "InternetConnectW"
  c_InternetConnectW :: Ptr () -> CWString -> Word16 -> CWString -> CWString -> Word32 -> Word32 -> CUIntPtr -> IO (Ptr ())
-- hInternet : void* -> Ptr ()
-- lpszServerName : LPCWSTR -> CWString
-- nServerPort : WORD -> Word16
-- lpszUserName : LPCWSTR optional -> CWString
-- lpszPassword : LPCWSTR optional -> CWString
-- dwService : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- dwContext : UINT_PTR optional -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let internetconnectw =
  foreign "InternetConnectW"
    ((ptr void) @-> (ptr uint16_t) @-> uint16_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> size_t @-> returning (ptr void))
(* hInternet : void* -> (ptr void) *)
(* lpszServerName : LPCWSTR -> (ptr uint16_t) *)
(* nServerPort : WORD -> uint16_t *)
(* lpszUserName : LPCWSTR optional -> (ptr uint16_t) *)
(* lpszPassword : LPCWSTR optional -> (ptr uint16_t) *)
(* dwService : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* dwContext : UINT_PTR optional -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library wininet (t "WININET.dll"))
(cffi:use-foreign-library wininet)

(cffi:defcfun ("InternetConnectW" internet-connect-w :convention :stdcall) :pointer
  (h-internet :pointer)   ; void*
  (lpsz-server-name (:string :encoding :utf-16le))   ; LPCWSTR
  (n-server-port :uint16)   ; WORD
  (lpsz-user-name (:string :encoding :utf-16le))   ; LPCWSTR optional
  (lpsz-password (:string :encoding :utf-16le))   ; LPCWSTR optional
  (dw-service :uint32)   ; DWORD
  (dw-flags :uint32)   ; DWORD
  (dw-context :uint64))   ; UINT_PTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $InternetConnectW = Win32::API::More->new('WININET',
    'LPVOID InternetConnectW(LPVOID hInternet, LPCWSTR lpszServerName, WORD nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, WPARAM dwContext)');
# my $ret = $InternetConnectW->Call($hInternet, $lpszServerName, $nServerPort, $lpszUserName, $lpszPassword, $dwService, $dwFlags, $dwContext);
# hInternet : void* -> LPVOID
# lpszServerName : LPCWSTR -> LPCWSTR
# nServerPort : WORD -> WORD
# lpszUserName : LPCWSTR optional -> LPCWSTR
# lpszPassword : LPCWSTR optional -> LPCWSTR
# dwService : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# dwContext : UINT_PTR optional -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い