(ExeFile): new class to seek for resources within EXE files.

This commit is contained in:
morkt
2017-01-23 18:35:16 +04:00
parent c879c60a41
commit 42dce0e586
5 changed files with 290 additions and 129 deletions

View File

@@ -363,15 +363,12 @@ namespace GameRes.Formats.CatSystem
/// </summary>
public static string GetPassFromExe (string filename)
{
var exe = NativeMethods.LoadLibraryEx (filename, IntPtr.Zero, 0x20); // LOAD_LIBRARY_AS_IMAGE_RESOURCE
if (IntPtr.Zero == exe)
throw new Win32Exception (Marshal.GetLastWin32Error());
try
using (var exe = new ExeFile.ResourceAccessor (filename))
{
var code = GetResource (exe, "DATA", "V_CODE2");
var code = exe.GetResource ("DATA", "V_CODE2");
if (null == code || code.Length < 8)
return null;
var key = GetResource (exe, "KEY", "KEY_CODE");
var key = exe.GetResource ("KEY", "KEY_CODE");
if (null != key)
{
for (int i = 0; i < key.Length; ++i)
@@ -388,50 +385,6 @@ namespace GameRes.Formats.CatSystem
length = code.Length;
return Encodings.cp932.GetString (code, 0, length);
}
finally
{
NativeMethods.FreeLibrary (exe);
}
}
static byte[] GetResource (IntPtr exe, string name, string type)
{
var res = NativeMethods.FindResource (exe, name, type);
if (IntPtr.Zero == res)
return null;
var glob = NativeMethods.LoadResource (exe, res);
if (IntPtr.Zero == glob)
return null;
uint size = NativeMethods.SizeofResource (exe, res);
var src = NativeMethods.LockResource (glob);
if (IntPtr.Zero == src)
return null;
var dst = new byte[size];
Marshal.Copy (src, dst, 0, dst.Length);
return dst;
}
}
static internal class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static internal extern IntPtr LoadLibraryEx (string lpFileName, IntPtr hReservedNull, uint dwFlags);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static internal extern bool FreeLibrary (IntPtr hModule);
[DllImport( "kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static internal extern IntPtr FindResource (IntPtr hModule, string lpName, string lpType);
[DllImport("Kernel32.dll", SetLastError = true)]
static internal extern IntPtr LoadResource (IntPtr hModule, IntPtr hResource);
[DllImport("Kernel32.dll", SetLastError = true)]
static internal extern uint SizeofResource (IntPtr hModule, IntPtr hResource);
[DllImport("kernel32.dll")]
static internal extern IntPtr LockResource (IntPtr hResData);
}
}