2016-10-04 63 views
0

我有配置了软盘驱动器(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 

Output Screenshot

错误代码是2:系统无法找到指定的文件。

由于无法打开软盘驱动器,因此结构变量会保存垃圾值。

有人可以帮忙吗?

+1

错误粘贴为_text_请。 –

+0

我打赌“A:”不是设备名称。在设备管理器中找到您的驱动器,然后尝试为该设备列出的其他“名称”。用于访问软盘的[文档](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v = vs.85).aspx)中列出了一些限制;例如,你目前不使用'FILE_SHARE_WRITE',但看起来你必须。 –

+1

请注意,[文档](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v = vs.85).aspx)指出:“打开卷或软盘时, dwShareMode参数必须具有FILE_SHARE_WRITE标志。“ – nos

回答

1

看起来传递给ReadSector()函数的参数有问题(它依次将参数传递给CreateFile()函数)和ReadFile()函数调用。

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

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

那固定的文件句柄问题,但随后无法读取file.I然后实现它是不工作的ReadFile()函数。

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

我刚刚和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

+0

如果此代码(或者就此而言,原始代码)编译时不会抱怨类型不匹配,那么您的编译器配置错误。 'src'参数应该是'wchar_t *'以匹配CreateFile()。 –

+0

谢谢@HarryJohnston! – shellcode

+0

简单的解决方案是将字符集更改为“多字节字符集”。 – shellcode

相关问题