Q: A MFC DLL being called from c#

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hiya

I have a DLL written in VC++ MFC. It contains a class. I would like to be
able to create an instance of the class from within a C# application. I
believe the only way I can do this is to use COM? I'm afraid I'm not
familiar with this technology and would appreciate any advice on how to
proceed.

As an example of what I'm trying to do and to simplify matters, let us
assume that the class I'm trying to use from the DLL in the C# application
is something trivial like:

class CMFCMyClass
{
public:
CMFCMyClass();
~CMFCMyClass();
int getNumber()
{
return 42;
};
}

Can anybody tell me how to create the DLL, using COM technology, and then
create an instance of the class in C#.

Thanks in advance

Geoff
 
Geoff,

In this case, you have two options. Both require that you wrap the
object in some way to expose it in the manner you want to access it. The
best approach would be to create a managed wrapper in VC++, which would
create a managed class which accesses the unmanaged class and aggregates the
calls to the various properties/methods on the class. For more information
on how to do this, check out the article on the O'Reilly website titled
"Writing Managed Wrappers with Managed C++", located at (watch for line
wrap):

http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html

The other option is to wrap the MFC object ina COM object. To do that
would be a little more complicated, as you would have to define your
interface, then create the class factory, etc, etc (which you can do through
Active Template Language very easily, btw). All in all, it's better to go
the managed wrapper route.

Once you have the wrapper, you can just set a reference, and then use
your class from C#.

Hope this helps.
 
Back
Top