Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
SetFilePointer (kernel32)
.
C# Signature:
[DllImport("kernel32.dll")]
static extern bool SetFilePointerEx(IntPtr hFile, long liDistanceToMove,
IntPtr lpNewFilePointer, uint dwMoveMethod);
public enum EMoveMethod : uint
{
Begin = 0,
Current = 1,
End = 2
}
C# Signature:
[DllImport("kernel32.dll")]
public static extern bool SetFilePointerEx(
SafeFileHandle hFile, long liDistanceToMove,
out long lpNewFilePointer, uint dwMoveMethod);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern unsafe uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] int* lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] out int lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
User-Defined Types:
None.
VB Signature:
Public Enum EMoveMethod : uint
FILE_BEGIN = 0
FILE_CURRENT = 1
FILE_END = 2
End Enum
Notes:
None.
<DllImport("kernel32.dll", SetLastError:=True)> _
Shared Function SetFilePointer( _
ByVal hFile As IntPtr, _
ByVal lDistanceToMove As Integer, _
ByRef lpDistanceToMoveHigh As IntPtr, _
ByVal dwMoveMethod As EMoveMethod) _
As System.UInt32
End Function
Tips & Tricks:
The third argument (lpNewFilePointer) is of type LARGE_INTEGER, which has a size of 64 bits. For this reason use a UInt64 for this argument. (See sample below)
Public Const INVALID_SET_FILE_POINTER As Integer = -1
Sample Code:
UInt64 n = 0; //it is important to use UInt64, because the 3rd argument is of type PLARGE_INTEGER, where LARGE_INTEGER has a size of 64 bits.
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SetFilePointer(IntPtr handle, int lDistanceToMove, out int lpDistanceToMoveHigh, uint dwMoveMethod);
Alternative Managed API:
Do you know one? Please contribute it!
public long Seek(IntPtr handle, long offset, SeekOrigin origin)
{
uint moveMethod = 0;
switch (origin)
{
case SeekOrigin.Begin:
moveMethod = 0;
break;
case SeekOrigin.Current:
moveMethod = 1;
break;
case SeekOrigin.End:
moveMethod = 2;
break;
}
int lo = (int)(offset & 0xffffffff);
int hi = (int)(offset >> 32);
lo = SetFilePointer(handle, lo, out hi, moveMethod);
if (lo == -1)
{
if (GetLastError() != 0)
{
throw new Exception("INVALID_SET_FILE_POINTER");
}
}
return (((long)hi << 32) | (uint)lo);
}
Alternative Managed API:
Do you know one? Please contribute it!
The SetFilePointerEx API
12/17/2008 7:21:05 PM - -219.111.155.239
The SetFilePointerEx API
9/18/2012 1:07:38 PM - -203.59.226.166
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).