Skip to content

Instantly share code, notes, and snippets.

@juniorjacob
Last active July 2, 2021 09:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juniorjacob/8e1c79ad85ff49dbfdf1a9bfbf7e4105 to your computer and use it in GitHub Desktop.
Save juniorjacob/8e1c79ad85ff49dbfdf1a9bfbf7e4105 to your computer and use it in GitHub Desktop.
Converts any file's byte's into a Resource Header file. File -> Resource
#include <vector>
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <fstream>
std::string file_path = "";
std::string file_endpath = "";
std::string namespace_name = "example_namespace";
std::string namespace_resource_name = "resources";
int linebreak_number = 40;
bool ReadFileToMemory(const std::string& file_path, std::vector<uint8_t>* out_buffer)
{
std::ifstream file_ifstream(file_path, std::ios::binary);
if (!file_ifstream)
return false;
out_buffer->assign((std::istreambuf_iterator<char>(file_ifstream)), std::istreambuf_iterator<char>());
file_ifstream.close();
return true;
}
int main(int argc, char* argv[])
{
std::vector<uint8_t> dll_contents;
ReadFileToMemory(file_path, &dll_contents);
FILE* file = fopen(file_endpath.c_str(), "w");
fprintf(file, "#pragma once\n#include <stdint.h>\n\nnamespace %s {\n\tuint8_t %s[] = {\n\t\t", namespace_name.c_str(), namespace_resource_name.c_str());
for (size_t i = 0; i < dll_contents.size(); i++) {
uint8_t byte = dll_contents.at(i);
if (i == dll_contents.size() - 1) {
fprintf(file, "0x%02X", byte);
}
else {
fprintf(file, "0x%02X, ", byte);
if (((i + 1) % linebreak_number) == 0)
fprintf(file, "\n\t\t");
}
}
fprintf(file, "\n\t};\n};");
fclose(file);
system("pause");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment