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.
[DllImport("kernel32.dll", SetLastError=true)]
static extern unsafe int WriteFileGather(IntPtr hFile,
FILE_SEGMENT_ELEMENT* aSegmentArray, int nNumberOfBytesToWrite,
IntPtr lpReserved, System.Threading.NativeOverlapped* lpOverlapped);
static extern unsafe int WriteFile(IntPtr handle, IntPtr buffer,
int numBytesToWrite, IntPtr numBytesWritten, NativeOverlapped* lpOverlapped);
User-Defined Types:
[StructLayout(LayoutKind.Explicit, Size = 8)]
internal struct FILE_SEGMENT_ELEMENT
{
[FieldOffset(0)]
public IntPtr Buffer;
[FieldOffset(0)]
public UInt64 Alignment;
}
or
Notes:
The documentation for the Scatter/Gather functions states that the buffer addresses used must be page aligned (not just storage aligned). Indeed, passing a managed allocation, such as a byte array, causes error 87 (Invalid Parameter). This function is asynchronous only so an use the AsyncResult and Overlapped pattern established by the FileStream.
for Hooking into a process and see what an application want's to write using windows APIs, remember to clean up the System.Text.StringBuilder lpBuffer by deleting the junk data that remains concatenated.
You can do it using:
string text = lpBuffer.ToString().Substring(0, (int)nNumberOfBytesToWrite).Replace("\r\n", " ")
Tips & Tricks:
It is recommended that VirtualAlloc be used to allocate an unmanaged block of memory for use with this function. The array of file segments can be effeciently constructed without heap allocation using stackalloc. You will need to get the system's page size through a call to GetSystemInfo.
...and this will remove the junk data from lpBuffer
Sample Code:
// Prepares the array of file segments given an IntPtr[] ABuffers (only uses ACount of the buffers so that these arrays can be pooled)
// The size of each buffer (ASize) must be a multiple of the system's page size (get using GetSystemInfo)
int LPerBufferPageCount = ASize / FPageSize;
FILE_SEGMENT_ELEMENT* LElements = stackalloc FILE_SEGMENT_ELEMENT[(ACount * LPerBufferPageCount) + 1];
for (int LElementIndex = 0; LElementIndex < ACount; LElementIndex++)
{
for (int LPageIndex = 0; LPageIndex < LPerBufferPageCount; LPageIndex++)
LElements[(LElementIndex * LPerBufferPageCount) + LPageIndex].Buffer =
(IntPtr)((uint)ABuffers[LElementIndex] + (LPageIndex * FPageSize));
}
LElements[(ACount * LPerBufferPageCount)].Buffer = IntPtr.Zero;
Alternative Managed API:
No managed API as of the 1.1 Framework.
VB Signature:
<DllImport("kernel32.dll", SetlastError:=True)> _
Private Shared Function WriteFile(ByVal hFile As IntPtr, ByVal Buffer As IntPtr,
ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer,
ByRef lpOverlapped As OVERLAPPED) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
"File" = The SafeFileHandle to the file to write to. The file must have at least write-level access.
"Buffer" = The variable, array, or string holding the data to write to the file.
"NumberOfBytesToWrite" = The number of bytes of data to write to the file.
"NumberOfBytesWritten" = Receives the number of bytes of data actually written to the file.
"Overlapped" = If the file is asynchronous (overlapped), this is an threading.NativeOverlappedStructure specifying where to begin writing at. If the file is synchronous (not overlapped), this must be 0.
<DllImport("kernel32.dll", SetlastError:=True)> Friend Shared Function WriteFile( _
ByVal File As SafeFileHandle, _
ByVal Buffer As System.Text.StringBuilder, _
ByVal NumberOfBytesToWrite As Integer, _
ByRef NumberOfBytesWritten As Integer, _
ByRef Overlapped As System.Threading.NativeOverlapped) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
or
<DllImport("kernel32.dll")> _
Public Shared Function WriteFile( _
ByVal hFile As IntPtr, _
ByVal Buffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
ByRef lpOverlapped As OVERLAPPED) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
User-Defined Types:
None.
Notes:
The documentation states that if using unbuffered IO, the memory must be "storage aligned" (aligned to the sector size of the storage device). Either this is not enforced, or managed allocations (specifically byte arrays) are automatically storage aligned because there seems to be no problem using it. Note that storage aligned and page aligned are not the same, and managed allocations are not in general page aligned (required for WriteFileScatter).
In fact the buffer must be aligned to the granularity of the SCSI/IDE adapter's DMA alignment (PDEVICE_OBJECT->AlignmentRequirement). The MSDN documentation is written for the lamers and probably it seemed safe to restrict them using the sector size (which is >= 512 byte, while most modern SCSI cards use <=4 byte requirement).
For more information on unbuffered IO in .NET see:
Warning! GetOverlappedResult writes to the address of the buffer specified in the ORIGINAL OPERATION (ie/ ReadFile or WriteFile). .NET may move the address of the buffer before GetOverlappedResult returns, resulting in a buffer overflow. Use AllocHGlobal and FreeHGlobal or otherwise ensure the buffer is pinned between the two calls.
If you are passing a System.Threading.NativeOverlapped struct in (for lpOverlapped) then there's no need to also call SetFilePointer or SetFilePointerEx. The offset in the OVERLAPPED structure trumps wherever the file position was previouly set to.
Sample Code:
Public Sub Write(ByVal Packet As stArcnetPacket, ByVal DataLength As Int16)
Dim dwErrorCode As FarcConstants
Dim dwBytesWritten As Int32
Dim ip As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Packet))
Marshal.StructureToPtr(Packet, ip, True)
If WriteFile(hDriver, ip, DataLength + 6, dwBytesWritten, Nothing) Then
Marshal.FreeHGlobal(ip)
Else
dwErrorCode = Marshal.GetLastWin32Error
Marshal.FreeHGlobal(ip)
Throw New System.IO.IOException("Write fails. Errorcode: " & dwErrorCode.ToString, _
New System.ComponentModel.Win32Exception(dwErrorCode))
End If
End Sub
3/16/2007 7:59:07 AM - nathan@alphora.com-65.37.109.158
TODO - a short description
3/16/2007 7:52:17 AM - anonymous
Click to read this page
4/6/2008 7:23:14 AM - anonymous
Click to read this page
4/6/2008 7:23:14 AM - anonymous
TODO - a short description
3/16/2007 8:11:30 AM - -61.11.98.124
The VirtualAllocEx API
10/1/2015 7:31:33 AM - 76.169.70.95
The ReadFileScatter API
3/16/2007 7:57:43 AM - anonymous
The ReadFileScatter API
3/16/2007 7:57:44 AM - -203.199.198.252
The GetOverlappedResult API
10/21/2011 2:56:38 PM - -60.29.18.98
The ReadFileScatter API
3/16/2007 7:57:44 AM - -203.199.198.252
The GetOverlappedResult API
10/21/2011 2:56:38 PM - -60.29.18.98
Click to read this page
12/2/2010 4:36:50 PM - -81.247.25.174
The SetFilePointerEx API
9/18/2012 1:07:38 PM - -203.59.226.166
The SetFilePointerEx API
12/17/2008 7:21:05 PM - -219.111.155.239
The ReadFileScatter API
3/16/2007 7:57:44 AM - -203.199.198.252
The WriteFileGather API
3/16/2007 7:59:07 AM - nathan@alphora.com-65.37.109.158
The ReadFileScatter API
3/16/2007 7:57:43 AM - anonymous
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).