c++ - 如何使用 C++ 输出可执行文件

标签 c++ windows visual-c++ mfc

我想要一个可以将另一个程序写入磁盘的程序。我想方法是使用标准二进制写入文件。我的问题是我不知道如何将其他可执行文件放入我的“安装”程序的源代码中。我不想在我的安装程序旁边有任何额外的文件。如果需要使用标准 C++ 以外的东西,我想为此使用 mfc api

最佳答案

编辑 *.rc 资源文件并添加以下行:

1 RCDATA "C:\\my disk\\source.exe"

“source.exe” 将包含在您的程序中。您可以对任何文件执行此操作。

1 在上例中用作 ID。您可以使用任何唯一标识符。

在运行时提取数据:

bool copy()
{
    HINSTANCE hinst = AfxGetInstanceHandle(); //or just NULL
    HRSRC hrsrc = FindResource(hinst, MAKEINTRESOURCE(1), RT_RCDATA);
    if(hrsrc)
    {
        auto hglobal = LoadResource(hinst, hrsrc);
        auto data = LockResource(hglobal);
        auto datasize = SizeofResource(hinst, hrsrc);

        CFile file;
        if(file.Open(L"c:\\target\\output.exe", CFile::modeWrite | CFile::modeCreate))
        {
            file.Write(data, datasize);
            file.Close();
            return true;
        }
        else
        {
            TRACE("cannot open file\n");
        }
    }
    else
    {
        TRACE("resource not found\n");
    }

    return false;
}

关于c++ - 如何使用 C++ 输出可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113993/

相关文章:

c++ - 返回 char*/Visual Studio 调试器异常

c++ - 链接器错误 : already defined in my project when I don't define it at all

windows - 如何在 Windows 上获取 cpu 缓存关联设置

visual-c++ - 如何从 C++ 代码访问 $(SolutionDir) 宏

c++ - visual c++中的strtok问题

java - Titanium Studio - Java 返回错误 13

c++ - 如何在平行 View 中保持良好的纵横比

c++ - 想要在 C 中使用 while 创建菱形

c++ - '.' 标记前缺少模板参数

windows - git install LAN 在 ubuntu 服务器和 windows LAN 上