Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a SDK .lib which is written on native C++. I want to write a C# application to use the functions available in the native C++ library.
As I found in the net The solution is in the following way, correct me If I am wrong

1. Need to write a wrapper function for that .lib file using C++/CLI file and creat a DLL.

2. Refer this DLL from C# application & achieve the result.

Please confirm the above way is right ? If yes can we write a wrapper through VC++ / do we need to write only through C++/CLI ?

Need some sample on writing a wrapper function on C++/CLI .

Thanks,
Pattabi
Posted

rprtech wrote:
1. Need to write a wrapper function for that .lib file using C++/CLI file and creat a DLL.


This is true. You will need to create a new managed class library in C++ (New > Project > Visual C++ > CLR > Class Library). In this class library you will need to add a reference to the .lib file. Finally, you will have to define and create a "public ref class" that wraps the functionality of the unmanaged code. Don't forget to add the include path of the header file that defines the .lib function.

rprtech wrote:
2. Refer this DLL from C# application & achieve the result.


To reference the C++/CLI class from C# you can add a reference the C++ project. I also like to have both projects in the same solution. You can also build the C++ DLL (class library) and refernce the DLL directly.

rprtech wrote:
Need some sample on writing a wrapper function on C++/CLI .


Just create a public method (static if you like) in your C++ ref class and in the body of the function call to the unmanaged code.
 
Share this answer
 
Comments
Batul Saifee 22-Jun-10 10:55am    
My native project references a lot of libs. If I just add a reference to the native .lib, it still does not work. I have to put additional include directories as well as additional dependencies. Is that the correct way to do it?
Yes. You have to write an unmanaged DLL, and then use InteropServices in your managed assembly to get to the DLLs functionality.
 
Share this answer
 
Hi,

Thanks for your reply. Could you please give sample code/ link for it ?
 
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