2016-10-04 3 views
0

플로피 드라이브 (A :)가 설치된 Windows 7 VM이 있습니다. 플로피 드라이브의 부트 섹터를 구조로 읽으려고합니다. 그러나이 프로그램을 실행할 때마다 플로피 드라이브를 찾지 못합니다. 나는 그것이 접근 가능하다는 것을 확인할 수있다.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

[documentation] (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);하지만 그 다음 실현 믿을수를 읽을 수 없습니다 작동하지 않는 ReadFile() 함수였습니다. 문제

코드 : ReadFile(h, buff, num, &br, NULL); 난 그냥이 기능은 읽을 필요가 얼마나 많은 바이트 알 필요가로 512 'NUM'을 대체했다

. 여기서 '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' 매개 변수는 CreateFile()과 일치시키기 위해'wchar_t * '이어야합니다. –

+0

감사합니다. @HarryJohnston! – shellcode

+0

간단한 해결책은 문자 집합을 "멀티 바이트 문자 집합"으로 변경하는 것입니다. – shellcode

관련 문제