简体   繁体   中英

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?

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.)

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM