Well I have .exe application and some other file. What I want to do is to write this other file to the end of .exe file. .exe file should find the address of this file in its memory, read it from there and do some stuff.
I was able to get to the address of the file I wrote to the memory before but when I try reading from there I get access denied exception. How can I read from there?
Basically I just want to have one self-unpacking PE file. Yes, I know, I can just make self-extracting archive but that's not what I want because I need both .exe and .dll but self-extracting archive can be only .exe so it looks the only way is to make my application self-extracting itself. Here's the code:
int main(void)
{
HMODULE hBegin = GetModuleHandle(NULL);
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)hBegin;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((PBYTE)hBegin + dosHeader->e_lfanew);
PIMAGE_SECTION_HEADER pSectionTable = (PIMAGE_SECTION_HEADER)(ntHeaders + 1);
// get size of each section
DWORD dwSize = 0;
for(int i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++)
{
dwSize += pSectionTable[i].SizeOfRawData;
}
//get size of PE headers
dwSize += ntHeaders->OptionalHeader.SizeOfHeaders;
WCHAR lpszSfxPath[MAX_PATH];
GetModuleFileNameW(NULL, lpszSfxPath, MAX_PATH);
HANDLE hFile = CreateFileW(lpszSfxPath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
SetFilePointer(hFile, dwSize, NULL, FILE_BEGIN);
BYTE BUF[10];
if(!ReadFile(hFile,BUF,sizeof(BYTE),NULL,NULL))
printf("FAIL!\n");
printf("HELLO WORLD\n");
getchar();
return 0;
}
After calling SetFilePointer file pointer points just after the end of file where my packed file is stored, but I'm not able to read from ther