Skip to main content
GameDev.net gamedev.net
🔒 Locked

DLL and plugin system

Started by Snatch Apr 27, 2004 at 12:21 PM 3 replies 1.2k views
Original Post
Snatch
Snatch
Hello everybody, i have a little question with DLL and plugin system. I have a DLL which is going to be a plugin of my application (if you somebody can help... ) this DLL contain init function: BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) and after some code that use function and object of my application (the .exe). But it can''t works because these function are undefined when linking the DLL. What trick can i use so that DLL should be able to use function of the .EXE ? I have some idea: 1) change the .exe in a .lib and link it with the dll (and create another .exe with just few code that call function of the .lib). But it makes a lot of duplicate code. (the .lib is in the .exe and in all the .lib). 2) not using a .lib but another .dll which contain the application, but if my application wants to load the plugin, the plugin is going to load the other .dll which contain the application ? or is there some kind of protection in the loading of dll that disallow people to load more than one time a same dll ? 3) use some sort of _dllspec (dllimport) but i am not used with these. I would be very happy if somebody could help me because i am feeling lost. PS: excuse for my english it is not my natural language.
SiCrane
SiCrane
If I understand what you are asking, there are a couple of ways to do this. By far the easiest is to give the the DLL functions that accept function pointers as parameters. The executable can call this functions with its own functions or possible even functions from other DLLs in order to customize the DLL''s behaviour.
Evil Steve
Evil Steve
If i understand correctly, you want the EXE to be able to use the functions in the DLL and the DLL to be able to use function in the EXE?

You can declare functions in an EXE as __declspec(dllexport), and then use GetProcAddress() on the handle of the EXE, to get a function pointer. I'm not sure how good that is though, since it seems a little hacky to me.

Personally, i'd have a second DLL of common functions, and link it with both the EXE and DLL.

About the first point:

// In the exe:

extern "C" {
__declspec(dllexport) void SomeFunction();
};

void SomeFunction()
{
MessageBox(NULL,"Hello!","Test",MB_OK);
}


// In the DLL:

typedef void (*LPSomeFunc)();
void CallSomeFunction(HMODULE hModExe)
{
LPSomeFunc pfn = (LPSomeFunc)GetProcAddress(hModExe,"SomeFunction");
pfn();
}

// And then to call the function in the Dll (from inside the exe):

CallSomeFunction(GetModuleHandle(NULL));


Edit: Actually, SiCranes idea sounds far better...

[edited by - Evil Steve on April 27, 2004 1:43:20 PM]
darkbard
darkbard
Hi, this is not an answer to the question, but I''d like to know how to implement a simple DLL plugin system using C++. there''s anyone who can help me?
Zidane
Zidane
quote:
Original post by darkbard
Hi, this is not an answer to the question, but I''d like to know how to implement a simple DLL plugin system using C++. there''s anyone who can help me?


Ok, Ill try to help but this will be breif im afraid.

Firstly you mention C++ so I assume you want it to be an OOP plugin based system. Correct me if I am wrong.

Now AFAIK what we are going to do is similar to how COM works (very very very simplified)

Firstly we design the plugin interface, for this project it is a compiler plugin for a fictional editor.

Now our interface must describe the full functionality that our main application will expect from the plugin. I will make this simple for clarity.

IFictionalEditorPlugin.h:



class IFictionalEditorPlugin
{
virtual int compile(char* pSource_)=0;
};



WRITING A PLUGIN

Now our editor is to support C++ compiling, so we will write a DLL for it, firstly we must implement the IFictionalEditorPlugin
and provide method implementations (inside the dll)

CPPFictionalEditorPlugin.h



class CPPFictionaEditorPlugin : IFictionalEditorPlugin
{
virtual int compile(char* pSource_){

....




Now we must write a function that is exported from the DLL that client and plugin agree on (both parameters, return and the name)



__declspec(dllexport) IFictionalEditorPlugin* getPluginObject()
{
return (IFictionalEditorPlugin*) new CPPFictionalEditorPlugin();
}



Using the plugin

When the client wishes to use the plugin (decided during rutime somehow). The client must take the following steps:

1) Load the dll and aquire its handle via LoadLibrary
2) Aquire the handle to the object creation function, the name of which and synemtics were agreed upon between plugin and client. This will use GetProcAddress.
3) Call this function pointer and you have a handle to the object that the plugin developer wrote.


The most important part is that you cannot export class from DLLs at runtime because the client will not understand the class type, as the client will not have been compiled with the nessery files to (well you can using import libs and headers - This is known as static binding, not really to usefull for a plugin system) in this case understand the CPP compiler plugin. So what we are doing is designing a standard interface that is set by the client and forcing plugins to implement it. This is using OOP to allow a system to change its behaviours dynamically.

Ok some of this will not make sense im sure, but it is how I developed it for my editor.













Programming + Metalica + Geek = Zidane

"Welcome to Nearly Banned. If you do it again, you''ll be banned." - Superpig

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.