Solved

C++ .NET using a managed class library

Posted on 2004-09-08
7
307 Views
Last Modified: 2013-12-03
Hi guys ...

I have written a C++ managed class library DLL and a simple app to use this managed DLL. In order to use this class I have started as follows :

Assembly *asm = Assembly::LoadFrom("D:\Test\tester.DLL");
Type *asmTypes[]  = asm->GetTypes();
for (int i = 0; i <= asmTypes->Count - 1; i++)
{
   if (asmTypes[i]->GetInterface("IRequiredObject") != NULL)
   {
      Object *TestObject = Activator::CreateInstance(asmTypes[i]);            
      IRequiredInterface *reqInt = dynamic_cast<IRequiredInterface *>(TestObject);

Note that IRequiredInterface was defined in a shared header file that both the DLL and the EXE share - Mistake 1 ...

Although the intellisence now gives me the methods in IRequiredInterface eg :
reqInt ->IRequiredMethod1();
The dynamic_cast operation fails at runtime and returns NULL.

I know that I can use late binding to export each of the methods 1 by 1 from the DLL :
MethodInfo *MyMethod1 = asmTypes[i]->GetMethod("IRequiredMethod1");
Object *tmpObj = MyMethod1->Invoke(TestObject , NULL);

The problem I face here is the number of methods that are required, multiplied by a selection of different DLL's makes late binding an ugly solution.

My question : After retrieving a class from a managed DLL, is it possible to instantiate it and then make method calls from it, without late-binding, so using it is almost like using a normal (local) class ?

Any help here is greatly appreciated ...
¬STeve
0
Comment
Question by:steve_hsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12010514
If you add a rerefence to the managed C++ dll in the other app, you can just create instances of objects from the dll.  Go to the project where you want to use the dll and right click on "References" in the Solution Explorer and say "Add Reference" and select the dll project.  Now you can just do (assuming the app is also managed C++):

   DLLProj::ClassName *pC = new DLLProj::ClassName(...);

where DLLProj is the namespace from the dll and ClassName is a class you implemented in the dll.  Then you will have intellisense and can use functions directly.  The managed C++ dll can also be referenced and used from other .NET languages - use syntax appropriate for the language.
0
 
LVL 2

Author Comment

by:steve_hsk
ID: 12011279
Hi, thanks for the post ...

In my hast to get the question out and find an answer I have missed the fact that this code forms part of a module manager for a plugin architecture, and subsequently it is not possible to reference the plugin DLL from the module manager. It must be loaded at runtime, and the plugin(s) may potentially be selected by the user from a file browser dialog.

However, as this can be done at design time AND the framework gives the ability to load a class in at runtime : Activator::CreateInstance(asmTypes[i]) ... surely there must be a way to use this class properly without late binding ???

Apologies for any confusion,
¬STeve
0
 
LVL 2

Author Comment

by:steve_hsk
ID: 12015510
Hi guys ...

I have managed to find the answer I was looking for in c# and used the same methods in C++ :
http://www.skeet.org.uk/csharp/plugin.html

The key problem was storing the interfaces in a shared header file. Although the source is the same the CLR recognises these interface objects in the EXE and the DLL as different binary objects, as they have been compiled as different times.

The solution, is to create a 2nd DLL and transfer the interfaces from the the shared header file, into this DLL. When this Interface DLL is used by the EXE and the PlugIn DLL, it is the same binary, the CLR recognises this, and the subsequent cast suceeds :
IRequiredInterface *reqInt = dynamic_cast<IRequiredInterface *>(TestObject);

So before reqInt was returned NULL by this cast, now the cast is successful and not only will intellisense so the list of class methods, but the calls to these methods also suceed :
reqInt ->IRequiredMethod1();

This method will allow for more complex interfaces (not always a good thing from a design perspective, but sometimes necessary), and very little coding when compared to late binding methods.

If there are no objections, I shall post a refund request in community support ?

Best regards,
¬STeve
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:drichards
ID: 12016699
No problem.

I discovered the same solution lat night by trial and error - no Internet in the hotel, though, so I couldn't post it.  Isn't it nice that it's so well documented by Microsoft?  I'll file this one away for future use as it's a bit unexpected coming from regular C++ development.

Good luck.
0
 
LVL 2

Author Comment

by:steve_hsk
ID: 12016781
Another documentation feux pas ... add it to the list of overlooked and poorly documented features :-)

Although class control is a logical step in the path of reflection, I am impressed with this solution. If and hopefully when I find the time, I feel a codeproject article coming on !

Thanks again for your efforts !

¬STeve
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 12056935
Closed, 250 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question