通过C ++/CLI包装函数从C#引用非托管.LIB文件 [英] Referring unmanaged .LIB file from C# through C++/CLI wrapper function

查看:98
本文介绍了通过C ++/CLI包装函数从C#引用非托管.LIB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个使用本机C ++编写的SDK .lib.我想编写一个C#应用程序以使用本机C ++库中提供的功能.
正如我在网上找到的解决方法是通过以下方式纠正我,如果我错了

1.需要使用C ++/CLI文件为该.lib文件编写包装函数,并创建一个DLL.

2.从C#应用程序中引用此DLL并获得结果.

请确认以上方式对吗?如果可以,我们可以通过VC ++编写包装器吗?是否仅需要通过C ++/CLI编写?

需要一些在C ++/CLI上编写包装函数的示例.

谢谢,
Pattabi

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

推荐答案

rprtech写道:
rprtech wrote:

1.需要使用C ++/CLI文件为该.lib文件编写包装函数,并创建一个DLL.

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



这是真实的.您将需要在C ++中创建一个新的托管类库(新建>项目> Visual C ++> CLR>类库").在此类库中,您将需要添加对.lib文件的引用.最后,您将必须定义并创建一个公共引用类",以包装非托管代码的功能.不要忘记添加定义.lib函数的头文件的包含路径.



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写道:
rprtech wrote:

2.从C#应用程序&中引用此DLL.达到结果.

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



要从C#引用C ++/CLI类,可以添加对C ++项目的引用.我也喜欢将两个项目都放在同一个解决方案中.您还可以构建C ++ DLL(类库)并直接引用DLL.



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写道:
rprtech wrote:

需要一些在C ++/CLI上编写包装函数的示例.

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



只需在C ++ ref类和对非托管代码的函数调用主体中创建一个公共方法(如果愿意,可以使用静态方法).



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.


是的.您必须编写一个非托管DLL,然后在托管程序集中使用InteropServices才能获得DLLs功能.
Yes. You have to write an unmanaged DLL, and then use InteropServices in your managed assembly to get to the DLLs functionality.




感谢您的回复.您能否提供示例代码/链接?
Hi,

Thanks for your reply. Could you please give sample code/ link for it ?


这篇关于通过C ++/CLI包装函数从C#引用非托管.LIB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆