2016-10-04 80 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

相關問題