如何在Windows系统上读取MBR数据

时间:2018-02-07 14:51:56

标签: c++ windows mbr

我正在使用一个文件,其MBR代码的偏移量为54字节。 在main()函数中,我调用OpenMBbrFile(),然后调用ReadMbrData()。 但是,我无法正确读取缓冲区中的MBR。请帮助我解决问题............................................ .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. ........

BOOL OpenMbrFile(LPWSTR sPath)
{

    cout << "OpenMBR: Create Handle" << endl;

    m_hMbrFile = CreateFile(sPath
        , GENERIC_READ
        , FILE_SHARE_READ
        , NULL
        , OPEN_EXISTING
        , FILE_ATTRIBUTE_NORMAL
        , NULL);

    if (m_hMbrFile == INVALID_HANDLE_VALUE) return FALSE;

    return TRUE;
}

BOOL CloseMbrFile()
{

    cout << "CloseMBR: Closing Handle" << endl;

    BOOL bReturn = TRUE;

    if (m_hMbrFile != INVALID_HANDLE_VALUE)
        bReturn = CloseHandle(m_hMbrFile);

    m_hMbrFile = NULL;

    cout << "Returning from CloseMBR" << endl;

    return bReturn;
}


BOOL ReadMbrData()
{

    cout << "ReadMBR: Initialization" << endl;

    BOOL bReturn = FALSE;
    DWORD dwByteRead = 0;
    LARGE_INTEGER filepointer;
    filepointer.QuadPart = 54;  //MBR data in File at offset 54
    BYTE* pBuff = NULL;
    pBuff = new BYTE[512];
    if (!pBuff) 
    {
        cout << "ReadMBR: Cleaning Stuff up" << endl;
        if (pBuff) delete[] pBuff;
        CloseMbrFile();
        ClosePhysicalDrive();
        return bReturn;
    }

    if (m_hMbrFile == INVALID_HANDLE_VALUE) return FALSE;

    cout << "ReadMBR: Setting fp" << endl;

    if(!SetFilePointerEx(m_hMbrFile, filepointer, NULL, FILE_BEGIN))
    {
        cout << "Failed to SetFilePointer ( " << m_hMbrFile << ", " << filepointer.QuadPart << ", 0, FILE_BEGIN)" << endl;
        cout << "ReadMBR: Cleaning Stuff up" << endl;
        if (pBuff) delete[] pBuff;
        CloseMbrFile();
        ClosePhysicalDrive();
        return bReturn;

    }

    cout << "ReadMBR: Starting to read File" << endl;

    bReturn = ReadFile(m_hMbrFile, pBuff, sizeof(*pBuff), &dwByteRead, 0);

    if (bReturn)
        bReturn = (sizeof(*pBuff) == dwByteRead);       //Need to check this condition? shd it be 512 or size of wud do??

    cout << "ReadMBR: Cleaning Stuff up" << endl;
    if (pBuff) delete[] pBuff;
    CloseMbrFile();
    ClosePhysicalDrive();

    return bReturn;
}

0 个答案:

没有答案