低级 C++ C# 中的 style i/o 用于读取 FAT32

发布于 2024-07-27 01:47:39 字数 2601 浏览 9 评论 0原文

我正在努力读取硬盘的 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:\< /code> 驱动器,出于试用目的,我让它读取第一个文件目录簇并查询每个文件条目,并显示文件名(如果已删除)。

然而,我想将其打造成一个成熟的应用程序,为此我必须经常使用字节数组并将其根据 FAT32 规范映射到指定的数据结构。

如何有效地使用正在读取数据的字节数组? 我已经使用 filestream 和 binaryreader 尝试了相同的代码,它可以工作,但是现在假设我有一个 C 结构,就像

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

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

I am working on reading the FAT32 entry of the hard disk and so far have been successful in reading the entries by making use of the CreateFile, ReadFile, and SetFilePointer APIs. Here is my code (written in C#) so far.

---The DLL IMPORTS-----

[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);

------CODE----Will Work in any .NET Application---------

        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);
                    }

                }
            }

The code above reads the F:\ drive and for trial purposes I have made it to read the first File Directory Cluster and query through each file entry and display the file name if it has been deleted.

However I want to make it to a full-blown application, for which I will have to frequently use the byte array and map it to the specified data structures according to the FAT32 Specification.

How can I efficiently use the byte array into which I am reading the data? I have tried the same code using filestream and binaryreader and it works, however now suppose I have a C Structure something like

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

I want to have a similar data structure in C# and when I read data to a byte array I want to map it to the structure. I tried many options but didn't get a complete solution. I tried making a class and do serialization too but that also didn't work. I have around 3 more structures like theese which I will be using as I read the data from the FAT entry. How can I best achieve the desired results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-08-03 01:47:39

如果您想将二进制数据直接读入结构体,C 风格, 这篇文章您可能感兴趣。 他围绕 C stdio 函数编写了一个非托管包装器并与其进行互操作。 我已经尝试过了——效果确实很好。 在 C# 中直接读取结构体很好,而且速度很快。 你可以这样做:

unsafe 
{ 
 fmp3.Read<MyStruct>(&myStructVar); 
}

If you want to read binary data directly into structs, C-style, this article may interest you. He wrote an unmanaged wrapper around the C stdio functions and interoperates with it. I have tried it - it does work quite well. It is nice to read directly into a struct in C#, and it is fast. You can just do:

unsafe 
{ 
 fmp3.Read<MyStruct>(&myStructVar); 
}
墨小墨 2024-08-03 01:47:39

我在

I gave an answer on how to convert between byte arrays and structs in this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文