我正在使用FileRead API。 我使用了Windows 7 x64,並且我的代碼工作正常。 現在我安裝了一個新的Windows 7 x86和VS2008 teamsuit以及.NET 2,3 + SP1 + SP2,3.5,3.5.1。 我跑我的代碼作爲管理員,但仍然遇到follwoing錯誤:使用FileRead API時出現AccessViolationException
AccessViolationException(Attempted to read or write protected memory. This is often an indication that other memory is corrupt.)
int nread = 0;
uint handle;
byte[] buff = new byte[1024];
string driveRoot = string.Concat("\\\\.\\", driveLetter);
uint hRoot = CreateFile(driveRoot,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (hRoot != -1)
handle = ReadFile(hRoot, buff, 1024, nread, new System.Threading.NativeOverlapped());
你的句柄應該是IntPtr,但是在x86上不會有問題。儘管如此,x64應該是個問題。而且nread也作爲指針傳遞,你傳遞一個uint。還應該是一個IntPtr。 –