Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #define NOMINMAX
  2. #define UNICODE
  3. #define _UNICODE
  4.  
  5. #include <fstream>
  6. #include <string>
  7. #include <string_view>
  8. #include <iostream>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include <Windows.h>
  12.  
  13. using namespace std::literals;
  14.  
  15.  
  16. int ShowUsage(const wchar_t* program) {
  17. std::wcerr << L"dmpres v0.1.0"sv << std::endl;
  18. std::wcerr << L"Copyright (c) 2019 SegaraRai"sv << std::endl;
  19. std::wcerr << std::endl;
  20. std::wcerr << L"usage: infile resource_name outfile"sv << std::endl;
  21. std::wcerr << std::endl;
  22. std::wcerr << L"set outfile to \"-\" to output to stdout"sv << std::endl;
  23. std::wcerr << std::endl;
  24. return 2;
  25. }
  26.  
  27.  
  28. int wmain(int argc, wchar_t* argv[]) {
  29. if (argc != 4) {
  30. return ShowUsage(argv[0]);
  31. }
  32.  
  33. const std::wstring inFile(argv[1]);
  34. const std::wstring resourceName(argv[2]);
  35. const std::wstring outFile(argv[3]);
  36.  
  37. const bool useStdout = outFile == L"-"sv;
  38.  
  39.  
  40. bool success = false;
  41.  
  42. std::ofstream ofs;
  43.  
  44. HMODULE hModule = NULL;
  45. HRSRC hResource = NULL;
  46. HGLOBAL hGlobal = NULL;
  47.  
  48. hModule = LoadLibraryW(inFile.c_str());
  49. if (!hModule) {
  50. std::wcerr << L"cannot load module "sv << inFile << std::endl;
  51. goto end;
  52. }
  53.  
  54. hResource = FindResourceW(hModule, resourceName.c_str(), RT_RCDATA);
  55. if (!hResource) {
  56. std::wcerr << L"resource "sv << resourceName << L" not found"sv << std::endl;
  57. goto end;
  58. }
  59.  
  60. hGlobal = LoadResource(hModule, hResource);
  61. if (!hGlobal) {
  62. std::wcerr << L"cannot load resource"sv << std::endl;
  63. goto end;
  64. }
  65.  
  66. const auto size = SizeofResource(hModule, hResource);
  67. // skip error check
  68.  
  69. const auto data = LockResource(hGlobal);
  70. if (!data) {
  71. std::wcerr << L"cannot load resource data"sv << std::endl;
  72. goto end;
  73. }
  74.  
  75. if (useStdout) {
  76. if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
  77. std::wcerr << L"_setmode failed"sv << std::endl;
  78. goto end;
  79. }
  80. }
  81.  
  82. if (!useStdout) {
  83. ofs.open(outFile, std::ios::binary);
  84. if (ofs.fail() || ofs.bad()) {
  85. std::wcerr << L"failed to open outuput file"sv << std::endl;
  86. goto end;
  87. }
  88. }
  89.  
  90. std::ostream* os = useStdout ? &std::cout : &ofs;
  91.  
  92. os->write(static_cast<const char*>(data), static_cast<std::streamsize>(size));
  93. os->flush();
  94.  
  95. success = true;
  96.  
  97. end:
  98. // close file
  99. if (ofs.is_open()) {
  100. ofs.close();
  101. }
  102.  
  103. // no need to free hGlobal
  104. hGlobal = NULL;
  105.  
  106. // no need to free hResource
  107. hResource = NULL;
  108.  
  109. // free hModule
  110. if (hModule) {
  111. FreeLibrary(hModule);
  112. hModule = NULL;
  113. }
  114.  
  115. return success ? 0 : 1;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement