Ubalo
Ubalo

Reputation: 759

How C++ can import a DLL made in C#?

I have a DLL made in C#, this DLL contains some clases like Creator.

I need to load this DLL and use Creator class in C++ unmanaged,

so Is there some way to create that instance or must I load just the functions exposed?

I need something like this:


CreatorInstance->Init();

Is this posible?

Upvotes: 2

Views: 4392

Answers (4)

ALOToverflow
ALOToverflow

Reputation: 2699

First of all, it is possible and you do not "have" to use CLI or the /clr switch. Using the good old COM architecture you can do it pretty easily http://msdn.microsoft.com/en-us/library/zsfww439.aspx. Understanding the way COM works might be the biggest challenge here, but it's usefull once you know it.

Upvotes: 0

R N
R N

Reputation: 73

Here is an interesting article on how you should be able to accomplish this without using the /CLR option

http://www.codeproject.com/KB/cs/ManagedCOM.aspx

Works pretty well.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564781

John Fisher's approach using C++/CLI is by far the easiest means of handling this, but it is not the only means.

The other three options are:

1) Use COM interop to wrap the .NET class via COM

2) You can host the CLR in your native, unmanaged application, and call into it. For details, see this article.

3) You can host the Mono runtime, and use it to call your managed code. For details on this, see this page.

Option 2 and 3 are very similar, but IMO, 3 is easier than 2.

Upvotes: 2

John Fisher
John Fisher

Reputation: 22717

Most of what you need can be found here: http://msdn.microsoft.com/en-us/library/x0w2664k%28VS.80%29.aspx

Primarily, you need to learn about the /clr switch for C++ compilation. Then you need to understand the C++ extensions that Microsoft added to allow for mixed assemblies. (A C++ "pointer" to a managed class would use p^ instead of p*, and so on.)

Upvotes: 4

Related Questions