I'm trying to read the PE headers of a file to get some information. For .NET
and C#
, I'm using BitConverter
to convert the Byte array obtained after having read the file to an integer equivalent. I wish to do the same with C++
, but am not sure of the best approach. I'm using an unsigned
char
array
as the Byte array
equivalent.
The code is given below..
uint16_t GetAppCompiledMachineType(string fileName)
{
const int ptr_offset = 4096;
const int mac_offset = 4;
char *data = new char[4096];
fstream f;
f.open(fileName, ios::in | ios::binary );
f.read(data, 4096);
int32_t pe_addr= *reinterpret_cast<int32_t*>(data, ptr_offset);
uint16_t machineUint = *reinterpret_cast<std::uint16_t*>(data, pe_addr + mac_offset);
return machineUint;
}
int _tmain(int argc, _TCHAR* argv[])
{
string fileName = "<some_path>\\depends.exe";
uint16_t tempInt = GetAppCompiledMachineType(fileName);
cout<<tempInt;
std::getchar();
return 0;
}
I'll be using the O/P to query the PE header for information. Need the equivalent of BitCOnverter here. and hopefully it will work.
UPDATE : Thanks for the replies. As suggested I'm trying to use the cast, to convert the character array
into Int
, to read the PE Header
, but it's giving me an access violation Unhandled exception. This is the full code, the file is valid and is being read. I tried with debug, and Optimization disabled, but to no avail.
Kindly advise.
Thanks a lot.