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?

8
  • 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. Commented Nov 1, 2022 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 Commented Nov 1, 2022 at 14:19
  • 1
    Maybe not possible stackoverflow.com/questions/67407062/… Commented Nov 1, 2022 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? Commented Nov 1, 2022 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). Commented Nov 1, 2022 at 14:25

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool! Consider clicking '✔' to mark it as the accepted answer. It will also help others to solve the similar issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.