【问题标题】:How to read bin file (FAT16 partition) from a Memory Mapped File in C?如何从 C 中的内存映射文件中读取 bin 文件(FAT16 分区)?
【发布时间】:2015-05-09 19:01:11
【问题描述】:

我在 .bin 文件中有一个非常小的 FAT16 分区。 我已使用 CreateFile、CreateFileMapping 和 MapViewOfFile 将其映射到内存中。

我想要做的是读取文件的特定字节。

例如,我想读取从 0x36 到 0x3A 的偏移量,以检查这是否是 FAT16 分区:

这是我的代码,直到:

#include <Windows.h>
#include <stdio.h>

void CheckError (BOOL condition, LPCSTR message, UINT retcode);


int main(int argc, char *argv[])
{
    HANDLE hFile;
    HANDLE hMap;
    char *pView;
    DWORD TamArchivoLow, TamArchivoHigh;

    //> open file
    hFile =CreateFile (L"disk10mb.bin", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    CheckError(hFile == INVALID_HANDLE_VALUE,"ERROR opening the file", 1);

    //> get file size.
    TamArchivoLow = GetFileSize (hFile, &TamArchivoHigh);


    //> Create the map
    hMap = CreateFileMapping (hFile, NULL, PAGE_READWRITE, TamArchivoHigh, TamArchivoLow, NULL);
    CheckError(NULL== hMap, "ERROR executing CreateFileMapping", 1);

    //> Create the view
    pView= (char *) MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0,  TamArchivoLow);
    CheckError(NULL==pView, "ERROR executing MapViewOfFile", 1);


    // Access the file through pView
    //////////////////////////////////////





    //////////////////////////////////////



    //>Free view and map
    UnmapViewOfFile(pView);
    CloseHandle(hMap);
    CloseHandle(hFile);

    return 0;

}


void CheckError (BOOL condition, LPCSTR message, UINT retcode)
{
    if (condition)
    {
        printf ("%s\n", message);
        ExitProcess (retcode);
    }
}

【问题讨论】:

  • 这些函数仅仅读取 4 个字节就有点过头了。你试过简单的open() lseek() read() close()吗?
  • 我要读取更多字节,这只是开始

标签: c windows memory-mapped-files memory-mapping


【解决方案1】:

pview[0x36] 将为您提供偏移量 0x36 处的字节,依此类推。例如,要检查 FAT16 签名,您可以:

if (pview[0x36] == 'F' && pview[0x37] == 'A' && pview[0x38] == 'T' &&
        pview[0x39] == '1' && pview[0x3A] == '6') {
    // ...
}

【讨论】:

  • 你如何比较 pView[0x36] 和字符 'F' ?
  • @user204415 听起来你最好先学习更多关于基本 C 编程的知识,然后再做这样的花哨的东西。
  • @user204415 我添加了一个简单的示例。 (可能有更好的方法,但这个是最直接理解的)。
猜你喜欢
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多