views:

497

answers:

4
+3  A: 

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
There must be a typelib-import way of doing this, no?
xtofl
If you were dealing with COM objects, than you could use a typelib. As Reed Copsey suggested below, that is an option.You indicated in a comment below that you can't switch to CLR because you "must use native code". These options are not exclusive. Microsoft specifically designed C++ as the language to use when you needed to get managed and unmanaged code talking together.
John Fisher
A: 

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.

Reed Copsey
Thanks.I can't switch to cls, I must use native code.about your options:1-I have the DLL already as a COM but I want to find another way to do that.2 and 3- How they work?
Ubalo
Your comment doesn't indicate whether you clearly understood the clr suggestion. You can't turn on the /CLR switch? Is this just too difficult?If you turn on /clr, you are mixing native with managed. It's not a complete switch over.
John Fisher
My problem is a client requirement, it should not be /CLR, it just be native. It is not my choice.
Ubalo
@Ubalo: I'll edit for details
Reed Copsey
Thank you I will try both.
Ubalo
+1  A: 

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.

R N
interop is dirt slow but far better than clr
Patrick
A: 

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.

Frank