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.
//4
[DllImport(@"kernel32.dll", SetLastError = true)]
static extern unsafe bool ReadFile(
SafeFileHandle hFile, // handle to file
byte* pBuffer, // data buffer, should be fixed
int NumberOfBytesToRead, // number of bytes to read
IntPtr pNumberOfBytesRead, // number of bytes read, provide IntPtr.Zero here
NativeOverlapped *lpOverlapped // should be fixed, if not IntPtr.Zero
);
VB Signature:
<DllImport("kernel32.dll", SetlastError:=True)> _
Private Shared Function ReadFile(ByVal hFile As IntPtr, ByVal Buffer As IntPtr, _
ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
or
<DllImport("kernel32.dll")> Friend Shared Function ReadFile( _
ByVal File As SafeFileHandle, _
ByVal Buffer As System.Text.StringBuilder, _
ByVal NumberOfBytesToRead As Integer, _
ByRef NumberOfBytesRead As Integer, _
ByRef Overlapped As System.Threading.NativeOverlapped) As SafeFileHandle
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 ReadFileScatter).
For more information on unbuffered IO in .NET see:
Regarding the above note, my experience shows that failing to provide a storage-aligned buffer causes ReadFile to perform all IO synchronously. I.e. it always blocks until the operation is complete and never returns ERROR_IO_PENDING.
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. The same applies to the overlapped structure.
Public Function Read() As stArcnetPacket
Dim dwErrorCode As FarcConstants
Dim erg As stArcnetPacket
Dim dwBytesReceived As Int32
Dim ip As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(erg))
If ReadFile(hDriver, ip, Marshal.SizeOf(erg), dwBytesReceived, Nothing) Then
erg = Marshal.PtrToStructure(ip, erg.GetType)
ReDim Preserve erg.Data(dwBytesReceived - 6)
Marshal.FreeHGlobal(ip)
Else
dwErrorCode = Marshal.GetLastWin32Error
Marshal.FreeHGlobal(ip)
Throw New System.IO.IOException("Read fails. Errorcode: " & _
dwErrorCode.ToString, New System.ComponentModel.Win32Exception(dwErrorCode))
End If
Return erg
End Function
USED W/ SIGNATURE #4
// This is taken from the USBSharp.cs class
public unsafe byte[] CT_ReadFile(int InputReportByteLength)
{
int BytesRead =0;
byte[] BufBytes = new byte[InputReportByteLength];
if (ReadFile(HidHandle, BufBytes, InputReportByteLength, ref BytesRead, null))
{
byte[] OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, OutBytes, BytesRead);
return OutBytes;
}
else
{
return null;
}
}
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
Click to read this page
3/31/2016 3:44:13 AM - oxewadrgwmo-85.235.206.106
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).