idanshmu
idanshmu

Reputation: 5251

Can a DLL load a resource from calling EXE?

Background

I have two C++ projects. One EXE and one DLL. The EXE loads the DLL and call its single exported function. Eventually, I will generate even more DLLs that will be loaded and run by the EXE

Motivation

EXE and all DLLs will use some resource. So, instead of adding the resource to all DLLs, I'd like to add it, once, to the EXE and wish the DLL to load the resource from the caller EXE.

Attempt

I already know how to load my own resource using: FindResource(), LoadResource(), LockResource(). All I'm missing is the hModule to the caller process.

In addition I've read Can you get the caller DLL or executable module from the callee

Question (TL;DR)

So, if my approch describbed above is the right one, then, How do I get an HMODULE to the caller module withing a DLL?

Upvotes: 0

Views: 1343

Answers (2)

idanshmu
idanshmu

Reputation: 5251

Bad understanding on how resource loading works led me to ask this.

I think that the following illustration will shed some light for those who are/were confused as me.

enter image description here

Upvotes: 1

GeorgeT
GeorgeT

Reputation: 504

You can call ::GetModuleHandle(NULL) to get the calling exe's handle and then pass it to FindResource or other relevant functions.

But, I would not recommend it because you hurt the module's reusability.

If your dll exports useful and probaly reusable functionality, and in the near future you need another executable referencing the same dll, you will have to duplicate the dll's resources in the second exe.

Upvotes: 3

Related Questions