Matias Chara
Matias Chara

Reputation: 991

Using the same dll in another dll and a third codebase that runs the executable

So basically I am creating my own dll for use in a c# codebase, using c++ and a c-style layer for exporting functions. The issue is that both the c# part of the code (which actually runs and uses the dll) and my library itself both use another library (it's imgui, so lets call it such, even though it is irrelevant mostly) that runs on global state.

I initialize imgui to certain settings in my own dll and then use some of its functions in the c# code. Currently I am simply compiling the library into my dll and doing a c# binding myself that issues calls to imgui through my own dll. My question is whether I can use imgui as a dll for use both in my own dll and the main c# program in such a way that state is shared and the initialization step initializes the same code for both my own dll and the c# code.

That is, the c# code calls one of my functions in my own dll to initialize it, that function imports a dll (imgui), in the common directory for both my dll and the executable, and initializes IT. After this, the main c# program that imports both my dll and imgui then calls imgui functions to manipulate it's state. Will my dll and the main c# program be manipulating the same imgui state?

Upvotes: 0

Views: 98

Answers (1)

mikelegg
mikelegg

Reputation: 1327

Generally yes

I say 'generally' because there are some conditions:

Everything needs to be in the same process (you are not clear on this but I assume so)

Both callers need to be able to get some handle on the state. So for example, if the state is memory allocated on the heap, then both callers need to be able to get a pointer to it, or else it needs to be a global variable somewhere they can access, either directly or indirectly.

Really it is the same as if it was 3 different C# modules

(also adding the disclaimer to think if you really need to use the C++, but I'm sure you have your reasons)

Upvotes: 1

Related Questions