Last active
March 11, 2017 19:04
-
-
Save azyobuzin/e41436350721c178fe368948ca741444 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
namespace ResourceInDll | |
{ | |
internal static class Extensions | |
{ | |
public static byte[] ReadExact(this Stream stream, int size) | |
{ | |
var bytesRead = 0; | |
var bs = new byte[size]; | |
while (bytesRead < size) | |
{ | |
var i = stream.Read(bs, bytesRead, size - bytesRead); | |
if (i == 0) throw new EndOfStreamException("EOF"); | |
bytesRead += i; | |
} | |
return bs; | |
} | |
public static int ReadDWord(this Stream stream) | |
{ | |
var bs = stream.ReadExact(4); | |
return bs[0] | bs[1] << 8 | bs[2] << 16 | bs[3] << 24; | |
} | |
public static int ReadWord(this Stream stream) | |
{ | |
var bs = stream.ReadExact(2); | |
return bs[0] | bs[1] << 8; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace ResourceInDll | |
{ | |
public class Class1 | |
{ | |
public static Stream GetResourceStream() | |
{ | |
var thisFilePath = typeof(Class1).Assembly.Location; | |
var stream = new FileStream(thisFilePath, FileMode.Open, FileAccess.Read); | |
stream.Seek(60, SeekOrigin.Begin); | |
var ntHeaderAddress = stream.ReadDWord(); | |
stream.Seek(ntHeaderAddress + 6, SeekOrigin.Begin); | |
var numberOfSections = stream.ReadWord(); | |
stream.Seek(12, SeekOrigin.Current); | |
var sizeOfOptionalHeader = stream.ReadWord(); | |
// 最初のセクションヘッダー | |
stream.Seek(2 + sizeOfOptionalHeader, SeekOrigin.Current); | |
var maxAddress = 0; | |
for(var i = 0; i < numberOfSections; i++) | |
{ | |
stream.Seek(16, SeekOrigin.Current); | |
var sizeOfRawData = stream.ReadDWord(); | |
var pointerToRawData = stream.ReadDWord(); | |
maxAddress = Math.Max( | |
maxAddress, | |
pointerToRawData + sizeOfRawData | |
); | |
stream.Seek(16, SeekOrigin.Current); | |
} | |
stream.Seek(maxAddress, SeekOrigin.Begin); | |
return stream; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment