Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NOMINMAX
- #define UNICODE
- #define _UNICODE
- #include <fstream>
- #include <string>
- #include <string_view>
- #include <iostream>
- #include <io.h>
- #include <fcntl.h>
- #include <Windows.h>
- using namespace std::literals;
- int ShowUsage(const wchar_t* program) {
- std::wcerr << L"dmpres v0.1.0"sv << std::endl;
- std::wcerr << L"Copyright (c) 2019 SegaraRai"sv << std::endl;
- std::wcerr << std::endl;
- std::wcerr << L"usage: infile resource_name outfile"sv << std::endl;
- std::wcerr << std::endl;
- std::wcerr << L"set outfile to \"-\" to output to stdout"sv << std::endl;
- std::wcerr << std::endl;
- return 2;
- }
- int wmain(int argc, wchar_t* argv[]) {
- if (argc != 4) {
- return ShowUsage(argv[0]);
- }
- const std::wstring inFile(argv[1]);
- const std::wstring resourceName(argv[2]);
- const std::wstring outFile(argv[3]);
- const bool useStdout = outFile == L"-"sv;
- bool success = false;
- std::ofstream ofs;
- HMODULE hModule = NULL;
- HRSRC hResource = NULL;
- HGLOBAL hGlobal = NULL;
- hModule = LoadLibraryW(inFile.c_str());
- if (!hModule) {
- std::wcerr << L"cannot load module "sv << inFile << std::endl;
- goto end;
- }
- hResource = FindResourceW(hModule, resourceName.c_str(), RT_RCDATA);
- if (!hResource) {
- std::wcerr << L"resource "sv << resourceName << L" not found"sv << std::endl;
- goto end;
- }
- hGlobal = LoadResource(hModule, hResource);
- if (!hGlobal) {
- std::wcerr << L"cannot load resource"sv << std::endl;
- goto end;
- }
- const auto size = SizeofResource(hModule, hResource);
- // skip error check
- const auto data = LockResource(hGlobal);
- if (!data) {
- std::wcerr << L"cannot load resource data"sv << std::endl;
- goto end;
- }
- if (useStdout) {
- if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
- std::wcerr << L"_setmode failed"sv << std::endl;
- goto end;
- }
- }
- if (!useStdout) {
- ofs.open(outFile, std::ios::binary);
- if (ofs.fail() || ofs.bad()) {
- std::wcerr << L"failed to open outuput file"sv << std::endl;
- goto end;
- }
- }
- std::ostream* os = useStdout ? &std::cout : &ofs;
- os->write(static_cast<const char*>(data), static_cast<std::streamsize>(size));
- os->flush();
- success = true;
- end:
- // close file
- if (ofs.is_open()) {
- ofs.close();
- }
- // no need to free hGlobal
- hGlobal = NULL;
- // no need to free hResource
- hResource = NULL;
- // free hModule
- if (hModule) {
- FreeLibrary(hModule);
- hModule = NULL;
- }
- return success ? 0 : 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement