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

SetFilePointer

関数
ファイルの読み書き位置を移動する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD SetFilePointer(
    HANDLE hFile,
    INT lDistanceToMove,
    INT* lpDistanceToMoveHigh,   // optional
    SET_FILE_POINTER_MOVE_METHOD dwMoveMethod
);

パラメーター

名前方向
hFileHANDLEin
lDistanceToMoveINTin
lpDistanceToMoveHighINT*inoutoptional
dwMoveMethodSET_FILE_POINTER_MOVE_METHODin

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD SetFilePointer(
    HANDLE hFile,
    INT lDistanceToMove,
    INT* lpDistanceToMoveHigh,   // optional
    SET_FILE_POINTER_MOVE_METHOD dwMoveMethod
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern uint SetFilePointer(
    IntPtr hFile,   // HANDLE
    int lDistanceToMove,   // INT
    IntPtr lpDistanceToMoveHigh,   // INT* optional, in/out
    uint dwMoveMethod   // SET_FILE_POINTER_MOVE_METHOD
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetFilePointer(
    hFile As IntPtr,   ' HANDLE
    lDistanceToMove As Integer,   ' INT
    lpDistanceToMoveHigh As IntPtr,   ' INT* optional, in/out
    dwMoveMethod As UInteger   ' SET_FILE_POINTER_MOVE_METHOD
) As UInteger
End Function
' hFile : HANDLE
' lDistanceToMove : INT
' lpDistanceToMoveHigh : INT* optional, in/out
' dwMoveMethod : SET_FILE_POINTER_MOVE_METHOD
Declare PtrSafe Function SetFilePointer Lib "kernel32" ( _
    ByVal hFile As LongPtr, _
    ByVal lDistanceToMove As Long, _
    ByVal lpDistanceToMoveHigh As LongPtr, _
    ByVal dwMoveMethod As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetFilePointer = ctypes.windll.kernel32.SetFilePointer
SetFilePointer.restype = wintypes.DWORD
SetFilePointer.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    ctypes.c_int,  # lDistanceToMove : INT
    ctypes.POINTER(ctypes.c_int),  # lpDistanceToMoveHigh : INT* optional, in/out
    wintypes.DWORD,  # dwMoveMethod : SET_FILE_POINTER_MOVE_METHOD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetFilePointer = kernel32.NewProc("SetFilePointer")
)

// hFile (HANDLE), lDistanceToMove (INT), lpDistanceToMoveHigh (INT* optional, in/out), dwMoveMethod (SET_FILE_POINTER_MOVE_METHOD)
r1, _, err := procSetFilePointer.Call(
	uintptr(hFile),
	uintptr(lDistanceToMove),
	uintptr(lpDistanceToMoveHigh),
	uintptr(dwMoveMethod),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SetFilePointer(
  hFile: THandle;   // HANDLE
  lDistanceToMove: Integer;   // INT
  lpDistanceToMoveHigh: Pointer;   // INT* optional, in/out
  dwMoveMethod: DWORD   // SET_FILE_POINTER_MOVE_METHOD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'SetFilePointer';
result := DllCall("KERNEL32\SetFilePointer"
    , "Ptr", hFile   ; HANDLE
    , "Int", lDistanceToMove   ; INT
    , "Ptr", lpDistanceToMoveHigh   ; INT* optional, in/out
    , "UInt", dwMoveMethod   ; SET_FILE_POINTER_MOVE_METHOD
    , "UInt")   ; return: DWORD
●SetFilePointer(hFile, lDistanceToMove, lpDistanceToMoveHigh, dwMoveMethod) = DLL("KERNEL32.dll", "dword SetFilePointer(void*, int, void*, dword)")
# 呼び出し: SetFilePointer(hFile, lDistanceToMove, lpDistanceToMoveHigh, dwMoveMethod)
# hFile : HANDLE -> "void*"
# lDistanceToMove : INT -> "int"
# lpDistanceToMoveHigh : INT* optional, in/out -> "void*"
# dwMoveMethod : SET_FILE_POINTER_MOVE_METHOD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。