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

TransmitFile

関数
ファイルの内容を接続済みソケットへ転送する。
DLLMSWSOCK.dll呼出規約winapi対応OSWindows 8.1 以降

シグネチャ

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

BOOL TransmitFile(
    SOCKET hSocket,
    HANDLE hFile,
    DWORD nNumberOfBytesToWrite,
    DWORD nNumberOfBytesPerSend,
    OVERLAPPED* lpOverlapped,   // optional
    TRANSMIT_FILE_BUFFERS* lpTransmitBuffers,   // optional
    DWORD dwReserved
);

パラメーター

名前方向
hSocketSOCKETin
hFileHANDLEin
nNumberOfBytesToWriteDWORDin
nNumberOfBytesPerSendDWORDin
lpOverlappedOVERLAPPED*inoutoptional
lpTransmitBuffersTRANSMIT_FILE_BUFFERS*inoptional
dwReservedDWORDin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL TransmitFile(
    SOCKET hSocket,
    HANDLE hFile,
    DWORD nNumberOfBytesToWrite,
    DWORD nNumberOfBytesPerSend,
    OVERLAPPED* lpOverlapped,   // optional
    TRANSMIT_FILE_BUFFERS* lpTransmitBuffers,   // optional
    DWORD dwReserved
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("MSWSOCK.dll", ExactSpelling = true)]
static extern bool TransmitFile(
    UIntPtr hSocket,   // SOCKET
    IntPtr hFile,   // HANDLE
    uint nNumberOfBytesToWrite,   // DWORD
    uint nNumberOfBytesPerSend,   // DWORD
    IntPtr lpOverlapped,   // OVERLAPPED* optional, in/out
    IntPtr lpTransmitBuffers,   // TRANSMIT_FILE_BUFFERS* optional
    uint dwReserved   // DWORD
);
<DllImport("MSWSOCK.dll", ExactSpelling:=True)>
Public Shared Function TransmitFile(
    hSocket As UIntPtr,   ' SOCKET
    hFile As IntPtr,   ' HANDLE
    nNumberOfBytesToWrite As UInteger,   ' DWORD
    nNumberOfBytesPerSend As UInteger,   ' DWORD
    lpOverlapped As IntPtr,   ' OVERLAPPED* optional, in/out
    lpTransmitBuffers As IntPtr,   ' TRANSMIT_FILE_BUFFERS* optional
    dwReserved As UInteger   ' DWORD
) As Boolean
End Function
' hSocket : SOCKET
' hFile : HANDLE
' nNumberOfBytesToWrite : DWORD
' nNumberOfBytesPerSend : DWORD
' lpOverlapped : OVERLAPPED* optional, in/out
' lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional
' dwReserved : DWORD
Declare PtrSafe Function TransmitFile Lib "mswsock" ( _
    ByVal hSocket As LongPtr, _
    ByVal hFile As LongPtr, _
    ByVal nNumberOfBytesToWrite As Long, _
    ByVal nNumberOfBytesPerSend As Long, _
    ByVal lpOverlapped As LongPtr, _
    ByVal lpTransmitBuffers As LongPtr, _
    ByVal dwReserved As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

TransmitFile = ctypes.windll.mswsock.TransmitFile
TransmitFile.restype = wintypes.BOOL
TransmitFile.argtypes = [
    ctypes.c_size_t,  # hSocket : SOCKET
    wintypes.HANDLE,  # hFile : HANDLE
    wintypes.DWORD,  # nNumberOfBytesToWrite : DWORD
    wintypes.DWORD,  # nNumberOfBytesPerSend : DWORD
    ctypes.c_void_p,  # lpOverlapped : OVERLAPPED* optional, in/out
    ctypes.c_void_p,  # lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional
    wintypes.DWORD,  # dwReserved : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MSWSOCK.dll')
TransmitFile = Fiddle::Function.new(
  lib['TransmitFile'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hSocket : SOCKET
    Fiddle::TYPE_VOIDP,  # hFile : HANDLE
    -Fiddle::TYPE_INT,  # nNumberOfBytesToWrite : DWORD
    -Fiddle::TYPE_INT,  # nNumberOfBytesPerSend : DWORD
    Fiddle::TYPE_VOIDP,  # lpOverlapped : OVERLAPPED* optional, in/out
    Fiddle::TYPE_VOIDP,  # lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional
    -Fiddle::TYPE_INT,  # dwReserved : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "mswsock")]
extern "system" {
    fn TransmitFile(
        hSocket: usize,  // SOCKET
        hFile: *mut core::ffi::c_void,  // HANDLE
        nNumberOfBytesToWrite: u32,  // DWORD
        nNumberOfBytesPerSend: u32,  // DWORD
        lpOverlapped: *mut OVERLAPPED,  // OVERLAPPED* optional, in/out
        lpTransmitBuffers: *mut TRANSMIT_FILE_BUFFERS,  // TRANSMIT_FILE_BUFFERS* optional
        dwReserved: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("MSWSOCK.dll")]
public static extern bool TransmitFile(UIntPtr hSocket, IntPtr hFile, uint nNumberOfBytesToWrite, uint nNumberOfBytesPerSend, IntPtr lpOverlapped, IntPtr lpTransmitBuffers, uint dwReserved);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_TransmitFile' -Namespace Win32 -PassThru
# $api::TransmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved)
#uselib "MSWSOCK.dll"
#func global TransmitFile "TransmitFile" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; TransmitFile hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, varptr(lpOverlapped), varptr(lpTransmitBuffers), dwReserved   ; 戻り値は stat
; hSocket : SOCKET -> "sptr"
; hFile : HANDLE -> "sptr"
; nNumberOfBytesToWrite : DWORD -> "sptr"
; nNumberOfBytesPerSend : DWORD -> "sptr"
; lpOverlapped : OVERLAPPED* optional, in/out -> "sptr"
; lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional -> "sptr"
; dwReserved : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MSWSOCK.dll"
#cfunc global TransmitFile "TransmitFile" sptr, sptr, int, int, var, var, int
; res = TransmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved)
; hSocket : SOCKET -> "sptr"
; hFile : HANDLE -> "sptr"
; nNumberOfBytesToWrite : DWORD -> "int"
; nNumberOfBytesPerSend : DWORD -> "int"
; lpOverlapped : OVERLAPPED* optional, in/out -> "var"
; lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional -> "var"
; dwReserved : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL TransmitFile(SOCKET hSocket, HANDLE hFile, DWORD nNumberOfBytesToWrite, DWORD nNumberOfBytesPerSend, OVERLAPPED* lpOverlapped, TRANSMIT_FILE_BUFFERS* lpTransmitBuffers, DWORD dwReserved)
#uselib "MSWSOCK.dll"
#cfunc global TransmitFile "TransmitFile" intptr, intptr, int, int, var, var, int
; res = TransmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved)
; hSocket : SOCKET -> "intptr"
; hFile : HANDLE -> "intptr"
; nNumberOfBytesToWrite : DWORD -> "int"
; nNumberOfBytesPerSend : DWORD -> "int"
; lpOverlapped : OVERLAPPED* optional, in/out -> "var"
; lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional -> "var"
; dwReserved : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procTransmitFile = mswsock.NewProc("TransmitFile")
)

// hSocket (SOCKET), hFile (HANDLE), nNumberOfBytesToWrite (DWORD), nNumberOfBytesPerSend (DWORD), lpOverlapped (OVERLAPPED* optional, in/out), lpTransmitBuffers (TRANSMIT_FILE_BUFFERS* optional), dwReserved (DWORD)
r1, _, err := procTransmitFile.Call(
	uintptr(hSocket),
	uintptr(hFile),
	uintptr(nNumberOfBytesToWrite),
	uintptr(nNumberOfBytesPerSend),
	uintptr(lpOverlapped),
	uintptr(lpTransmitBuffers),
	uintptr(dwReserved),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function TransmitFile(
  hSocket: NativeUInt;   // SOCKET
  hFile: THandle;   // HANDLE
  nNumberOfBytesToWrite: DWORD;   // DWORD
  nNumberOfBytesPerSend: DWORD;   // DWORD
  lpOverlapped: Pointer;   // OVERLAPPED* optional, in/out
  lpTransmitBuffers: Pointer;   // TRANSMIT_FILE_BUFFERS* optional
  dwReserved: DWORD   // DWORD
): BOOL; stdcall;
  external 'MSWSOCK.dll' name 'TransmitFile';
result := DllCall("MSWSOCK\TransmitFile"
    , "UPtr", hSocket   ; SOCKET
    , "Ptr", hFile   ; HANDLE
    , "UInt", nNumberOfBytesToWrite   ; DWORD
    , "UInt", nNumberOfBytesPerSend   ; DWORD
    , "Ptr", lpOverlapped   ; OVERLAPPED* optional, in/out
    , "Ptr", lpTransmitBuffers   ; TRANSMIT_FILE_BUFFERS* optional
    , "UInt", dwReserved   ; DWORD
    , "Int")   ; return: BOOL
●TransmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved) = DLL("MSWSOCK.dll", "bool TransmitFile(int, void*, dword, dword, void*, void*, dword)")
# 呼び出し: TransmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved)
# hSocket : SOCKET -> "int"
# hFile : HANDLE -> "void*"
# nNumberOfBytesToWrite : DWORD -> "dword"
# nNumberOfBytesPerSend : DWORD -> "dword"
# lpOverlapped : OVERLAPPED* optional, in/out -> "void*"
# lpTransmitBuffers : TRANSMIT_FILE_BUFFERS* optional -> "void*"
# dwReserved : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。