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

FtpCommandA

関数
FTPサーバーへ任意のコマンドを直接送信する(ANSI版)。
DLLWININET.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL FtpCommandA(
    void* hConnect,
    BOOL fExpectResponse,
    FTP_FLAGS dwFlags,
    LPCSTR lpszCommand,
    UINT_PTR dwContext,   // optional
    void** phFtpCommand   // optional
);

パラメーター

名前方向説明
hConnectvoid*inInternetConnect の呼び出しによって返されたハンドル。
fExpectResponseBOOLinFTP サーバーによってデータ接続が確立されることをアプリケーションが想定しているかどうかを示す Boolean 値。データ接続を想定する場合は TRUE に、そうでない場合は FALSE に設定する必要があります。
dwFlagsFTP_FLAGSin

次のいずれかの値に設定できるパラメーター。

意味
FTP_TRANSFER_TYPE_ASCII
FTP ASCII (Type A) 転送方式を使用してファイルを転送します。制御データおよび書式設定データはローカルの相当する形式に変換されます。
FTP_TRANSFER_TYPE_BINARY
FTP Image (Type I) 転送方式を使用してファイルを転送します。ファイルは変更なしでそのまま転送されます。これは既定の転送方式です。
lpszCommandLPCSTRinFTP サーバーに送信するコマンドを格納する文字列へのポインター。
dwContextUINT_PTRinoptionalコールバック操作でアプリケーションのコンテキストを識別するために使用される、アプリケーション定義の値を格納する変数へのポインター。
phFtpCommandvoid**outoptional有効なデータソケットが開かれた場合に作成されるハンドルへのポインター。 phFtpCommand に値が設定されるためには、 fExpectResponse パラメーターを TRUE に設定する必要があります。

戻り値の型: BOOL

公式ドキュメント

FTP サーバーにコマンドを直接送信します。(ANSI)

戻り値

成功した場合は TRUE を、それ以外の場合は FALSE を返します。特定のエラーメッセージを取得するには、 GetLastError を呼び出します。

解説(Remarks)

クライアントアプリケーションがオフラインの場合、 GetLastErrorERROR_INTERNET_NO_DIRECT_ACCESS を返すことがあります。1 つ以上のパラメーターが無効な場合、 GetLastErrorERROR_INVALID_PARAMETER を返します。

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

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

wininet.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして FtpCommand を定義しています。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、コンパイルエラーまたは実行時エラーを引き起こす不一致が発生する可能性があります。詳細については、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>

BOOL FtpCommandA(
    void* hConnect,
    BOOL fExpectResponse,
    FTP_FLAGS dwFlags,
    LPCSTR lpszCommand,
    UINT_PTR dwContext,   // optional
    void** phFtpCommand   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WININET.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool FtpCommandA(
    IntPtr hConnect,   // void*
    bool fExpectResponse,   // BOOL
    uint dwFlags,   // FTP_FLAGS
    [MarshalAs(UnmanagedType.LPStr)] string lpszCommand,   // LPCSTR
    UIntPtr dwContext,   // UINT_PTR optional
    IntPtr phFtpCommand   // void** optional, out
);
<DllImport("WININET.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FtpCommandA(
    hConnect As IntPtr,   ' void*
    fExpectResponse As Boolean,   ' BOOL
    dwFlags As UInteger,   ' FTP_FLAGS
    <MarshalAs(UnmanagedType.LPStr)> lpszCommand As String,   ' LPCSTR
    dwContext As UIntPtr,   ' UINT_PTR optional
    phFtpCommand As IntPtr   ' void** optional, out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hConnect : void*
' fExpectResponse : BOOL
' dwFlags : FTP_FLAGS
' lpszCommand : LPCSTR
' dwContext : UINT_PTR optional
' phFtpCommand : void** optional, out
Declare PtrSafe Function FtpCommandA Lib "wininet" ( _
    ByVal hConnect As LongPtr, _
    ByVal fExpectResponse As Long, _
    ByVal dwFlags As Long, _
    ByVal lpszCommand As String, _
    ByVal dwContext As LongPtr, _
    ByVal phFtpCommand As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

FtpCommandA = ctypes.windll.wininet.FtpCommandA
FtpCommandA.restype = wintypes.BOOL
FtpCommandA.argtypes = [
    ctypes.POINTER(None),  # hConnect : void*
    wintypes.BOOL,  # fExpectResponse : BOOL
    wintypes.DWORD,  # dwFlags : FTP_FLAGS
    wintypes.LPCSTR,  # lpszCommand : LPCSTR
    ctypes.c_size_t,  # dwContext : UINT_PTR optional
    ctypes.c_void_p,  # phFtpCommand : void** optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WININET.dll')
FtpCommandA = Fiddle::Function.new(
  lib['FtpCommandA'],
  [
    Fiddle::TYPE_VOIDP,  # hConnect : void*
    Fiddle::TYPE_INT,  # fExpectResponse : BOOL
    -Fiddle::TYPE_INT,  # dwFlags : FTP_FLAGS
    Fiddle::TYPE_VOIDP,  # lpszCommand : LPCSTR
    Fiddle::TYPE_UINTPTR_T,  # dwContext : UINT_PTR optional
    Fiddle::TYPE_VOIDP,  # phFtpCommand : void** optional, out
  ],
  Fiddle::TYPE_INT)
#[link(name = "wininet")]
extern "system" {
    fn FtpCommandA(
        hConnect: *mut (),  // void*
        fExpectResponse: i32,  // BOOL
        dwFlags: u32,  // FTP_FLAGS
        lpszCommand: *const u8,  // LPCSTR
        dwContext: usize,  // UINT_PTR optional
        phFtpCommand: *mut *mut ()  // void** optional, out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WININET.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool FtpCommandA(IntPtr hConnect, bool fExpectResponse, uint dwFlags, [MarshalAs(UnmanagedType.LPStr)] string lpszCommand, UIntPtr dwContext, IntPtr phFtpCommand);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WININET_FtpCommandA' -Namespace Win32 -PassThru
# $api::FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
#uselib "WININET.dll"
#func global FtpCommandA "FtpCommandA" sptr, sptr, sptr, sptr, sptr, sptr
; FtpCommandA hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand   ; 戻り値は stat
; hConnect : void* -> "sptr"
; fExpectResponse : BOOL -> "sptr"
; dwFlags : FTP_FLAGS -> "sptr"
; lpszCommand : LPCSTR -> "sptr"
; dwContext : UINT_PTR optional -> "sptr"
; phFtpCommand : void** optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WININET.dll"
#cfunc global FtpCommandA "FtpCommandA" sptr, int, int, str, sptr, sptr
; res = FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
; hConnect : void* -> "sptr"
; fExpectResponse : BOOL -> "int"
; dwFlags : FTP_FLAGS -> "int"
; lpszCommand : LPCSTR -> "str"
; dwContext : UINT_PTR optional -> "sptr"
; phFtpCommand : void** optional, out -> "sptr"
; BOOL FtpCommandA(void* hConnect, BOOL fExpectResponse, FTP_FLAGS dwFlags, LPCSTR lpszCommand, UINT_PTR dwContext, void** phFtpCommand)
#uselib "WININET.dll"
#cfunc global FtpCommandA "FtpCommandA" intptr, int, int, str, intptr, intptr
; res = FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
; hConnect : void* -> "intptr"
; fExpectResponse : BOOL -> "int"
; dwFlags : FTP_FLAGS -> "int"
; lpszCommand : LPCSTR -> "str"
; dwContext : UINT_PTR optional -> "intptr"
; phFtpCommand : void** optional, out -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wininet = windows.NewLazySystemDLL("WININET.dll")
	procFtpCommandA = wininet.NewProc("FtpCommandA")
)

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

extern "wininet" fn FtpCommandA(
    hConnect: ?*anyopaque, // void*
    fExpectResponse: i32, // BOOL
    dwFlags: u32, // FTP_FLAGS
    lpszCommand: [*c]const u8, // LPCSTR
    dwContext: usize, // UINT_PTR optional
    phFtpCommand: ?*anyopaque // void** optional, out
) callconv(std.os.windows.WINAPI) i32;
proc FtpCommandA(
    hConnect: pointer,  # void*
    fExpectResponse: int32,  # BOOL
    dwFlags: uint32,  # FTP_FLAGS
    lpszCommand: cstring,  # LPCSTR
    dwContext: uint,  # UINT_PTR optional
    phFtpCommand: pointer  # void** optional, out
): int32 {.importc: "FtpCommandA", stdcall, dynlib: "WININET.dll".}
pragma(lib, "wininet");
extern(Windows)
int FtpCommandA(
    void* hConnect,   // void*
    int fExpectResponse,   // BOOL
    uint dwFlags,   // FTP_FLAGS
    const(char)* lpszCommand,   // LPCSTR
    size_t dwContext,   // UINT_PTR optional
    void** phFtpCommand   // void** optional, out
);
ccall((:FtpCommandA, "WININET.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Int32, UInt32, Cstring, Csize_t, Ptr{Cvoid}),
      hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
# hConnect : void* -> Ptr{Cvoid}
# fExpectResponse : BOOL -> Int32
# dwFlags : FTP_FLAGS -> UInt32
# lpszCommand : LPCSTR -> Cstring
# dwContext : UINT_PTR optional -> Csize_t
# phFtpCommand : void** optional, out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t FtpCommandA(
    void* hConnect,
    int32_t fExpectResponse,
    uint32_t dwFlags,
    const char* lpszCommand,
    uintptr_t dwContext,
    void** phFtpCommand);
]]
local wininet = ffi.load("wininet")
-- wininet.FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
-- hConnect : void*
-- fExpectResponse : BOOL
-- dwFlags : FTP_FLAGS
-- lpszCommand : LPCSTR
-- dwContext : UINT_PTR optional
-- phFtpCommand : void** optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WININET.dll');
const FtpCommandA = lib.func('__stdcall', 'FtpCommandA', 'int32_t', ['void *', 'int32_t', 'uint32_t', 'str', 'uintptr_t', 'void *']);
// FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
// hConnect : void* -> 'void *'
// fExpectResponse : BOOL -> 'int32_t'
// dwFlags : FTP_FLAGS -> 'uint32_t'
// lpszCommand : LPCSTR -> 'str'
// dwContext : UINT_PTR optional -> 'uintptr_t'
// phFtpCommand : void** optional, out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WININET.dll", {
  FtpCommandA: { parameters: ["pointer", "i32", "u32", "buffer", "usize", "pointer"], result: "i32" },
});
// lib.symbols.FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand)
// hConnect : void* -> "pointer"
// fExpectResponse : BOOL -> "i32"
// dwFlags : FTP_FLAGS -> "u32"
// lpszCommand : LPCSTR -> "buffer"
// dwContext : UINT_PTR optional -> "usize"
// phFtpCommand : void** optional, out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t FtpCommandA(
    void* hConnect,
    int32_t fExpectResponse,
    uint32_t dwFlags,
    const char* lpszCommand,
    size_t dwContext,
    void** phFtpCommand);
C, "WININET.dll");
// $ffi->FtpCommandA(hConnect, fExpectResponse, dwFlags, lpszCommand, dwContext, phFtpCommand);
// hConnect : void*
// fExpectResponse : BOOL
// dwFlags : FTP_FLAGS
// lpszCommand : LPCSTR
// dwContext : UINT_PTR optional
// phFtpCommand : void** optional, out
// 構造体/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);
    boolean FtpCommandA(
        Pointer hConnect,   // void*
        boolean fExpectResponse,   // BOOL
        int dwFlags,   // FTP_FLAGS
        String lpszCommand,   // LPCSTR
        long dwContext,   // UINT_PTR optional
        Pointer phFtpCommand   // void** optional, out
    );
}
@[Link("wininet")]
lib LibWININET
  fun FtpCommandA = FtpCommandA(
    hConnect : Void*,   # void*
    fExpectResponse : Int32,   # BOOL
    dwFlags : UInt32,   # FTP_FLAGS
    lpszCommand : UInt8*,   # LPCSTR
    dwContext : LibC::SizeT,   # UINT_PTR optional
    phFtpCommand : Void**   # void** optional, out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef FtpCommandANative = Int32 Function(Pointer<Void>, Int32, Uint32, Pointer<Utf8>, UintPtr, Pointer<Void>);
typedef FtpCommandADart = int Function(Pointer<Void>, int, int, Pointer<Utf8>, int, Pointer<Void>);
final FtpCommandA = DynamicLibrary.open('WININET.dll')
    .lookupFunction<FtpCommandANative, FtpCommandADart>('FtpCommandA');
// hConnect : void* -> Pointer<Void>
// fExpectResponse : BOOL -> Int32
// dwFlags : FTP_FLAGS -> Uint32
// lpszCommand : LPCSTR -> Pointer<Utf8>
// dwContext : UINT_PTR optional -> UintPtr
// phFtpCommand : void** optional, out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function FtpCommandA(
  hConnect: Pointer;   // void*
  fExpectResponse: BOOL;   // BOOL
  dwFlags: DWORD;   // FTP_FLAGS
  lpszCommand: PAnsiChar;   // LPCSTR
  dwContext: NativeUInt;   // UINT_PTR optional
  phFtpCommand: Pointer   // void** optional, out
): BOOL; stdcall;
  external 'WININET.dll' name 'FtpCommandA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "FtpCommandA"
  c_FtpCommandA :: Ptr () -> CInt -> Word32 -> CString -> CUIntPtr -> Ptr () -> IO CInt
-- hConnect : void* -> Ptr ()
-- fExpectResponse : BOOL -> CInt
-- dwFlags : FTP_FLAGS -> Word32
-- lpszCommand : LPCSTR -> CString
-- dwContext : UINT_PTR optional -> CUIntPtr
-- phFtpCommand : void** optional, out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let ftpcommanda =
  foreign "FtpCommandA"
    ((ptr void) @-> int32_t @-> uint32_t @-> string @-> size_t @-> (ptr void) @-> returning int32_t)
(* hConnect : void* -> (ptr void) *)
(* fExpectResponse : BOOL -> int32_t *)
(* dwFlags : FTP_FLAGS -> uint32_t *)
(* lpszCommand : LPCSTR -> string *)
(* dwContext : UINT_PTR optional -> size_t *)
(* phFtpCommand : void** optional, out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library wininet (t "WININET.dll"))
(cffi:use-foreign-library wininet)

(cffi:defcfun ("FtpCommandA" ftp-command-a :convention :stdcall) :int32
  (h-connect :pointer)   ; void*
  (f-expect-response :int32)   ; BOOL
  (dw-flags :uint32)   ; FTP_FLAGS
  (lpsz-command :string)   ; LPCSTR
  (dw-context :uint64)   ; UINT_PTR optional
  (ph-ftp-command :pointer))   ; void** optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $FtpCommandA = Win32::API::More->new('WININET',
    'BOOL FtpCommandA(LPVOID hConnect, BOOL fExpectResponse, DWORD dwFlags, LPCSTR lpszCommand, WPARAM dwContext, LPVOID phFtpCommand)');
# my $ret = $FtpCommandA->Call($hConnect, $fExpectResponse, $dwFlags, $lpszCommand, $dwContext, $phFtpCommand);
# hConnect : void* -> LPVOID
# fExpectResponse : BOOL -> BOOL
# dwFlags : FTP_FLAGS -> DWORD
# lpszCommand : LPCSTR -> LPCSTR
# dwContext : UINT_PTR optional -> WPARAM
# phFtpCommand : void** optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
使用する型