问题描述
你好
我已经制作了一个更复杂的例程的骨架来演示该效果.由于某种原因,文件指针没有移动,我一次又一次地读取同一代码块.
I've made a skeleton of a more complex routine to demonstrate the effect. For some reason the file pointer does not move and I am reading the same block of code again and again.
public void DiskReadSimplified( )
{
不安全
{
字符串lpFileName = @" \\.\ PHYSICALDRIVE0 " ;;
this.debugPrint = true;
Monitors mon =新的Monitors();
bool成功=否;
byte [] subBuffer =新的byte [24];
SetControls.setAccessRule(lpFileName);
DealLastError(" SetControls.setAccessRule lpFileName");
SYSTEM_INFO lpSystemInfo =新的SYSTEM_INFO();
NativeMethods.GetSystemInfo(out lpSystemInfo);
Console.WriteLine("PageSize =" + lpSystemInfo.dwPageSize.ToString());
Console.WriteLine(" lpSystemInfo.dwAllocationGranularity: =" + lpSystemInfo.dwAllocationGranularity.ToString());
uint dwFlagsAndAttributes = FILE_ATTRIBUTE_DEVICE | FILE_FLAG_NO_BUFFERING |
nbsp; bsp FILE_FLAG_OVERLAPPED;
IntPtr hFile = CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ,IntPtr.Zero,
nbsp; bsp nbsp; bsp OPEN_EXISTING,dwFlagsAndAttributes,IntPtr.Zero);
DealLastError(" Create hFile");
public void DiskReadSimplified ( )
{
unsafe
{
string lpFileName = @"\\.\PHYSICALDRIVE0";
this.debugPrint = true;
Monitors mon = new Monitors ( );
bool success = false;
byte[ ] subBuffer = new byte[ 24 ];
SetControls.setAccessRule ( lpFileName );
DealLastError ( "SetControls.setAccessRule lpFileName " );
SYSTEM_INFO lpSystemInfo = new SYSTEM_INFO ( );
NativeMethods.GetSystemInfo ( out lpSystemInfo );
Console.WriteLine ( "PageSize = " + lpSystemInfo.dwPageSize.ToString ( ) );
Console.WriteLine ( "lpSystemInfo.dwAllocationGranularity: = " + lpSystemInfo.dwAllocationGranularity.ToString ( ) );
uint dwFlagsAndAttributes = FILE_ATTRIBUTE_DEVICE | FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED;
IntPtr hFile = CreateFile ( lpFileName, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero,
OPEN_EXISTING, dwFlagsAndAttributes, IntPtr.Zero );
DealLastError ( "Create hFile" );
OVERLAPPED lpOverlapped =新的OVERLAPPED();
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine =
nbsp; bsp nbsp; bsp 新的LPOVERLAPPED_COMPLETION_ROUTINE(目标);
//移动字节
byte [] lpBuffer =新的byte [lpSystemInfo.dwAllocationGranularity];
GCHandle GCH = GCHandle.Alloc(lpBuffer,GCHandleType.Pinned);
IntPtr lpBufferPointer = GCH.AddrOfPinnedObject();
OVERLAPPED lpOverlapped = new OVERLAPPED ( );
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine =
new LPOVERLAPPED_COMPLETION_ROUTINE ( Target );
// Moving bytes
byte[ ] lpBuffer = new byte[ lpSystemInfo.dwAllocationGranularity ];
GCHandle GCH = GCHandle.Alloc ( lpBuffer, GCHandleType.Pinned );
IntPtr lpBufferPointer = GCH.AddrOfPinnedObject ( );
LARGE_INTEGER liDistanceToMove =新的LARGE_INTEGER();
liDistanceToMove.u.LowPart = lpSystemInfo.dwAllocationGranularity; //64 K
LARGE_INTEGER liDistanceToMove = new LARGE_INTEGER ( );
liDistanceToMove.u.LowPart = lpSystemInfo.dwAllocationGranularity; // 64 K
IntPtr点=新的IntPtr();
GCH = GCHandle.Alloc(点,GCHandleType.Pinned);
System.IntPtr lpNewSourceFilePointer = GCH.AddrOfPinnedObject();
lpNewSourceFilePointer = IntPtr.Zero;
uint lpNumberOfBytesToRead = lpSystemInfo.dwAllocationGranularity; //64 K
如果(this.debugPrint)
{
Console.WriteLine(" lpNumberOfBytesToRead =" + lpNumberOfBytesToRead.ToString());
}
liDistanceToMove.u.LowPart = 0x00000000; //使用的第一个实例.无处可移动,只需将其设置为开始即可.
liDistanceToMove.u.HighPart = 0x00000000;
uint dwMoveMethod = FILE_BEGIN;
成功= NativeMethods.SetFilePointerEx(hFile,liDistanceToMove,lpNewSourceFilePointer,dwMoveMethod);
DealLastError(" SetFilePointerEx");
如果(this.debugPrint)
{
Console.WriteLine(成功移动第一个SetFilePointerEx ="+成功+" File Pointer ="+ lpNewSourceFilePointer);
}
IntPtr point = new IntPtr ( );
GCH = GCHandle.Alloc ( point, GCHandleType.Pinned );
System.IntPtr lpNewSourceFilePointer = GCH.AddrOfPinnedObject ( );
lpNewSourceFilePointer = IntPtr.Zero;
uint lpNumberOfBytesToRead = lpSystemInfo.dwAllocationGranularity; // 64 K
if ( this.debugPrint )
{
Console.WriteLine ( "lpNumberOfBytesToRead = " + lpNumberOfBytesToRead.ToString ( ) );
}
liDistanceToMove.u.LowPart = 0x00000000; // first instance of usage. Nowhere to move, simply set to start.
liDistanceToMove.u.HighPart = 0x00000000;
uint dwMoveMethod = FILE_BEGIN;
success = NativeMethods.SetFilePointerEx ( hFile, liDistanceToMove, lpNewSourceFilePointer, dwMoveMethod );
DealLastError ( "SetFilePointerEx" );
if ( this.debugPrint )
{
Console.WriteLine ( "success for moving the first SetFilePointerEx = " + success + " File Pointer = " + lpNewSourceFilePointer );
}
dwMoveMethod = FILE_CURRENT;
liDistanceToMove.u.LowPart = lpSystemInfo.dwAllocationGranularity; //64 K
this.completionCode = false;
dwMoveMethod = FILE_CURRENT;
liDistanceToMove.u.LowPart = lpSystemInfo.dwAllocationGranularity; // 64 K
this.completionCode = false;
mon.AddElement(lpBuffer);
DealLastError(" mon.AddElement");
成功=错误;
//第一回合
成功= NativeMethods.ReadFileEx(hFile,lpBufferPointer,lpNumberOfBytesToRead,ref lpOverlapped,lpCompletionRoutine);
DealLastError("ReadFileEx");
如果(this.debugPrint)
{
Array.Copy(lpBuffer,0,subBuffer,0,24);
DebugPrintData.DebugAndPrintData(subBuffer,"));
Console.WriteLine("ReadFileEx的成功=" +成功);
}
//第二回合
成功= NativeMethods.SetFilePointerEx(hFile,liDistanceToMove,lpNewSourceFilePointer,dwMoveMethod);
DealLastError("SetFilePointerEx P_3");
成功= NativeMethods.ReadFileEx(hFile,lpBufferPointer,lpNumberOfBytesToRead,ref lpOverlapped,lpCompletionRoutine);
DealLastError("ReadFileEx");
如果(this.debugPrint)
{
Array.Copy(lpBuffer,0,subBuffer,0,24);
DebugPrintData.DebugAndPrintData(subBuffer,"));
Console.WriteLine("成功的ReadFileEx =" +成功+"文件指针=" + lpNewSourceFilePointer);
}
//第三回合
成功= NativeMethods.SetFilePointerEx(hFile,liDistanceToMove,lpNewSourceFilePointer,dwMoveMethod);
DealLastError("SetFilePointerEx P_4");
成功= NativeMethods.ReadFileEx(hFile,lpBufferPointer,lpNumberOfBytesToRead,ref lpOverlapped,lpCompletionRoutine);
DealLastError("ReadFileEx");
如果(this.debugPrint)
{
Array.Copy(lpBuffer,0,subBuffer,0,24);
DebugPrintData.DebugAndPrintData(subBuffer,"));
Console.WriteLine("成功的ReadFileEx =" +成功+"文件指针=" + lpNewSourceFilePointer);
}
mon.AddElement ( lpBuffer );
DealLastError ( "mon.AddElement " );
success = false;
// Round 1
success = NativeMethods.ReadFileEx ( hFile, lpBufferPointer, lpNumberOfBytesToRead, ref lpOverlapped, lpCompletionRoutine );
DealLastError ( "ReadFileEx" );
if ( this.debugPrint )
{
Array.Copy ( lpBuffer, 0, subBuffer, 0, 24 );
DebugPrintData.DebugAndPrintData ( subBuffer, "" );
Console.WriteLine ( "success for ReadFileEx = " + success );
}
// Round 2
success = NativeMethods.SetFilePointerEx ( hFile, liDistanceToMove, lpNewSourceFilePointer, dwMoveMethod );
DealLastError ( "SetFilePointerEx P_3" );
success = NativeMethods.ReadFileEx ( hFile, lpBufferPointer, lpNumberOfBytesToRead, ref lpOverlapped, lpCompletionRoutine );
DealLastError ( "ReadFileEx" );
if ( this.debugPrint )
{
Array.Copy ( lpBuffer, 0, subBuffer, 0, 24 );
DebugPrintData.DebugAndPrintData ( subBuffer, "" );
Console.WriteLine ( "success for ReadFileEx = " + success + " File Pointer = " + lpNewSourceFilePointer );
}
// Round 3
success = NativeMethods.SetFilePointerEx ( hFile, liDistanceToMove, lpNewSourceFilePointer, dwMoveMethod );
DealLastError ( "SetFilePointerEx P_4" );
success = NativeMethods.ReadFileEx ( hFile, lpBufferPointer, lpNumberOfBytesToRead, ref lpOverlapped, lpCompletionRoutine );
DealLastError ( "ReadFileEx" );
if ( this.debugPrint )
{
Array.Copy ( lpBuffer, 0, subBuffer, 0, 24 );
DebugPrintData.DebugAndPrintData ( subBuffer, "" );
Console.WriteLine ( "success for ReadFileEx = " + success + " File Pointer = " + lpNewSourceFilePointer );
}
//第四回合
成功= NativeMethods.SetFilePointerEx(hFile,liDistanceToMove,lpNewSourceFilePointer,dwMoveMethod);
DealLastError("SetFilePointerEx P_4");
成功= NativeMethods.ReadFileEx(hFile,lpBufferPointer,lpNumberOfBytesToRead,ref lpOverlapped,lpCompletionRoutine);
DealLastError("ReadFileEx");
如果(this.debugPrint)
{
Array.Copy(lpBuffer,0,subBuffer,0,24);
DebugPrintData.DebugAndPrintData(subBuffer,"));
Console.WriteLine("成功的ReadFileEx =" +成功+"文件指针=" + lpNewSourceFilePointer);
}
// Round 4
success = NativeMethods.SetFilePointerEx ( hFile, liDistanceToMove, lpNewSourceFilePointer, dwMoveMethod );
DealLastError ( "SetFilePointerEx P_4" );
success = NativeMethods.ReadFileEx ( hFile, lpBufferPointer, lpNumberOfBytesToRead, ref lpOverlapped, lpCompletionRoutine );
DealLastError ( "ReadFileEx" );
if ( this.debugPrint )
{
Array.Copy ( lpBuffer, 0, subBuffer, 0, 24 );
DebugPrintData.DebugAndPrintData ( subBuffer, "" );
Console.WriteLine ( "success for ReadFileEx = " + success + " File Pointer = " + lpNewSourceFilePointer );
}
//第五回合
成功= NativeMethods.SetFilePointerEx(hFile,liDistanceToMove,lpNewSourceFilePointer,dwMoveMethod);
DealLastError("SetFilePointerEx P_4");
成功= NativeMethods.ReadFileEx(hFile,lpBufferPointer,lpNumberOfBytesToRead,ref lpOverlapped,lpCompletionRoutine);
DealLastError("ReadFileEx");
如果(this.debugPrint)
{
Array.Copy(lpBuffer,0,subBuffer,0,24);
DebugPrintData.DebugAndPrintData(subBuffer,"));
Console.WriteLine("成功的ReadFileEx =" +成功+"文件指针=" + lpNewSourceFilePointer);
}
}
}
我发誓几年前使用过origianl例程,并且阅读了顺序的代码块.某些事情已经改变,我无法理解差异.
// Round 5
success = NativeMethods.SetFilePointerEx ( hFile, liDistanceToMove, lpNewSourceFilePointer, dwMoveMethod );
DealLastError ( "SetFilePointerEx P_4" );
success = NativeMethods.ReadFileEx ( hFile, lpBufferPointer, lpNumberOfBytesToRead, ref lpOverlapped, lpCompletionRoutine );
DealLastError ( "ReadFileEx" );
if ( this.debugPrint )
{
Array.Copy ( lpBuffer, 0, subBuffer, 0, 24 );
DebugPrintData.DebugAndPrintData ( subBuffer, "" );
Console.WriteLine ( "success for ReadFileEx = " + success + " File Pointer = " + lpNewSourceFilePointer );
}
}
}
I swear the origianl routine worked years ago and I read sequential blocks of code. Something has changed and I cannot catch the difference.
最后所有错误均为0.成功总是返回true, lpNewSourceFilePointer每次迭代总是返回0.
All last errors are 0. success always returns true, lpNewSourceFilePointer always returns 0, every iteration.
出什么问题了?
谢谢.
推荐答案
http://msdn.microsoft.com/en-us/library/aa365542(VS.85).aspx
" lpNewFilePointer [输出,可选]
"lpNewFilePointer [out, optional]
指向用于接收新文件指针的变量的指针.如果此参数为NULL,则不返回新文件指针. "
A pointer to a variable to receive the new file pointer.If this parameter is NULL, the new file pointer is not returned."
如果要检索某些内容,请将该参数设为ref参数,或使其指向某个有效内存.
If you want to retrieve something, either make that parameter a ref parameter, or make it point to some valid memory.
这篇关于SetFilePointerEx不会移动指针.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!