开发者

using c# dll in project c++

i want use dll made in c#(visual studio 2008) in c++ project(visual studio 开发者_如何学Go2003). how to do that ? please heeelp


There is more than just COM interop, the MSDN FAQ also lists lesser known methods:

2.2 How do I call .NET assembly from native Visual C++?

There are basically four methods to call .NET assembly from native VC++ code. Microsoft All-In-One Code Framework has working examples that demonstrate the methods.

  1. Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly. (All-In-One Code Framework Sample Code: CppHostCLR)

  2. If the .NET assembly can be exposed as a COM component, native VC++ module can call into the .NET assembly through .NET – COM interop. (All-In-One Code Framework Sample Code: CppCOMClient)

  3. Reverse PInvoke: the managed code call native passing a delegate the native code can call back. (All-In-One Code Framework Sample Code: CSPInvokeDll)

  4. If the module containing native VC++ code is allowed to enable CLR, the native VC++ code can call .NET assembly directly through the “It Just Works”, or “IJW”, mechanism. (All-In-One Code Framework Sample Code: CppCLIWrapLib)


You cannot directly use .NET assemblies from unmanaged C++ code. One possible solution is to expose the assembly as COM object using the regasm.exe utility and then consume it from C++. Note that .NET types that need to be exposed as COM objects might need to be decorated with the [COMVisible(true)] attribute and would still require the .NET framework installed on the target computer running the C++ code.


There are many ways to do this.

Is the C# DLL a COM DLL? If so you can use the regular COM API/Specification to access it in your C++ program. There are many tutorials on making your C# DLL COM visible. It's not that difficult, a few compile switches and C# attributes basically.

Otherwise, can you compile your C++ project using the /clr compiler switch? If so you can use the Using Directive to import your C# DLL directly.


As Darin said, one way is via COM. The other two ways (I know of) are:

  • You can create a mixed-mode DLL in C++. That DLL can contain and call managed code, but it can also export unmanaged functions, like a normal C++ DLL. It can be included like any other C++ DLL
  • It's a lot harder, but you can also host the CLR. Google for "CLR hosting" to find details about the API or samples.


after setting the "Resolve #using References" (Project->C/C++->General) compiler option to the directory containing the assembly you want, then you put a line like this in your code.

#using <NiftyAssembly.dll>


Make library file [.Tlb ] file and then only we can use the dll's. You have to make .TLB (Type Library file) of your original c# DLL and use that.TLB library in the VC++ code. For that you have to register .TLB file on your pc as well as it is neccessary to import that .TLB file in your vc++ apllication. When you write C# DLL remember to use interfaces that would be implemented in the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜