Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C++ Windows Forms Application. I want this application to be able to use various custom modules contained in "C++ Class Library" (dll) files, that will all have the same public function calls in them but will do different things. The dll may also have other support functions that the dll uses, but are private. These private functions will change from one dll to another. The program will not know what dlls are available, it'll scan a folder and load in whatever it finds on startup.

I have the class library concept working when adding the DLL as a reference in the main app... and instantiating/using it like this simplified example:

C++ Class Library: compiles to dll:
using namespace System;
public ref class Module_Test {
	public:
		Module_Test(void) { a = 500; }
	private:
		int a;
 
	public: int GetNumber() { return a; }
};

C++ Windows Forms Application excerpt:
Module_Test md1;
MessageBox::Show(String::Format("{0}", md1.GetNumber()), "This will display the number 500", MessageBoxButtons::OK, MessageBoxIcon::Information);

So, to make it work as intended, I believe that I need to use LoadLibrary() and GetProcAddress(), and while LoadLibrary(L"Module_Test.dll") works... I'm not quite sure where to go from there. I can't seem to get GetProcAddress() to find the GetNumber() test function, nor do I know how to cast it or instantiate it to be able to use the whole class as I do above via the use of a reference.

I've googled just about every combination of words I can think of, no luck. I've found lots of simialr examples, but they're often not applicable because they're different kinds of code... unmanaged, older c++ code dll, etc. I can't find any newer examples that work with with the Visual Studio 2010 project types I'm using here.

Thoughts?
Posted
Updated 18-Jan-12 17:18pm
v2

In these cases, we use functions which behaves like class factories.

Just have a exported (global) function (using__declspec(dllexport) ) in your dll and let this function return object/pointer of Module_Test class.

You can use GetProcAddress to get this dll global function and once you get its address then invoke it to get Module_Test object/pointer.

With this Module_Test object/pointer, you can call it's functions in the usual way.

Hope this answers your problem.
 
Share this answer
 
I solved this using Reflection
 
Share this answer
 
Comments
Lakamraju Raghuram 25-Jan-12 12:01pm    
Can you give the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900