如何在我的引导程序exe中嵌入MSI
How to embed an MSI in my bootstrapper exe
我为我的应用程序编写了一个C++安装程序引导程序(这是一个硬性要求,因为我需要完全控制文件的格式)。现在我正试图找出如何用最少的开销将MSI嵌入到引导程序EXE中。我所有的搜索都向我展示了相反的情况(如何在MSI中嵌入EXE)。
这个安装程序必须脱机工作,所以我不能让引导程序简单地下载文件。
我当然可以将它作为字节流嵌入到我的源文件中,但这会导致文件大小的大量开销。
将数据作为资源嵌入二进制文件并提取它是很常见的
http://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource
http://www.codeproject.com/Articles/4221/Adding-and-extracting-binary-resources
IIRC,编译器将在构建时通过正确的调整嵌入数据,所以你只需要提取它。
下面是一个如何提取数据的示例。
#include "stdafx.h"
#include "resource.h"
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <Windows.h>
#include <shellapi.h>
using namespace std;
void extract_bin_resource(std::wstring strCustomResName, int nResourceId, std::string strOutputPath)
{
HGLOBAL hResourceLoaded; // handle to loaded resource
HRSRC hRes; // handle/ptr to res. info.
char *lpResLock; // pointer to resource data
DWORD dwSizeRes;
std::string strOutputLocation;
std::string strAppLocation;
hRes = FindResource(NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str());
hResourceLoaded = LoadResource(NULL, hRes);
lpResLock = (char *)LockResource(hResourceLoaded);
dwSizeRes = SizeofResource(NULL, hRes);
std::ofstream outputFile(strOutputPath.c_str(), std::ios::binary);
outputFile.write((const char *)lpResLock, dwSizeRes);
outputFile.close();
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Extract the bundled installer
extract_bin_resource(L"MSI", IDR_MSI1, "installer.msi");
// Execute the installer
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = L"msiexec.exe";
ShExecInfo.lpParameters = L"/i installer.msi";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
// Delete the installer
DeleteFile(L"installer.msi");
return 0;
}
相关文章:
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 终端不会为C++文件创建.exe文件吗
- 在Windows中以.exe的形式运行c++
- Visual Studio mkl_link_tool.exe链接错误
- PrintWindow函数在notepad.exe中出错
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 获取当前正在运行的 exe 名称(不是路径)
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 1d.exe找不到SFML文件?
- VI工作室代码错误无法打开输出文件主.exe
- 如何为每个客户生成自定义 exe
- 使用参数在 C# 的C++上运行.exe
- 在 ******.exe 中0x00000000772CA267 (ntdll.dll) 处未处理的异常:0xC0000
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 解析引用 |exe/dll vs lib
- Q没有管理权限的 exe 无法启动维护工具
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- Eclipse 没有在 Windows (C++) 中编译.exe
- 如何在我的引导程序exe中嵌入MSI