diff --git a/ClrPhlib/include/ClrPhlib.h b/ClrPhlib/include/ClrPhlib.h index 7897dbe..69cf4a9 100644 --- a/ClrPhlib/include/ClrPhlib.h +++ b/ClrPhlib/include/ClrPhlib.h @@ -105,9 +105,9 @@ namespace System { DateTime ^ Time; Int16 Magic; - IntPtr ImageBase; + Int64 ImageBase; Int32 SizeOfImage; - IntPtr EntryPoint; + Int64 EntryPoint; Int32 Checksum; @@ -163,7 +163,7 @@ namespace System { !PE(); // Initalize PeProperties struct once the PE has been loaded into memory - void InitProperties(); + bool InitProperties(); private: diff --git a/ClrPhlib/src/managed/PE.cpp b/ClrPhlib/src/managed/PE.cpp index a383739..559b658 100644 --- a/ClrPhlib/src/managed/PE.cpp +++ b/ClrPhlib/src/managed/PE.cpp @@ -31,13 +31,22 @@ PE::!PE() { bool PE::Load() { + // Load PE as mapped section wchar_t* PvFilepath = (wchar_t*)(Marshal::StringToHGlobalUni(Filepath)).ToPointer(); this->LoadSuccessful = m_Impl->LoadPE(PvFilepath); + Marshal::FreeHGlobal(IntPtr((void*)PvFilepath)); - if (LoadSuccessful) - InitProperties(); + if (!LoadSuccessful) { + return false; + } + + // Parse PE + LoadSuccessful &= InitProperties(); + if (!LoadSuccessful) { + m_Impl->UnloadPE(); + return false; + } - Marshal::FreeHGlobal(IntPtr((void*)PvFilepath)); return LoadSuccessful; } @@ -48,7 +57,7 @@ void PE::Unload() m_Impl->UnloadPE(); } -void PE::InitProperties() +bool PE::InitProperties() { LARGE_INTEGER time; SYSTEMTIME systemTime; @@ -69,17 +78,17 @@ void PE::InitProperties() { PIMAGE_OPTIONAL_HEADER32 OptionalHeader = (PIMAGE_OPTIONAL_HEADER32) &PvMappedImage.NtHeaders->OptionalHeader; - Properties->ImageBase = (IntPtr) (Int32) OptionalHeader->ImageBase; + Properties->ImageBase = (Int64) OptionalHeader->ImageBase; Properties->SizeOfImage = OptionalHeader->SizeOfImage; - Properties->EntryPoint = (IntPtr) (Int32) OptionalHeader->AddressOfEntryPoint; + Properties->EntryPoint = (Int64) OptionalHeader->AddressOfEntryPoint; } else { PIMAGE_OPTIONAL_HEADER64 OptionalHeader = (PIMAGE_OPTIONAL_HEADER64)&PvMappedImage.NtHeaders->OptionalHeader; - Properties->ImageBase = (IntPtr)(Int64)OptionalHeader->ImageBase; + Properties->ImageBase = (Int64)OptionalHeader->ImageBase; Properties->SizeOfImage = OptionalHeader->SizeOfImage; - Properties->EntryPoint = (IntPtr)(Int64)OptionalHeader->AddressOfEntryPoint; + Properties->EntryPoint = (Int64)OptionalHeader->AddressOfEntryPoint; } @@ -91,6 +100,7 @@ void PE::InitProperties() Properties->DllCharacteristics = PvMappedImage.NtHeaders->OptionalHeader.DllCharacteristics; Properties->FileSize = PvMappedImage.Size; + return true; }