Archived

This topic is now archived and is closed to further replies.

gwihlidal

Cross DLL <-> Application Communication

Recommended Posts

I could use some help =/ Here''s how I designed my game engine (yes I know this is game related but the theory and design is general =) I have the EXE running the actual engine itself (rendering, entity, physics, etc..) and I made the individual game code into a DLL object (similar to how half life does it) Now, the DLL implements certain functions that the EXE knows to call when things happen (such as a keyboard button is depressed) The events are passing fine to the DLL from the EXE and it works absolutely perfect for what I want, except for one little thing. How can I call a function in the EXE. I know I need to inform the DLL the prototype and how everything works, but you can''t really export functions from an EXE. Any ideas? One idea I came up with was an exe that loads the game DLL and put the engine code also in a DLL and load that in the exe as well. Then I can get the loaded handle IDs from LoadLibrary and pass them into each DLL so that each DLL knows the correct instance of the other DLL so that memory is referenced at the same location (I think that''s how this works?) and then use the passed in handle ID to load the other DLL''s exported functions. Here''s a summary GameDLL --- MyGameFunction(); EngineDLL --- MyEngineFunction(); EXE --- Loads both GameDLL and EngineDLL and gets the handles from LoadLibrary Inform GameDLL the handle of EngineDLL, and vice versa Now the GameDLL loads MyEngineFunction() from EngineDLL using the supplied handle, and EngineDLL loads MyGameFunction from GameDLL using the supplied handle. Would this approach work? I REALLY could use some help on this topic. I understand for the most part how DLLs work, it''s just this little cross referencing that I don''t quite understand how to do cleanly. Thanks, ~Graham ---- while (your_engine >= my_engine) { my_engine++; }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Generally I think calling exe functions from the dll is not so good, but if you really have to I''d to it that way:
Make a struct (or class) with all the exe functions your dll needs to call (=function pointers). Have some kind of init function in your dll that takes this srtuct as a parameter. Then when you need to call an exe function call the function in the struct.
You could also just do this with one single function that takes an int as parameter that runs the function according to the parameter.

Share this post


Link to post
Share on other sites
Oh so that would work, excellent.

Thank you,

Trust me, I have designed my engine really well so that if this idea works it''ll be very clean to mod for.

~Graham



----
while (your_engine >= my_engine)
{
my_engine++;
}

Share this post


Link to post
Share on other sites
Calling an Exported Function in an EXE from Within a DLL

In the EXE:
// Do exactly as you would export a DLL...
extern "C"
{
EXPORT void ExeFn(char * lpszMessage)
{
MessageBox(NULL,lpszMessage,"From Exe",MB_OK);
}
}

In the DLL:
...
// Get the handle of the EXE that loaded us.

FnPtrT FnPtr = (FnPtrT)::GetProcAddress(GetModuleHandle(NULL),
"ExeFn");

www.codeguru.com/dll/ExportingFromExe.html

[edited by - Metro_Mystery on October 8, 2003 9:29:33 AM]

Share this post


Link to post
Share on other sites