Ok, thanks, I will give that a try. I have one problem with your implementation though... In your Create(); you use a char pointer to define the actual plugin. That's all fine and all, but in the final usage of this system, the app will have no clue as to what the plugin's name will be. I intend this to be a program that scans a directory and loads any plugins it finds, discarding the ones that aren't compatible. The only thing the app will know is the interface, and how to use that interface. I'll implement your version, and see what happens, thanks!
EDIT: Unfortunately, either I am doing something wrong, or you did. Heres my code for the app:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <windows.h>
#include <iostream>
class IAdd
{
public:
virtual int Add(int, int) = 0;
};
typedef IAdd* FuncCreate(const char*);
typedef void FuncDestroy(IAdd*);
int main(int argc, char* argv[])
{
HINSTANCE h = LoadLibrary("add.dll");
if (h)
{
FuncCreate* Create = (FuncCreate*)GetProcAddress(h, "Create");
FuncDestroy* Destroy = (FuncDestroy*)GetProcAddress(h, "Destroy");
if (Create)
{
if (Destroy)
{
if (IAdd* pAdd = Create("CAdd"))
{
std::cout << pAdd->Add(8, 11) << std::endl;
Destroy(pAdd);
}
else
{
std::cout << "Could not create object" << std::endl;
}
}
else
{
std::cout << "Bad implementation: Destroy" << std::endl;
}
}
else
{
std::cout << "Bad implementation: Create" << std::endl;
}
FreeLibrary(h);
}
else
{
std::cout << "Could not load library" << std::endl;
}
return 0;
}
|
The DLL is exactly what you have posted above.
Basically, I get "Bad implementation: Destroy" when I run the program. I inserted the comments when I saw that nothing was spat out on the console. I'll mess around and see what happens.
EDIT EDIT: I figured it out: You posted a spelling mistake in the Add.dll class. You entered Destory instead of Destroy :)