gpt4 book ai didi

C 程序无法获取软盘驱动器的句柄

转载 作者:行者123 更新时间:2023-11-30 15:05:24 26 4
gpt4 key购买 nike

我有一个配置了软盘驱动器 (A:) 的 Windows 7 虚拟机。我正在尝试将软盘驱动器的引导扇区读入结构中。然而,每次我运行这个程序时,它都无法找到软盘驱动器。我可以确认它是可以访问的。

代码:

#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

Output Screenshot

系统返回的错误代码为2:系统找不到指定的文件。

由于无法打开软驱,结构体变量中保存着垃圾值。

有人可以帮忙吗?

最佳答案

传递给 ReadSector() 函数的参数(该函数又将参数传递给 CreateFile() 函数)和 ReadFile() 函数调用似乎存在问题。

有问题的代码:ReadSector("\\\\.\\A:", 0, 1, &b);

我只需将“L”添加到第一个参数:ReadSector(L"\\\\.\\A:", 0, 1, &b);

这解决了文件句柄问题,但随后无法读取文件。然后我意识到是 ReadFile() 函数不起作用。

有问题的代码:ReadFile(h, buff, num, &br, NULL);

我只需将“num”替换为 512,因为此函数需要知道需要读取多少字节。这里的“num”设置为 1,这就是它没有按预期工作的原因。

ReadFile(h, buff, 512, &br, NULL)

我对原始代码进行了一些修改,以检查 CreateFile() 和 ReadFile() 返回值。

修改后的代码如下:

#include "stdafx.h"
#include<Windows.h>
#include<stdio.h>
#include<conio.h>
#include<WinBase.h>

#pragma pack(1)

struct boot
{
BYTE jump[3]; //BYTE is a typedef for unsigned char
char bsOemName[8];
WORD bytesperSector; //WORD is a typdef for unisigned short
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(L"\\\\.\\A:", 0, 1, &b); //Machinename.drive, 0 = read 0th logical sector(that is Boot Sector), 1 = Read 1 sector, &b = Read it into Structure b

printf("\nOEM Name: %s", b.bsOemName);
printf("\nBytes per sector: %d", b.bytesperSector);
printf("\nSectors per Cluster: %d", b.sectorpercluster);
printf("\nTotal 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);

if (h == INVALID_HANDLE_VALUE)
{
printf("\nError reading disk '%s'", src);
//printf("\nReturn value for handle = %d", h);
printf("\nLast Error: %ld", dw);

}

else
{
printf("\nReturn value for handle = %d", h);
}

SetFilePointer(h, (ss * 512), NULL,FILE_BEGIN );
if (!ReadFile(h, buff, 512, &br, NULL))
{
printf("\nReadFile: %u\n", GetLastError());
}
else
{
printf("\nRead File Success!\n");
}


CloseHandle(h);
}

程序输出:

C:\Users\IEUser\Desktop>Hardware.exe

Return value for handle = 36
Read File Success!

OEM Name: *-v4VIHC
Bytes per sector: 512
Sectors per Cluster: 1
Total Sectors: 2880

C:\Users\IEUser\Desktop>

引用:read-and-write-hard-disk-sector-directly-and-efficiently

关于C 程序无法获取软盘驱动器的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com