i want use dll made in c#(visual studio 2008) in c++ project(visual studio 2003). how to do that ? please heeelp
-
2possible duplicate of [How to use c# Dll in vc++??](http://stackoverflow.com/questions/980808/how-to-use-c-dll-in-vc) – Hans Passant Sep 26 '10 at 21:41
-
1possible duplicate of [Is it possible to use a DLL created using C# in an unmanaged VC++ application?](http://stackoverflow.com/questions/2873714/is-it-possible-to-use-a-dll-created-using-c-in-an-unmanaged-vc-application) – Dirk Vollmar Sep 26 '10 at 21:47
-
@0xA3: I'm curious, how would VC++ 2008 make things easier? Code compiled in VC 2003 can include COM components, and it can include any kind of native wrapper DLL, even if it's written in VC2008, right? – Niki Sep 26 '10 at 21:47
-
@nikie: As far as I know the VC++ /CLR option would require that both the C++ and the C# dll are using the same .NET runtime version. VS2003 is still targeting the old .NET 1.1. – Dirk Vollmar Sep 26 '10 at 21:51
-
@0xA3: True, but he could write the C++/CLR wrapper DLL in VC 2008, and include it in his existing VC2003 project, without upgrading his existing project. – Niki Sep 27 '10 at 07:30
6 Answers
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.
Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly. (All-In-One Code Framework Sample Code: CppHostCLR)
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)
Reverse PInvoke: the managed code call native passing a delegate the native code can call back. (All-In-One Code Framework Sample Code: CSPInvokeDll)
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)

- 172,527
- 53
- 255
- 316
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>

- 11
- 1
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.

- 1,023,142
- 271
- 3,287
- 2,928
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.

- 11,672
- 5
- 42
- 59
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.

- 15,662
- 5
- 48
- 74
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.

- 6,620
- 11
- 47
- 72