0
我試圖從一個特定的物理扇區向SD卡寫入一些數據。我收到了一家公司的代碼,他們說它可以在windows-xp上正常工作。這與WriteFile error #5 "denied access" under win Vista/seven
是一樣的情況下面是將數據寫入SD卡的部分(在我的cae drv值爲'F')。從其他人那裏讀取,我添加了鎖定和dismont,但鎖定失敗(也下載了)。我對Windows編程不太熟悉。有人能告訴我這段代碼有什麼問題嗎?謝謝你的幫助。 (BTW I; m鎖定3GiB)從WriteFile到物理磁盤的訪問被拒絕錯誤,win7
u32 HDD_write(u8 drv, u32 SecAddr, u32 blocks, u8 *buf)
{
u32 ret = 0;
u32 ldistanceLow, ldistanceHigh, dwpointer, bytestoread, numread;
char cur_drv[100];
HANDLE g_hDevice;
sprintf(cur_drv, "\\\\.\\%c:",drv); // , (u32)drv);
g_hDevice = CreateFile(cur_drv, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(g_hDevice == INVALID_HANDLE_VALUE)
return 0;
// lock and dismount
ret = LockFile(g_hDevice, 0, 0, 3 * 1023 * 1023 * 1023, 0);
printf("ret = %d", ret);
DeviceIoControl(g_hDevice, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, NULL, NULL);
printf("error = %d", GetLastError());
ldistanceLow = SecAddr << 9;
ldistanceHigh = SecAddr >> (32-9);
dwpointer = SetFilePointer(g_hDevice, ldistanceLow, (long *)&ldistanceHigh, FILE_BEGIN);
if(dwpointer != 0xFFFFFFFF) {
bytestoread = blocks * 512;
ret = WriteFile(g_hDevice, buf, bytestoread, (unsigned long *)&numread, NULL);
if(ret) ret = 1;
else {
ret = 0;
printf("error = %d", GetLastError());
}
}
CloseHandle(g_hDevice);
return ret;
}
當'LockFile()'失敗時'GetLastError()'返回什麼? – Cyclonecode 2014-11-25 08:26:49
你試過用DeviceIoControl(g_hDevice,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&dwBytesReturned,NULL)來鎖定音量嗎? – Cyclonecode 2014-11-25 08:37:37
[CreateFile:對原始磁盤的直接寫入操作「Access is denied」 - Vista,Win7]的可能的重複(http://stackoverflow.com/questions/8694713/createfile-direct-write-operation-to-raw-disk- access-is-denied-vista-win7) – Xearinox 2014-11-25 11:18:06