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

InternetConnectA

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

シグネチャ

// WININET.dll  (ANSI / -A)
#include <windows.h>

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

パラメーター

名前方向説明
hInternetvoid*in以前の InternetOpen の呼び出しによって返されたハンドル。
lpszServerNameLPCSTRinインターネットサーバーのホスト名を指定する null 終端文字列へのポインター。あるいは、この文字列にはサイトの IP 番号を ASCII のドット区切り 10 進形式 (例: 11.0.1.45) で指定することもできます。
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 で指定したサービスの既定のポートを使用します。
lpszUserNameLPCSTRinoptionalログオンするユーザーの名前を指定する null 終端文字列へのポインター。このパラメーターが NULL の場合、関数は適切な既定値を使用します。FTP プロトコルの場合、既定値は "anonymous" です。
lpszPasswordLPCSTRinoptionalログオンに使用するパスワードを格納する null 終端文字列へのポインター。 lpszPasswordlpszUsername の両方が NULL の場合、関数は既定の "anonymous" パスワードを使用します。FTP の場合、既定のパスワードはユーザーの電子メール名です。 lpszPasswordNULL で、 lpszUsernameNULL でない場合、関数は空のパスワードを使用します。
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 のセッションを開きます。(ANSI)

戻り値

接続に成功した場合はセッションへの有効なハンドルを返し、それ以外の場合は 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  (ANSI / -A)
#include <windows.h>

void* InternetConnectA(
    void* hInternet,
    LPCSTR lpszServerName,
    WORD nServerPort,
    LPCSTR lpszUserName,   // optional
    LPCSTR lpszPassword,   // optional
    DWORD dwService,
    DWORD dwFlags,
    UINT_PTR dwContext   // optional
);
[DllImport("WININET.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr InternetConnectA(
    IntPtr hInternet,   // void*
    [MarshalAs(UnmanagedType.LPStr)] string lpszServerName,   // LPCSTR
    ushort nServerPort,   // WORD
    [MarshalAs(UnmanagedType.LPStr)] string lpszUserName,   // LPCSTR optional
    [MarshalAs(UnmanagedType.LPStr)] string lpszPassword,   // LPCSTR optional
    uint dwService,   // DWORD
    uint dwFlags,   // DWORD
    UIntPtr dwContext   // UINT_PTR optional
);
<DllImport("WININET.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function InternetConnectA(
    hInternet As IntPtr,   ' void*
    <MarshalAs(UnmanagedType.LPStr)> lpszServerName As String,   ' LPCSTR
    nServerPort As UShort,   ' WORD
    <MarshalAs(UnmanagedType.LPStr)> lpszUserName As String,   ' LPCSTR optional
    <MarshalAs(UnmanagedType.LPStr)> lpszPassword As String,   ' LPCSTR optional
    dwService As UInteger,   ' DWORD
    dwFlags As UInteger,   ' DWORD
    dwContext As UIntPtr   ' UINT_PTR optional
) As IntPtr
End Function
' hInternet : void*
' lpszServerName : LPCSTR
' nServerPort : WORD
' lpszUserName : LPCSTR optional
' lpszPassword : LPCSTR optional
' dwService : DWORD
' dwFlags : DWORD
' dwContext : UINT_PTR optional
Declare PtrSafe Function InternetConnectA Lib "wininet" ( _
    ByVal hInternet As LongPtr, _
    ByVal lpszServerName As String, _
    ByVal nServerPort As Integer, _
    ByVal lpszUserName As String, _
    ByVal lpszPassword As String, _
    ByVal dwService As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

InternetConnectA = ctypes.windll.wininet.InternetConnectA
InternetConnectA.restype = ctypes.c_void_p
InternetConnectA.argtypes = [
    ctypes.POINTER(None),  # hInternet : void*
    wintypes.LPCSTR,  # lpszServerName : LPCSTR
    ctypes.c_ushort,  # nServerPort : WORD
    wintypes.LPCSTR,  # lpszUserName : LPCSTR optional
    wintypes.LPCSTR,  # lpszPassword : LPCSTR 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')
InternetConnectA = Fiddle::Function.new(
  lib['InternetConnectA'],
  [
    Fiddle::TYPE_VOIDP,  # hInternet : void*
    Fiddle::TYPE_VOIDP,  # lpszServerName : LPCSTR
    -Fiddle::TYPE_SHORT,  # nServerPort : WORD
    Fiddle::TYPE_VOIDP,  # lpszUserName : LPCSTR optional
    Fiddle::TYPE_VOIDP,  # lpszPassword : LPCSTR optional
    -Fiddle::TYPE_INT,  # dwService : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_UINTPTR_T,  # dwContext : UINT_PTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "wininet")]
extern "system" {
    fn InternetConnectA(
        hInternet: *mut (),  // void*
        lpszServerName: *const u8,  // LPCSTR
        nServerPort: u16,  // WORD
        lpszUserName: *const u8,  // LPCSTR optional
        lpszPassword: *const u8,  // LPCSTR 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.Ansi, SetLastError = true)]
public static extern IntPtr InternetConnectA(IntPtr hInternet, [MarshalAs(UnmanagedType.LPStr)] string lpszServerName, ushort nServerPort, [MarshalAs(UnmanagedType.LPStr)] string lpszUserName, [MarshalAs(UnmanagedType.LPStr)] string lpszPassword, uint dwService, uint dwFlags, UIntPtr dwContext);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WININET_InternetConnectA' -Namespace Win32 -PassThru
# $api::InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
#uselib "WININET.dll"
#func global InternetConnectA "InternetConnectA" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; InternetConnectA hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext   ; 戻り値は stat
; hInternet : void* -> "sptr"
; lpszServerName : LPCSTR -> "sptr"
; nServerPort : WORD -> "sptr"
; lpszUserName : LPCSTR optional -> "sptr"
; lpszPassword : LPCSTR optional -> "sptr"
; dwService : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; dwContext : UINT_PTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WININET.dll"
#cfunc global InternetConnectA "InternetConnectA" sptr, str, int, str, str, int, int, sptr
; res = InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
; hInternet : void* -> "sptr"
; lpszServerName : LPCSTR -> "str"
; nServerPort : WORD -> "int"
; lpszUserName : LPCSTR optional -> "str"
; lpszPassword : LPCSTR optional -> "str"
; dwService : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwContext : UINT_PTR optional -> "sptr"
; void* InternetConnectA(void* hInternet, LPCSTR lpszServerName, WORD nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, UINT_PTR dwContext)
#uselib "WININET.dll"
#cfunc global InternetConnectA "InternetConnectA" intptr, str, int, str, str, int, int, intptr
; res = InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
; hInternet : void* -> "intptr"
; lpszServerName : LPCSTR -> "str"
; nServerPort : WORD -> "int"
; lpszUserName : LPCSTR optional -> "str"
; lpszPassword : LPCSTR optional -> "str"
; dwService : DWORD -> "int"
; dwFlags : DWORD -> "int"
; dwContext : UINT_PTR optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wininet = windows.NewLazySystemDLL("WININET.dll")
	procInternetConnectA = wininet.NewProc("InternetConnectA")
)

// hInternet (void*), lpszServerName (LPCSTR), nServerPort (WORD), lpszUserName (LPCSTR optional), lpszPassword (LPCSTR optional), dwService (DWORD), dwFlags (DWORD), dwContext (UINT_PTR optional)
r1, _, err := procInternetConnectA.Call(
	uintptr(hInternet),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszServerName))),
	uintptr(nServerPort),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszUserName))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpszPassword))),
	uintptr(dwService),
	uintptr(dwFlags),
	uintptr(dwContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function InternetConnectA(
  hInternet: Pointer;   // void*
  lpszServerName: PAnsiChar;   // LPCSTR
  nServerPort: Word;   // WORD
  lpszUserName: PAnsiChar;   // LPCSTR optional
  lpszPassword: PAnsiChar;   // LPCSTR optional
  dwService: DWORD;   // DWORD
  dwFlags: DWORD;   // DWORD
  dwContext: NativeUInt   // UINT_PTR optional
): Pointer; stdcall;
  external 'WININET.dll' name 'InternetConnectA';
result := DllCall("WININET\InternetConnectA"
    , "Ptr", hInternet   ; void*
    , "AStr", lpszServerName   ; LPCSTR
    , "UShort", nServerPort   ; WORD
    , "AStr", lpszUserName   ; LPCSTR optional
    , "AStr", lpszPassword   ; LPCSTR optional
    , "UInt", dwService   ; DWORD
    , "UInt", dwFlags   ; DWORD
    , "UPtr", dwContext   ; UINT_PTR optional
    , "Ptr")   ; return: void*
●InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext) = DLL("WININET.dll", "void* InternetConnectA(void*, char*, int, char*, char*, dword, dword, int)")
# 呼び出し: InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
# hInternet : void* -> "void*"
# lpszServerName : LPCSTR -> "char*"
# nServerPort : WORD -> "int"
# lpszUserName : LPCSTR optional -> "char*"
# lpszPassword : LPCSTR optional -> "char*"
# dwService : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# dwContext : UINT_PTR optional -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "wininet" fn InternetConnectA(
    hInternet: ?*anyopaque, // void*
    lpszServerName: [*c]const u8, // LPCSTR
    nServerPort: u16, // WORD
    lpszUserName: [*c]const u8, // LPCSTR optional
    lpszPassword: [*c]const u8, // LPCSTR optional
    dwService: u32, // DWORD
    dwFlags: u32, // DWORD
    dwContext: usize // UINT_PTR optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc InternetConnectA(
    hInternet: pointer,  # void*
    lpszServerName: cstring,  # LPCSTR
    nServerPort: uint16,  # WORD
    lpszUserName: cstring,  # LPCSTR optional
    lpszPassword: cstring,  # LPCSTR optional
    dwService: uint32,  # DWORD
    dwFlags: uint32,  # DWORD
    dwContext: uint  # UINT_PTR optional
): pointer {.importc: "InternetConnectA", stdcall, dynlib: "WININET.dll".}
pragma(lib, "wininet");
extern(Windows)
void* InternetConnectA(
    void* hInternet,   // void*
    const(char)* lpszServerName,   // LPCSTR
    ushort nServerPort,   // WORD
    const(char)* lpszUserName,   // LPCSTR optional
    const(char)* lpszPassword,   // LPCSTR optional
    uint dwService,   // DWORD
    uint dwFlags,   // DWORD
    size_t dwContext   // UINT_PTR optional
);
ccall((:InternetConnectA, "WININET.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Cstring, UInt16, Cstring, Cstring, UInt32, UInt32, Csize_t),
      hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
# hInternet : void* -> Ptr{Cvoid}
# lpszServerName : LPCSTR -> Cstring
# nServerPort : WORD -> UInt16
# lpszUserName : LPCSTR optional -> Cstring
# lpszPassword : LPCSTR optional -> Cstring
# dwService : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# dwContext : UINT_PTR optional -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* InternetConnectA(
    void* hInternet,
    const char* lpszServerName,
    uint16_t nServerPort,
    const char* lpszUserName,
    const char* lpszPassword,
    uint32_t dwService,
    uint32_t dwFlags,
    uintptr_t dwContext);
]]
local wininet = ffi.load("wininet")
-- wininet.InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
-- hInternet : void*
-- lpszServerName : LPCSTR
-- nServerPort : WORD
-- lpszUserName : LPCSTR optional
-- lpszPassword : LPCSTR optional
-- dwService : DWORD
-- dwFlags : DWORD
-- dwContext : UINT_PTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WININET.dll');
const InternetConnectA = lib.func('__stdcall', 'InternetConnectA', 'void *', ['void *', 'str', 'uint16_t', 'str', 'str', 'uint32_t', 'uint32_t', 'uintptr_t']);
// InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
// hInternet : void* -> 'void *'
// lpszServerName : LPCSTR -> 'str'
// nServerPort : WORD -> 'uint16_t'
// lpszUserName : LPCSTR optional -> 'str'
// lpszPassword : LPCSTR optional -> 'str'
// dwService : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// dwContext : UINT_PTR optional -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WININET.dll", {
  InternetConnectA: { parameters: ["pointer", "buffer", "u16", "buffer", "buffer", "u32", "u32", "usize"], result: "pointer" },
});
// lib.symbols.InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext)
// hInternet : void* -> "pointer"
// lpszServerName : LPCSTR -> "buffer"
// nServerPort : WORD -> "u16"
// lpszUserName : LPCSTR optional -> "buffer"
// lpszPassword : LPCSTR optional -> "buffer"
// dwService : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// dwContext : UINT_PTR optional -> "usize"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* InternetConnectA(
    void* hInternet,
    const char* lpszServerName,
    uint16_t nServerPort,
    const char* lpszUserName,
    const char* lpszPassword,
    uint32_t dwService,
    uint32_t dwFlags,
    size_t dwContext);
C, "WININET.dll");
// $ffi->InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
// hInternet : void*
// lpszServerName : LPCSTR
// nServerPort : WORD
// lpszUserName : LPCSTR optional
// lpszPassword : LPCSTR 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.ASCII_OPTIONS);
    Pointer InternetConnectA(
        Pointer hInternet,   // void*
        String lpszServerName,   // LPCSTR
        short nServerPort,   // WORD
        String lpszUserName,   // LPCSTR optional
        String lpszPassword,   // LPCSTR optional
        int dwService,   // DWORD
        int dwFlags,   // DWORD
        long dwContext   // UINT_PTR optional
    );
}
@[Link("wininet")]
lib LibWININET
  fun InternetConnectA = InternetConnectA(
    hInternet : Void*,   # void*
    lpszServerName : UInt8*,   # LPCSTR
    nServerPort : UInt16,   # WORD
    lpszUserName : UInt8*,   # LPCSTR optional
    lpszPassword : UInt8*,   # LPCSTR 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 InternetConnectANative = Pointer<Void> Function(Pointer<Void>, Pointer<Utf8>, Uint16, Pointer<Utf8>, Pointer<Utf8>, Uint32, Uint32, UintPtr);
typedef InternetConnectADart = Pointer<Void> Function(Pointer<Void>, Pointer<Utf8>, int, Pointer<Utf8>, Pointer<Utf8>, int, int, int);
final InternetConnectA = DynamicLibrary.open('WININET.dll')
    .lookupFunction<InternetConnectANative, InternetConnectADart>('InternetConnectA');
// hInternet : void* -> Pointer<Void>
// lpszServerName : LPCSTR -> Pointer<Utf8>
// nServerPort : WORD -> Uint16
// lpszUserName : LPCSTR optional -> Pointer<Utf8>
// lpszPassword : LPCSTR optional -> Pointer<Utf8>
// dwService : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// dwContext : UINT_PTR optional -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function InternetConnectA(
  hInternet: Pointer;   // void*
  lpszServerName: PAnsiChar;   // LPCSTR
  nServerPort: Word;   // WORD
  lpszUserName: PAnsiChar;   // LPCSTR optional
  lpszPassword: PAnsiChar;   // LPCSTR optional
  dwService: DWORD;   // DWORD
  dwFlags: DWORD;   // DWORD
  dwContext: NativeUInt   // UINT_PTR optional
): Pointer; stdcall;
  external 'WININET.dll' name 'InternetConnectA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let internetconnecta =
  foreign "InternetConnectA"
    ((ptr void) @-> string @-> uint16_t @-> string @-> string @-> uint32_t @-> uint32_t @-> size_t @-> returning (ptr void))
(* hInternet : void* -> (ptr void) *)
(* lpszServerName : LPCSTR -> string *)
(* nServerPort : WORD -> uint16_t *)
(* lpszUserName : LPCSTR optional -> string *)
(* lpszPassword : LPCSTR optional -> string *)
(* 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 ("InternetConnectA" internet-connect-a :convention :stdcall) :pointer
  (h-internet :pointer)   ; void*
  (lpsz-server-name :string)   ; LPCSTR
  (n-server-port :uint16)   ; WORD
  (lpsz-user-name :string)   ; LPCSTR optional
  (lpsz-password :string)   ; LPCSTR 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 $InternetConnectA = Win32::API::More->new('WININET',
    'LPVOID InternetConnectA(LPVOID hInternet, LPCSTR lpszServerName, WORD nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, WPARAM dwContext)');
# my $ret = $InternetConnectA->Call($hInternet, $lpszServerName, $nServerPort, $lpszUserName, $lpszPassword, $dwService, $dwFlags, $dwContext);
# hInternet : void* -> LPVOID
# lpszServerName : LPCSTR -> LPCSTR
# nServerPort : WORD -> WORD
# lpszUserName : LPCSTR optional -> LPCSTR
# lpszPassword : LPCSTR optional -> LPCSTR
# dwService : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# dwContext : UINT_PTR optional -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い