将.exe文件嵌入C ++程序? [英] Embed .exe file into C++ program?
问题描述
我写了一个c ++程序,我想在其中执行我的第二个程序,这是一个exe文件.问题是我想将我的程序作为一个文件共享给其他人.
I wrote a c++ program and I want to execute my second program inside it, which is a exe file. The problem is I want to share my program to others as one single file.
当我在互联网上搜索时,我找到了解决方案.
When I search on the internet, I found this solution.
只需将第二个.exe文件作为二进制资源存储在主文件中.exe在编译时使用.rc文件.在运行时,您可以访问它使用
FindResource()
,LoadResource()
和LockResource()
,然后在将其传递到system()
之前,将其写到磁盘上的临时文件中.
Just store the second .exe file as a binary resource inside the main .exe using an .rc file at compile-time. At run-time, you can access it using
FindResource()
,LoadResource()
, andLockResource()
, and then write it out to a temp file on disk before passing it tosystem()
.
但是我不明白如何将.exe文件存储为二进制资源"
But I don't understand how to "store the .exe file as a binary resource"
我目前正在使用 CreateProcess()
来启动第二个程序,该程序运行良好.谁能为我写一些例子?
I am currently using CreateProcess()
to start my second program which working greatly.
Can anyone write some example for me?
推荐答案
在项目的资源脚本(定义了图标,对话框等的 .rc
文件)中,您可以添加一个二进制资源,其行如下所示:
In your project's resource script (the .rc
file in which icons, dialogs, etc. are defined), you can add a binary resource with a line like the following:
IDB_EMBEDEXE BINARY "<path>\\EmbedProgram.exe"
IDB_EMBEDEXE 标记/宏应在资源文件和 包含的 所包含的头文件中定义(使用它;这将是给 FindResource()
调用赋予的 lpName
参数,您可以使用 MAKEINTRESOURCE(IDB_EMBEDEXE)
形成该参数.为 lpType
参数指定"BINARY"
(对于Unicode构建,请指定 L"BINARY"
).
Where the IDB_EMBEDEXE
token/macro should be defined in a header file that is included by both that resource script and any C++ source(s) that use(s) it; this will be the lpName
argument given to the FindResource()
call, which you can form using MAKEINTRESOURCE(IDB_EMBEDEXE)
. Specify "BINARY"
(or L"BINARY"
for Unicode builds) for the lpType
argument.
赞:
#define IDB_EMBEDEXE 13232 // Or whatever suitable value you need
//...
// In the C++ code:
HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(IDB_EMBEDEXE), _TEXT("BINARY"));
HGLOBAL hGlobal = LoadResource(NULL, hResource);
size_t exeSiz = SizeofResource(NULL, hResource); // Size of the embedded data
void* exeBuf = LockResource(hGlobal); // usable pointer to that data
// You can now write the buffer to disk using "exeBuf" and "exeSiz"
然后,指定的可执行文件将被完全嵌入(作为二进制文件)资源到您的 build 可执行文件中,并可以按照引用的文章中的说明将其提取,写入磁盘并执行.
The specified executable file will then be completely embedded (as a binary) resource in your built executable, and can be extracted, written to disk and executed as described in the article you quote.
这篇关于将.exe文件嵌入C ++程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!