How do I embed binary files into a program to view them in Visual Studio's C++ development?

Asked 2 years ago, Updated 2 years ago, 50 views

Visual Studio's C++ development is looking for ways to embed binary files into the program and browse through the program.

For C and C++ development in gcc, I think it is possible to embed binary files into the program as shown in the link below.

Want to embed data files in binary
c++ visual-studio


2022-09-29 22:49

2 Answers

Select a project in Solution Explorer and select Project - Add Resource... from the menu.[Add Resources] The dialog box appears, so if you select Custom..., the New Custom Resources dialog box appears.The resource type is optional, but it is recommended to set it to BINARY.Enter to display a binary editor, type the appropriate characters before saving (because 0 bytes cannot be saved).

In the above steps, project name 1.rc, binary1.bin, and resource.h have been added to the project, and the configuration to build them has been completed.Right-click Project Name 1.rc and select View Code F7 to display the source code.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// BINARY
//

IDR_BINARY1BINARY "binary1.bin"

It should be.This statement means that binary1.bin is stored as a resource in the executable file.Also resource.h is

#defineIDR_BINARY1101

Macro-defined as , so use this ID to access from within the program.
The API you use is FindResource(), LoadResource(), SizeResource.e().

autobin=FindResourceW(nullptr, MAKEINTRESOURCEW(IDR_BINARY1), L "BINARY");
autoptr=LoadResource(nullptr,bin);
auto size = Size of Resource (nullptr, bin);

The first argument must pass the module handle, but nullptr is acceptable for the executable itself. The second argument for FindResourceW() is specified using the macro value defined in resource.h.The third argument is the value entered in the New Custom Resource dialog box.

Doing so gives the ptr variable the binary1.bin is loaded into memory and the size is the size of binary1.bin.


2022-09-29 22:49

The quick way to come up with this is to create a script that generates a C language source like char data[]={...};, run the script before building, and build it with an auto-generated source.

You can also write resources to a built .exe file later. Consider using APIs such as BeginUpdateResource, UpdateResource, and EndUpdateResource.


2022-09-29 22:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.