代码之家  ›  专栏  ›  技术社区  ›  Anirudh Goel

用于读取FAT32的C语言中的低级C++ I/O样式

  •  2
  • Anirudh Goel  · 技术社区  · 16 年前

    我正在读硬盘的fat32条目,到目前为止,我已经通过使用 CreateFile , ReadFile SetFilePointer API。这是我的代码(用c_写成)。

    ---dll导入---

    [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr CreateFile(string lpFileName, Int32 dwDesiredAccess,
                Int32 dwShareMode, Int32 lpSecurityAttributes, Int32 dwCreationDisposition,
                Int32 dwFlagsAndAttributes, IntPtr hTemplateFile);
    
            [DllImport("kernel32.dll")]
            static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
               uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, uint lpOverlapped);
    
            [DllImport("kernel32.dll")]
            extern static int SetFilePointer(IntPtr hFile, int lDistanceToMove, int lpDistanceToMoveHigh, uint dwMoveMethod);
    
            [DllImport("kernel32.dll")]
            extern static Boolean CloseHandle(IntPtr hObject);
    

    ----代码-----将在任何.NET应用程序中工作-----

            int ret, nread;
            IntPtr handle;
            int s = 512;
            byte[] readbuffer = new byte[512];
    
        IntPtr ptr = CreateFile(@"\\.\F:", -1073741824, 3, 0, 3, 128, IntPtr.Zero);
     if (ptr != System.IntPtr.Zero)
                {
                    int i = 100;
                    int ret = SetFilePointer(ptr, 0, 0, 0);
                    ret = SetFilePointer(ptr, 4194304, 0, 1);
                    while (true)
                    {
                        byte[] inp = new byte[512];
                        uint read = 0;
                        if (ret != -1)
                        {
                            ReadFile(ptr, inp, 512, out read, 0);
                            for (int k = 0; k < 16; k++)
                            {
                                string s = ASCIIEncoding.ASCII.GetString(inp, k*32, 11);
                                if (inp[k*32] == 0xE5)
                                {
                                    MessageBox.Show(s);
                                }
                            }
                            //ret = SetFilePointer(ptr, 512, 0, 1);
                        }
    
                    }
                }
    

    上面的代码读取 F:\ 驱动器,出于试用目的,我已使它读取第一个文件目录集群,并通过每个文件条目进行查询,如果文件名已被删除,则显示该文件名。

    不过,我想让它成为一个成熟的应用程序,为此我必须经常使用字节数组,并根据fat32规范将其映射到指定的数据结构。

    如何有效地使用读取数据的字节数组?我尝试过使用filestream和binaryreader编写相同的代码,但现在假设我有一个类似于

    struct bios_data
    {
    byte id[3];
    char name[11];
    byte sectorpercluster[2];
    ...
    }
    

    我想在c中有一个类似的数据结构,当我把数据读到字节数组时,我想把它映射到结构。我尝试了很多选择,但没有得到一个完整的解决方案。我试着创建一个类并进行序列化,但也没有成功。我还有大约3个类似see的结构,当我从fat条目中读取数据时,我将使用它们。我怎样才能最好地达到预期的效果?

    2 回复  |  直到 14 年前
        1
  •  1
  •   R Ubben    16 年前

    如果您想直接将二进制数据读入结构,C样式, this article 可能会引起你的兴趣。他围绕C stdio函数编写了一个非托管包装器,并与之进行互操作。我试过了,效果很好。在c_中直接读入结构非常好,而且速度很快。你可以这样做:

    unsafe 
    { 
     fmp3.Read<MyStruct>(&myStructVar); 
    }
    
        2
  •  0
  •   Community Mohan Dere    9 年前

    我给出了如何在 this question .