Solved

C++ .NET using a managed class library

Posted on 2004-09-08
7
300 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
  • 3
  • 2
7 Comments
 
LVL 19

Expert Comment

by:drichards
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 19

Expert Comment

by:drichards
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 250 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now