简体   繁体   English

ReadFile无法正确读取字节

[英]ReadFile not reading the bytes correctly

I am trying to read a file using readfile, store it into a wide array, then write it into another file. 我正在尝试使用readfile读取文件,将其存储到一个宽数组中,然后将其写入另一个文件中。 Problem is, when I put them side by side in HxD some bytes are correct (the text, for example) but everything else is completely different. 问题是,当我将它们并排放置在HxD中时,某些字节是正确的(例如文本),但其他所有内容完全不同。 I can't run it either 我也不能跑

struct a
{
    BYTE* buff;
    long siz;
};

int main()
{
    HANDLE hFile;
    a struct_a;

    if (hFile = CreateFileW(L"C:\\Windows\\System32\\notepad.exe", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr))
    {
        long lFileSize = GetFileSize(hFile, nullptr);

        if (lFileSize)
        {
            struct_a.siz = lFileSize;
            struct_a.buff = new BYTE[struct_a.siz];

            if (ReadFile(hFile, struct_a.buff, struct_a.siz,
                nullptr, nullptr))
            {
                CloseHandle(hFile);
            }

        }
    }

    HANDLE h = CreateFileA("C:\\Users\\USER\\Desktop\\notepad_new.exe", GENERIC_WRITE, FILE_SHARE_WRITE, nullptr,
        CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);

    WriteFile(h, struct_a.buff, struct_a.siz, nullptr, nullptr);

return 0;
}

I want it to be able to read the file correctly and then write it and have me be able to run it. 我希望它能够正确读取文件,然后将其写入并让我能够运行它。


As a bonus, I also tried writing some bytes around the end of the file after I read it by doing 另外,在读取文件后,我还尝试在文件末尾写一些字节

struct_a.buff[struct_a.siz - 5] = L'A';

but it never did show up anywhere. 但它从未出现在任何地方。 But when I tried writing it at the beginning (removing the brackets) it wrote it fine. 但是,当我尝试在一开始编写它(除去括号)时,它写得很好。

EDIT: I tried reading it afterwards and it read the correct letter weirdly enough 编辑:我尝试之后再阅读它,它足够奇怪地阅读正确的字母

EDIT 2: Picture of issue: 编辑2:问题图片: 在此处输入图片说明

The left file in the screenshot is a 32-bit EXE file. 屏幕截图中的左侧文件是32位EXE文件。 The byte you have highlighted that is different is the address of the IMAGE_NT_HEADERS structure in the file. 您突出显示的字节与文件中IMAGE_NT_HEADERS结构的地址不同。

At address 0xFC, 4 bytes into this structure, the 2 bytes are 4C 01. This is the Machine field in IMAGE_FILE_HEADERS and this value indicates the machine is "i386" (ie a 32-bit program). 在地址0xFC处,此结构中有4个字节,这2个字节是4C01。这是IMAGE_FILE_HEADERS中的Machine字段,并且该值指示该机器是“ i386”(即32位程序)。

In the right file, the address is 0xEC instead, and the bytes are 64 86, which is "AMD64" (ie this is a 64-bit program). 在正确的文件中,地址改为0xEC,字节为64 86,即“ AMD64”(即,这是一个64位程序)。

Probably your program is a 32-bit program, and so it accesses the 32-bit version of System32, because of a Windows feature called file system redirection (thanks to Paul Sanders for the link). 您的程序可能是32位程序,由于Windows称为文件系统重定向的功能 (感谢Paul Sanders提供了链接),因此它可以访问System32的32位版本。 On 64-bit Windows, 32-bit programs have System32 redirected to a different folder (which is really called SysWOW64) - according to this table: 在64位Windows上,根据此表,将32位程序的System32重定向到另一个文件夹(实际上称为SysWOW64):

                   32-bit System32        64-bit System32

32-bit program   C:\Windows\System32    C:\Windows\sysnative
64-bit program   C:\Windows\SysWOW64    C:\Windows\System32

You can solve this problem by either reading the notepad.exe from sysnative, or comparing it against the one in SysWOW64 instead of the one in System32, or by compiling your program as 64-bit. 您可以通过从sysnative读取notepad.exe或将其与SysWOW64中的内容而不是System32中的内容进行比较,或通过将程序编译为64位来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM