Win32 API 日本語リファレンス
ホームStorage.FileSystem › BackupWrite

BackupWrite

関数
バックアップデータをファイルへ逐次書き込んで復元する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL BackupWrite(
    HANDLE hFile,
    BYTE* lpBuffer,
    DWORD nNumberOfBytesToWrite,
    DWORD* lpNumberOfBytesWritten,
    BOOL bAbort,
    BOOL bProcessSecurity,
    void** lpContext
);

パラメーター

名前方向説明
hFileHANDLEin

復元するファイルまたはディレクトリのハンドルです。このハンドルを取得するには、CreateFile 関数を呼び出します。ファイルハンドルが ACCESS_SYSTEM_SECURITY アクセス権で作成されていない限り、SACL は復元されません。整合性 ACE を正しく復元するには、ファイルハンドルが WRITE_OWNER アクセス権でも作成されている必要があります。詳細については、ファイルのセキュリティとアクセス権 を参照してください。

ハンドルは同期(非オーバーラップ)である必要があります。つまり、CreateFile を呼び出す際に FILE_FLAG_OVERLAPPED フラグを設定してはなりません。この関数は受け取ったハンドルが同期であるかどうかを検証しないため、同期ハンドルに対してエラーコードを返しませんが、非同期(オーバーラップ)ハンドルで呼び出すと、デバッグが非常に困難な微妙なエラーを引き起こす可能性があります。

CreateFileFILE_FLAG_NO_BUFFERING フラグで呼び出された場合、BackupWrite 関数は失敗することがあります。この場合、 GetLastError 関数は ERROR_INVALID_PARAMETER の値を返します。

lpBufferBYTE*in関数がデータを書き出すバッファへのポインターです。
nNumberOfBytesToWriteDWORDinバッファのサイズ(バイト単位)です。バッファサイズは WIN32_STREAM_ID 構造体のサイズより大きくなければなりません。
lpNumberOfBytesWrittenDWORD*out書き込まれたバイト数を受け取る変数へのポインターです。
bAbortBOOLinそのハンドルに対する BackupWrite の使用を終了したかどうかを示します。 ファイルの復元中は、このパラメーターを FALSE に指定します。BackupWrite の使用を終えた後は、 このパラメーターに TRUE を指定し、適切な lpContext を渡して、BackupWrite をもう一度呼び出す必要があります。bAbortTRUE の場合は lpContext を渡す必要があり、他のすべてのパラメーターは無視されます。
bProcessSecurityBOOLin

関数がファイルまたはディレクトリのアクセス制御リスト(ACL)データを復元するかどうかを指定します。

bProcessSecurityTRUE の場合、ファイルまたはディレクトリのハンドルを開く際に WRITE_OWNER および WRITE_DAC アクセスを指定する必要があります。ハンドルがこれらのアクセス権を持たない場合、オペレーティングシステムは ACL データへのアクセスを拒否し、ACL データの復元は行われません。

lpContextvoid**inout

復元操作中のコンテキスト情報を維持するために BackupWrite が使用する内部データ構造へのポインターを受け取る変数へのポインターです。

指定したファイルまたはディレクトリに対する最初の BackupWrite の呼び出しの前に、lpContext が指す変数を NULL に設定する必要があります。関数はデータ構造のためのメモリを割り当て、その構造を指すように変数を設定します。BackupWrite の呼び出しの間に lpContext またはそれが指す変数を変更してはなりません。

データ構造で使用されているメモリを解放するには、復元操作が完了したときに bAbort パラメーターを TRUE に設定して BackupWrite を呼び出します。

戻り値の型: BOOL

公式ドキュメント

BackupRead を使用してバックアップしたファイルまたはディレクトリを復元します。

戻り値

関数が成功した場合、戻り値は 0 以外です。

関数が失敗した場合、戻り値は 0 であり、I/O エラーが発生したことを示します。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

この関数は、 暗号化ファイルシステム(Encrypted File System) で暗号化されたファイルの復元には使用しません。その目的には WriteEncryptedFileRaw を使用してください。

バックアップメディアから読み取るデータは、 WIN32_STREAM_ID 構造体で区切られたサブストリームでなければなりません。

BACKUP_LINK ストリームタイプを使用すると、ハードリンクを持つファイルを復元できます。

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

各言語での呼び出し定義

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

BOOL BackupWrite(
    HANDLE hFile,
    BYTE* lpBuffer,
    DWORD nNumberOfBytesToWrite,
    DWORD* lpNumberOfBytesWritten,
    BOOL bAbort,
    BOOL bProcessSecurity,
    void** lpContext
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool BackupWrite(
    IntPtr hFile,   // HANDLE
    IntPtr lpBuffer,   // BYTE*
    uint nNumberOfBytesToWrite,   // DWORD
    out uint lpNumberOfBytesWritten,   // DWORD* out
    bool bAbort,   // BOOL
    bool bProcessSecurity,   // BOOL
    IntPtr lpContext   // void** in/out
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function BackupWrite(
    hFile As IntPtr,   ' HANDLE
    lpBuffer As IntPtr,   ' BYTE*
    nNumberOfBytesToWrite As UInteger,   ' DWORD
    <Out> ByRef lpNumberOfBytesWritten As UInteger,   ' DWORD* out
    bAbort As Boolean,   ' BOOL
    bProcessSecurity As Boolean,   ' BOOL
    lpContext As IntPtr   ' void** in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hFile : HANDLE
' lpBuffer : BYTE*
' nNumberOfBytesToWrite : DWORD
' lpNumberOfBytesWritten : DWORD* out
' bAbort : BOOL
' bProcessSecurity : BOOL
' lpContext : void** in/out
Declare PtrSafe Function BackupWrite Lib "kernel32" ( _
    ByVal hFile As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal nNumberOfBytesToWrite As Long, _
    ByRef lpNumberOfBytesWritten As Long, _
    ByVal bAbort As Long, _
    ByVal bProcessSecurity As Long, _
    ByVal lpContext As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

BackupWrite = ctypes.windll.kernel32.BackupWrite
BackupWrite.restype = wintypes.BOOL
BackupWrite.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    ctypes.POINTER(ctypes.c_ubyte),  # lpBuffer : BYTE*
    wintypes.DWORD,  # nNumberOfBytesToWrite : DWORD
    ctypes.POINTER(wintypes.DWORD),  # lpNumberOfBytesWritten : DWORD* out
    wintypes.BOOL,  # bAbort : BOOL
    wintypes.BOOL,  # bProcessSecurity : BOOL
    ctypes.c_void_p,  # lpContext : void** in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procBackupWrite = kernel32.NewProc("BackupWrite")
)

// hFile (HANDLE), lpBuffer (BYTE*), nNumberOfBytesToWrite (DWORD), lpNumberOfBytesWritten (DWORD* out), bAbort (BOOL), bProcessSecurity (BOOL), lpContext (void** in/out)
r1, _, err := procBackupWrite.Call(
	uintptr(hFile),
	uintptr(lpBuffer),
	uintptr(nNumberOfBytesToWrite),
	uintptr(lpNumberOfBytesWritten),
	uintptr(bAbort),
	uintptr(bProcessSecurity),
	uintptr(lpContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function BackupWrite(
  hFile: THandle;   // HANDLE
  lpBuffer: Pointer;   // BYTE*
  nNumberOfBytesToWrite: DWORD;   // DWORD
  lpNumberOfBytesWritten: Pointer;   // DWORD* out
  bAbort: BOOL;   // BOOL
  bProcessSecurity: BOOL;   // BOOL
  lpContext: Pointer   // void** in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'BackupWrite';
result := DllCall("KERNEL32\BackupWrite"
    , "Ptr", hFile   ; HANDLE
    , "Ptr", lpBuffer   ; BYTE*
    , "UInt", nNumberOfBytesToWrite   ; DWORD
    , "Ptr", lpNumberOfBytesWritten   ; DWORD* out
    , "Int", bAbort   ; BOOL
    , "Int", bProcessSecurity   ; BOOL
    , "Ptr", lpContext   ; void** in/out
    , "Int")   ; return: BOOL
●BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext) = DLL("KERNEL32.dll", "bool BackupWrite(void*, void*, dword, void*, bool, bool, void*)")
# 呼び出し: BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext)
# hFile : HANDLE -> "void*"
# lpBuffer : BYTE* -> "void*"
# nNumberOfBytesToWrite : DWORD -> "dword"
# lpNumberOfBytesWritten : DWORD* out -> "void*"
# bAbort : BOOL -> "bool"
# bProcessSecurity : BOOL -> "bool"
# lpContext : void** in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn BackupWrite(
    hFile: ?*anyopaque, // HANDLE
    lpBuffer: [*c]u8, // BYTE*
    nNumberOfBytesToWrite: u32, // DWORD
    lpNumberOfBytesWritten: [*c]u32, // DWORD* out
    bAbort: i32, // BOOL
    bProcessSecurity: i32, // BOOL
    lpContext: ?*anyopaque // void** in/out
) callconv(std.os.windows.WINAPI) i32;
proc BackupWrite(
    hFile: pointer,  # HANDLE
    lpBuffer: ptr uint8,  # BYTE*
    nNumberOfBytesToWrite: uint32,  # DWORD
    lpNumberOfBytesWritten: ptr uint32,  # DWORD* out
    bAbort: int32,  # BOOL
    bProcessSecurity: int32,  # BOOL
    lpContext: pointer  # void** in/out
): int32 {.importc: "BackupWrite", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int BackupWrite(
    void* hFile,   // HANDLE
    ubyte* lpBuffer,   // BYTE*
    uint nNumberOfBytesToWrite,   // DWORD
    uint* lpNumberOfBytesWritten,   // DWORD* out
    int bAbort,   // BOOL
    int bProcessSecurity,   // BOOL
    void** lpContext   // void** in/out
);
ccall((:BackupWrite, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{UInt8}, UInt32, Ptr{UInt32}, Int32, Int32, Ptr{Cvoid}),
      hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext)
# hFile : HANDLE -> Ptr{Cvoid}
# lpBuffer : BYTE* -> Ptr{UInt8}
# nNumberOfBytesToWrite : DWORD -> UInt32
# lpNumberOfBytesWritten : DWORD* out -> Ptr{UInt32}
# bAbort : BOOL -> Int32
# bProcessSecurity : BOOL -> Int32
# lpContext : void** in/out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t BackupWrite(
    void* hFile,
    uint8_t* lpBuffer,
    uint32_t nNumberOfBytesToWrite,
    uint32_t* lpNumberOfBytesWritten,
    int32_t bAbort,
    int32_t bProcessSecurity,
    void** lpContext);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext)
-- hFile : HANDLE
-- lpBuffer : BYTE*
-- nNumberOfBytesToWrite : DWORD
-- lpNumberOfBytesWritten : DWORD* out
-- bAbort : BOOL
-- bProcessSecurity : BOOL
-- lpContext : void** in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const BackupWrite = lib.func('__stdcall', 'BackupWrite', 'int32_t', ['void *', 'uint8_t *', 'uint32_t', 'uint32_t *', 'int32_t', 'int32_t', 'void *']);
// BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext)
// hFile : HANDLE -> 'void *'
// lpBuffer : BYTE* -> 'uint8_t *'
// nNumberOfBytesToWrite : DWORD -> 'uint32_t'
// lpNumberOfBytesWritten : DWORD* out -> 'uint32_t *'
// bAbort : BOOL -> 'int32_t'
// bProcessSecurity : BOOL -> 'int32_t'
// lpContext : void** in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  BackupWrite: { parameters: ["pointer", "pointer", "u32", "pointer", "i32", "i32", "pointer"], result: "i32" },
});
// lib.symbols.BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext)
// hFile : HANDLE -> "pointer"
// lpBuffer : BYTE* -> "pointer"
// nNumberOfBytesToWrite : DWORD -> "u32"
// lpNumberOfBytesWritten : DWORD* out -> "pointer"
// bAbort : BOOL -> "i32"
// bProcessSecurity : BOOL -> "i32"
// lpContext : void** in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t BackupWrite(
    void* hFile,
    uint8_t* lpBuffer,
    uint32_t nNumberOfBytesToWrite,
    uint32_t* lpNumberOfBytesWritten,
    int32_t bAbort,
    int32_t bProcessSecurity,
    void** lpContext);
C, "KERNEL32.dll");
// $ffi->BackupWrite(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, bAbort, bProcessSecurity, lpContext);
// hFile : HANDLE
// lpBuffer : BYTE*
// nNumberOfBytesToWrite : DWORD
// lpNumberOfBytesWritten : DWORD* out
// bAbort : BOOL
// bProcessSecurity : BOOL
// lpContext : void** in/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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean BackupWrite(
        Pointer hFile,   // HANDLE
        byte[] lpBuffer,   // BYTE*
        int nNumberOfBytesToWrite,   // DWORD
        IntByReference lpNumberOfBytesWritten,   // DWORD* out
        boolean bAbort,   // BOOL
        boolean bProcessSecurity,   // BOOL
        Pointer lpContext   // void** in/out
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun BackupWrite = BackupWrite(
    hFile : Void*,   # HANDLE
    lpBuffer : UInt8*,   # BYTE*
    nNumberOfBytesToWrite : UInt32,   # DWORD
    lpNumberOfBytesWritten : UInt32*,   # DWORD* out
    bAbort : Int32,   # BOOL
    bProcessSecurity : Int32,   # BOOL
    lpContext : Void**   # void** in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef BackupWriteNative = Int32 Function(Pointer<Void>, Pointer<Uint8>, Uint32, Pointer<Uint32>, Int32, Int32, Pointer<Void>);
typedef BackupWriteDart = int Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Uint32>, int, int, Pointer<Void>);
final BackupWrite = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<BackupWriteNative, BackupWriteDart>('BackupWrite');
// hFile : HANDLE -> Pointer<Void>
// lpBuffer : BYTE* -> Pointer<Uint8>
// nNumberOfBytesToWrite : DWORD -> Uint32
// lpNumberOfBytesWritten : DWORD* out -> Pointer<Uint32>
// bAbort : BOOL -> Int32
// bProcessSecurity : BOOL -> Int32
// lpContext : void** in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function BackupWrite(
  hFile: THandle;   // HANDLE
  lpBuffer: Pointer;   // BYTE*
  nNumberOfBytesToWrite: DWORD;   // DWORD
  lpNumberOfBytesWritten: Pointer;   // DWORD* out
  bAbort: BOOL;   // BOOL
  bProcessSecurity: BOOL;   // BOOL
  lpContext: Pointer   // void** in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'BackupWrite';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "BackupWrite"
  c_BackupWrite :: Ptr () -> Ptr Word8 -> Word32 -> Ptr Word32 -> CInt -> CInt -> Ptr () -> IO CInt
-- hFile : HANDLE -> Ptr ()
-- lpBuffer : BYTE* -> Ptr Word8
-- nNumberOfBytesToWrite : DWORD -> Word32
-- lpNumberOfBytesWritten : DWORD* out -> Ptr Word32
-- bAbort : BOOL -> CInt
-- bProcessSecurity : BOOL -> CInt
-- lpContext : void** in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let backupwrite =
  foreign "BackupWrite"
    ((ptr void) @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint32_t) @-> int32_t @-> int32_t @-> (ptr void) @-> returning int32_t)
(* hFile : HANDLE -> (ptr void) *)
(* lpBuffer : BYTE* -> (ptr uint8_t) *)
(* nNumberOfBytesToWrite : DWORD -> uint32_t *)
(* lpNumberOfBytesWritten : DWORD* out -> (ptr uint32_t) *)
(* bAbort : BOOL -> int32_t *)
(* bProcessSecurity : BOOL -> int32_t *)
(* lpContext : void** in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("BackupWrite" backup-write :convention :stdcall) :int32
  (h-file :pointer)   ; HANDLE
  (lp-buffer :pointer)   ; BYTE*
  (n-number-of-bytes-to-write :uint32)   ; DWORD
  (lp-number-of-bytes-written :pointer)   ; DWORD* out
  (b-abort :int32)   ; BOOL
  (b-process-security :int32)   ; BOOL
  (lp-context :pointer))   ; void** in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $BackupWrite = Win32::API::More->new('KERNEL32',
    'BOOL BackupWrite(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPVOID lpNumberOfBytesWritten, BOOL bAbort, BOOL bProcessSecurity, LPVOID lpContext)');
# my $ret = $BackupWrite->Call($hFile, $lpBuffer, $nNumberOfBytesToWrite, $lpNumberOfBytesWritten, $bAbort, $bProcessSecurity, $lpContext);
# hFile : HANDLE -> HANDLE
# lpBuffer : BYTE* -> LPVOID
# nNumberOfBytesToWrite : DWORD -> DWORD
# lpNumberOfBytesWritten : DWORD* out -> LPVOID
# bAbort : BOOL -> BOOL
# bProcessSecurity : BOOL -> BOOL
# lpContext : void** in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目