使用ReadFile API函数时出现问题,为什么它不起作用?
问题描述:
Hellow ...我已阅读 http://buiba上的帖子.blogspot.com/2009/06/using-winapi-createfile-readfile.html [
Hellow... I''ve read the post at http://buiba.blogspot.com/2009/06/using-winapi-createfile-readfile.html[^] and I''m trying to read some bytes from my flash memory.. why it doesn''t work.. what am i doing wrong?, it gives no errors just my buffer after ReadFile is empty my code is: a lot of thanks.........in advance.
SafeFileHandle ptrFile = CreateFile("\\\\.\\u:", DesiredAccess.GENERIC_READ | DesiredAccess.GENERIC_WRITE, ShareMode.FILE_SHARE_READ_AND_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
uint size = GetFileSize(ptrFile,IntPtr.Zero);
byte[] buffer = new byte[512];
uint readed = 0;
uint a = SetFilePointer(ptrFile, 0, IntPtr.Zero, 0);
ReadFile(ptrFile, buffer,512 , out readed, IntPtr.Zero);
/* ---------------------------------------------------------
* WINAPI STUFF
* ------------------------------------------------------ */
private void ThrowLastWin32Err()
{
Marshal.ThrowExceptionForHR(
Marshal.GetHRForLastWin32Error());
}
[Flags]
public enum DesiredAccess : uint
{
GENERIC_READ = 0x80000000,
GENERIC_WRITE =0x40000000
}
[Flags]
public enum ShareMode : uint
{
FILE_SHARE_NONE = 0x0,
FILE_SHARE_READ = 0x1,
FILE_SHARE_WRITE = 0x2,
FILE_SHARE_READ_AND_WRITE = 0x3,
FILE_SHARE_DELETE = 0x4,
}
public enum MoveMethod : uint
{
FILE_BEGIN = 0,
FILE_CURRENT = 1,
FILE_END = 2
}
public enum CreationDisposition : uint
{
CREATE_NEW = 1,
CREATE_ALWAYS = 2,
OPEN_EXISTING = 3,
OPEN_ALWAYS = 4,
TRUNCATE_EXSTING = 5
}
[Flags]
public enum FlagsAndAttributes : uint
{
FILE_ATTRIBUTES_ARCHIVE = 0x20,
FILE_ATTRIBUTE_HIDDEN = 0x2,
FILE_ATTRIBUTE_NORMAL = 0x80,
FILE_ATTRIBUTE_OFFLINE = 0x1000,
FILE_ATTRIBUTE_READONLY = 0x1,
FILE_ATTRIBUTE_SYSTEM = 0x4,
FILE_ATTRIBUTE_TEMPORARY = 0x100,
FILE_FLAG_WRITE_THROUGH = 0x80000000,
FILE_FLAG_OVERLAPPED = 0x40000000,
FILE_FLAG_NO_BUFFERING = 0x20000000,
FILE_FLAG_RANDOM_ACCESS = 0x10000000,
FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000,
FILE_FLAG_DELETE_ON = 0x4000000,
FILE_FLAG_POSIX_SEMANTICS = 0x1000000,
FILE_FLAG_OPEN_REPARSE_POINT = 0x200000,
FILE_FLAG_OPEN_NO_CALL = 0x100000
}
public const uint INVALID_HANDLE_VALUE = 0xFFFFFFFF;
public const uint INVALID_SET_FILE_POINTER = 0xFFFFFFFF;
// Use interop to call the CreateFile function.
// For more information about CreateFile,
// see the unmanaged MSDN reference library.
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern SafeFileHandle CreateFile(
string lpFileName,
DesiredAccess dwDesiredAccess,
ShareMode dwShareMode,
IntPtr lpSecurityAttributes,
CreationDisposition dwCreationDisposition,
FlagsAndAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32", SetLastError = true)]
internal static extern bool ReadFile(
SafeFileHandle hFile,
Byte[] aBuffer,
UInt32 cbToRead,
out UInt32 cbThatWereRead,
IntPtr pOverlapped);
答
没有错误
但是您的代码并未测试错误,只是假设每次调用均成功.您应该检查每个调用的返回值,以检查是否成功.例如,您确定从CreateFile()
获得了有效的句柄吗?
it gives no errors
And yet your code is not testing for errors, but just assuming that every call succeeds. You should check the return values from each call to check whether it succeeded or not. For example, are you sure that you got a valid handle fromCreateFile()
?
不幸的是,这并不容易.如果单独使用每个字节,则可以轻松查看其值,但是如果将一组字节作为单个数字,则由于biglittle-endian格式,因此必须切换顺序架构中的单词和双词的集合.例如
Unfortunately this is not as easy as it could be; if you take each byte by itself you can easily see what value it has, but if you take a group of bytes as a single number then you have to switch the order owing to thebiglittle-endian format of words and double words in the intel architecture. For example
BYTE array[6] = { 12, 31, 44, 125, 7, 99 };
// is equivalent to
BYTE array[6] = { 0x0C, 0x1F, 0x2C, 0x7D, 0x07, 0x63 };
// so what is the decimal value of the last four bytes as an integer
// to make integer we need to reorder thus
int array2to5 = 0x63077D2C;
// which is the same as
int array2to5 = 1661435180;
// we can make this easier by using the printf() function like this:
printf("value = %d\n", *(int*)(array+2))
不好意思让我的大头和小头走错了路.
Apologies for getting my big and little ends the wrong way round.