我有配置了軟盤驅動器(A :)的Windows 7虛擬機。我正嘗試將軟盤驅動器的引導扇區讀入結構中。但是,每次運行此程序時,都無法找到軟盤驅動器。我可以確認它可以訪問。C程序無法獲得軟盤驅動器的手柄
代碼:
#include "stdafx.h"
#include<Windows.h>
#include<stdio.h>
#include<conio.h>
#include<WinBase.h>
#pragma pack(1)
struct boot
{
BYTE jump[3];
char bsOemName[8];
WORD bytesperSector;
BYTE sectorpercluster;
WORD sectorsreservedarea;
BYTE copiesFAT;
WORD maxrootdirentries;
WORD totalSectors;
BYTE mediaDescriptor;
WORD sectorsperFAT;
WORD sectorsperTrack;
WORD sides;
WORD hiddenSectors;
char reserve[480];
};
void ReadSector(char *src, int ss, int num, void* buff);
void main()
{
struct boot b;
ReadSector("\\\\.\\A:", 0, 1, &b);
printf("\nBoot sector Name: %s\n", b.bsOemName);
printf("Bytes per sector: %d\n", b.bytesperSector);
printf("Sectors per Cluster: %d\n", b.sectorpercluster);
printf("Total Sectors: %d\n", b.totalSectors);
}
void ReadSector(char *src, int ss, int num, void* buff)
{
HANDLE h; //HANDLE is a typedef of void *HANDLE
unsigned int br;
h = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
DWORD dw = GetLastError();
printf("\nLast Error: %d", dw);
if (h != NULL)
{
printf("\nError reading floppy disk '%s'", src);
printf("\nReturn value for handle = %d", h);
}
else
{
printf("\nSuccess..");
}
SetFilePointer(h, (ss * 512), NULL,FILE_BEGIN);
ReadFile(h, buff, num, &br, NULL);
CloseHandle(h);
}
輸出/錯誤:從系統返回
C:\Users\IEUser\Desktop>Hardware.exe
Last Error: 2
Error reading floppy disk '\\.\A:'
Return value for handle = -1
Boot sector Name:
Bytes per sector: 14336
Sectors per Cluster: 248
Total Sectors: 0
錯誤代碼是2:系統無法找到指定的文件。
由於無法打開軟盤驅動器,因此結構變量會保存垃圾值。
有人可以幫忙嗎?
錯誤粘貼爲_text_請。 –
我打賭「A:」不是設備名稱。在設備管理器中找到您的驅動器,然後嘗試爲該設備列出的其他「名稱」。用於訪問軟盤的[文檔](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v = vs.85).aspx)中列出了一些限制;例如,你目前不使用'FILE_SHARE_WRITE',但看起來你必須。 –
請注意,[文檔](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v = vs.85).aspx)指出:「打開卷或軟盤時, dwShareMode參數必須具有FILE_SHARE_WRITE標誌。「 – nos