2

I have a large binary file, ~1gb in size. I'd like to include this statically in a C++ executable compiled in Visual Studio 2019. The executable is built for Windows.

I'd like to access the binary file at runtime, but don't want to ship it alongside the application. So reading at runtime from a file is not an option.

I have seen the solution that just includes it as a byte array, but that is cumbersome, is there no better solution?

How would including it as a resource file look like?

PaperTsar
  • 961
  • 1
  • 9
  • 22
  • You can include binary resources using the .res file. The program can be either a Windows program or command line program. However you will need to amend the project for the command line program to invoke the rc compiler and the linker settings to link the compiled .res file. – Richard Critten Nov 01 '22 at 14:19
  • 2
    No idea how large a resource can be (and the startup time of your exe may suffer a lot). [Duplicate : adding-a-binary-file-to-resource-in-visual-studio](https://stackoverflow.com/questions/16592865/adding-a-binary-file-to-resource-in-visual-studio) – Pepijn Kramer Nov 01 '22 at 14:19
  • 1
    Maybe not possible https://stackoverflow.com/questions/67407062/visual-studio-embed-large-resource-file-almost-4gb – Alan Birtles Nov 01 '22 at 14:19
  • Why is having two files so much of an issue? I think havin a seperate data file is preferable over the one executable approach. Does all the data have to be available all the time? – Pepijn Kramer Nov 01 '22 at 14:21
  • If it's a Windows EXE, you could potentially put it into a resource, although I'm not sure if something this big is supported. A simple approach would be how self-extracting archives are made -- you just append the data to the executable, and provide some mechanism to find its beginning in the combined file (if you know the size at compile time, then it's a trivial seek from the end of file). – Dan Mašek Nov 01 '22 at 14:25
  • just write a separate program that generates a .cpp file that stores the data in an array. – jwezorek Nov 01 '22 at 14:49
  • @AlanBirtles The post you link to says that the maximum total size of an exe file is 4GB (even in 64 bit mode). Since the OPs file is "only" 1GB, that should work. – PMF Nov 01 '22 at 15:25
  • Visual Studio resource `import` can embed resources into exe. Maybe resouce can't exceed 1G? I haven't had a chance to test it. – Minxin Yu - MSFT Nov 02 '22 at 07:00

1 Answers1

3

Can be done with Resource files. Right click project > Add > Resource File > Import > Select file > Choose a resource type name freely.

#include <Windows.h>
#include "resource.h"
... 
# IDR_SOMETHING is the resource identifier, can be found in the autogenerated resource.h
HRSRC res = FindResource(NULL, MAKEINTRESOURCE(IDR_SOMETHING), "Res type name you choose");
DWORD size = SizeofResource(NULL, res);
HGLOBAL data = LoadResource(NULL, res);

data will be a simple pointer to start of memory where the resource is located.

PaperTsar
  • 961
  • 1
  • 9
  • 22