Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can i use a dll in VC++ which is created in c#.Please give some idea.

Thanks,
Laje.
Posted

My suggestion is to use a wrapper dll in C++/CLI. The advantage of C++/CLI is you can use both managed and unmanaged (native) code:

- In visual studion create a new project (Project type = Visual C++, CLR. Class Library).
- Name it "wrapper.dll" for example
- Add a reference to your C# dll, so you you can access all its content.
- Then write a few functions in native C/C++ and export them (you might need to rewrite a few structures as well).
- Once you compile wrapper.dll, you can use it from an unmanaged application.

I did this once with Ionic.Zip dll. This is a very easy to use and free library for zipping/unzipping files, and I wanted to use it from native code. I wrote a wrapper and it works nicely :)
 
Share this answer
 
Comments
Nish Nishant 7-Feb-11 9:09am    
Voted 5, proposed as answer.
Sergey Alexandrovich Kryukov 7-Feb-11 20:37pm    
This is a good way, my 5 (much better that COM). There is another, little known method. CLI actually allows exporting methods from .NET Assembly to unmanaged. Please see my answer; it references my old answer.
--SA
Olivier Levrey 8-Feb-11 15:16pm    
I had a look to your older answer and was very surprised (and impressed as well by the way): I really didn't know it was possible to export methods that way. I admit I don't have so much knowledge about .NET framework, MSIL and so on... Disassembing, editing IL code, then recompiling... uhuhu sounds scary to me! ;)
Olivier has already answered this with the best possible approach. Here's a free chapter from my book that will possibly help you with taking that approach:

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf[^]

Take a look at the section 4.2.1 Accessing a managed library from native code. It covers two approaches, the first one uses CCW, while the second one uses C++/CLI based interop (what Olivier mentioned above).
 
Share this answer
 
Comments
Espen Harlinn 7-Feb-11 14:34pm    
Not many can put such weight behind their answers :)
Nish Nishant 7-Feb-11 14:41pm    
Thanks Espen :-)
Sergey Alexandrovich Kryukov 7-Feb-11 20:40pm    
Hi Epsen, remember my old answer? It can work again here.
--SA
Sergey Alexandrovich Kryukov 7-Feb-11 20:40pm    
The approach is good, my 5.
Do you know the very different approach based on direct exporting of .NET assembly method to unmanaged. Please see my alternative answer, referencing my old answer.
--SA
Nish Nishant 8-Feb-11 8:30am    
Yeah, I am aware of that (have read a few articles on that technique). I would not recommend it for any non-trivial managed API though.
C++/CLI is a powerful thing; I like the answers based on it.
There is another, little know method based on direct exporting of the method from .NET assembly to unmanaged. Many though it was impossible, but in fact CLI standard allows it. This method makes the solution very smooth, and knowledge of C++ or C++/CLI is not required.

I already gave comprehensive information and references in my old answer:

Please see: loading C# DLL in MFC[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900