G
G
German2018-07-14 00:21:59
C++ / C#
German, 2018-07-14 00:21:59

How to get the hmodule of a resource?

In general, I'm trying to unload a jpg image from the resources of my program of my program, it is stored not in a dll, but in an exe.
Here is the code

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "resource.h"

using namespace std;

int main()
{
  //hmodule
  HRSRC jpg_resource = FindResource(NULL, (LPCWSTR)IDR_JPG1, L"JPG");
  if (!jpg_resource)
  {
    DWORD err;
    GetLastError();
  }
  cout << "HRSRC = " << jpg_resource << "\n";
  HGLOBAL load_resource = LoadResource(NULL, jpg_resource);
  if (!load_resource)
  {
    DWORD err;
    GetLastError();
  }
  cout << "HGLOBAL = " << load_resource << "\n";
  HANDLE load_image = LoadImage(NULL, L"from_exe.jpg", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  if (!load_image)
  {
    DWORD err;
    GetLastError();
  }
  cout << "HANDLE = " << load_image << "\n"; //всё время выводил 0
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-07-14
@res2001

I pulled from my old project calls to read the resource.
True, my resources were textual, but the essence of this does not change.

HMODULE hModule = GetModuleHandle(NULL);
HRSRC hResInfo = FindResourceA(hModule, MAKEINTRESOURCEA(IDR_README1), MAKEINTRESOURCEA(READMETXT));
HGLOBAL hResource = LoadResource(hModule, hResInfo);
DWORD nSize = SizeofResource(hModule, hResInfo);
LPVOID resource = LockResource(hResource);

After LockResource you get a pointer to the byte array of your resource, then do whatever you want with it.
In the example IDR_README1 and READMETXT is the resource identifier from the rc file.
The calls are arranged in real order, as they are in the project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question