【发布时间】:2017-02-12 13:08:05
【问题描述】:
我有一个配置了软盘驱动器 (A:) 的 Windows 7 VM。我正在尝试将软盘驱动器的引导扇区读入结构。但是,每次我运行这个程序时,它都找不到软驱。我可以确认它可以访问。
代码:
#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:系统找不到指定的文件。
由于无法打开软盘驱动器,结构变量包含垃圾值。
有人可以帮忙吗?
【问题讨论】:
-
请将错误粘贴为文本。
-
我敢打赌“A:”不是设备名称。在设备管理器中找到您的驱动器,然后尝试为该设备列出的其他一些“名称”。 the documentation 中还列出了一些访问软盘的限制;例如,您目前没有使用
FILE_SHARE_WRITE,但看起来您必须使用。 -
请注意,documentation 声明:“打开卷或软盘时,dwShareMode 参数必须具有 FILE_SHARE_WRITE 标志。”
-
@KlasLindbäck 这只是转义反斜杠,正如 C 字符串文字中所需要的那样。编译后,按照文档的建议,字符串正确:
\\.\A: -
@LightnessRacesinOrbit:感谢您的回复伙伴。我确实尝试了在设备管理器中找到的其他名称,但这没有帮助。我也尝试过使用“FILE_SHARE_WRITE”,但这也不起作用(尽管这是我应该根据文档使用的那个)。
标签: c windows console-application floppy