无法阅读超越Stream的结尾

时间:2013-03-29 08:21:39

标签: c# filestream binaryreader

我找到了一个关于阅读PE文件的链接:http://code.cheesydesign.com/?p=572。我正在使用此代码,但不幸的是,我在ntHeadersSignature(reader.ReadUint32)上收到错误。它说它无法在流之外阅读。我想知道为什么以及如何修复此错误,因为我尝试使用peekchar并使用readint32读取它,将其转换为uint不起作用。感谢您的真诚帮助!

public PeHeaderReader(string filePath)
{
    // Read in the DLL or EXE and get the timestamp
    using (FileStream stream = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
    {
        BinaryReader reader = new BinaryReader(stream);
        dosHeader = FromBinaryReader<IMAGE_DOS_HEADER>(reader);

        // Add 4 bytes to the offset
        stream.Seek(dosHeader.e_lfanew, SeekOrigin.Begin);

        UInt32 ntHeadersSignature = reader.ReadUInt32(); //HERE IS THE ERROR
        fileHeader = FromBinaryReader<IMAGE_FILE_HEADER>(reader);

        if (this.Is32BitHeader)
        {
            optionalHeader32 = FromBinaryReader<IMAGE_OPTIONAL_HEADER32>(reader);
        }
        else
        {
            optionalHeader64 = FromBinaryReader<IMAGE_OPTIONAL_HEADER64>(reader);
        }

        imageSectionHeaders = new IMAGE_SECTION_HEADER[fileHeader.NumberOfSections];

        for(int headerNo = 0; headerNo < imageSectionHeaders.Length; ++headerNo)
        {
            imageSectionHeaders[headerNo] = FromBinaryReader<IMAGE_SECTION_HEADER>(reader);
        }
    }
}

0 个答案:

没有答案