I've a c++ project. I admit that I'm a complete ZERO in c++. But still I need to write a c++.net wrapper so I could work with an unmanaged c++ library using it. So what I have: 1) unmanaged project's header files. 2) unmanaged project's libraries (.dll's and .lib's) 3) an empty C++.NET project which I plan to use as a wrapper for my c# application
我一个c++项目。我承认我在c++中完全是零。但是,我仍然需要编写一个c++.net包装器,这样我就可以使用它来使用非托管的c++库。所以我有:1)非托管项目的头文件。2)非托管项目的库(。dll和.lib) 3)一个空的c++。NET项目,我打算把它作为c#应用程序的包装
How can I start? I don't even know how to set a reference to an unmanaged library.
我如何开始?我甚至不知道如何设置对非托管库的引用。
S.O.S.
“求救信号”
2 个解决方案
#1
35
http://www.codeproject.com/KB/mcpp/quickcppcli.aspx#A8
http://www.codeproject.com/KB/mcpp/quickcppcli.aspx A8
This is general direction. You need to create C++/CLI Class Library project, add .NET class to it (StudentWrapper in this sample), create unmanaged class instance as managed class member, and wrap every unmanaged class function. Unmanaged library is added to C++/CLI project using linker dependencies list, and not as reference. In the Project - Properties - Linker open Additional Dependencies and add .lib name there.
这是大方向。您需要创建c++ /CLI类库项目,向其添加. net类(本示例中的StudentWrapper),以托管类成员的身份创建非托管类实例,并包装每个非托管类函数。非托管库使用链接器依赖列表添加到c++ /CLI项目中,而不是作为引用。在项目属性链接器中,打开附加的依赖项并在其中添加.lib名称。
Note: since we are talking about C++/CLI wrapper, no PInvoke! PInvoke is used to call exported functions (API), and not classes.
注意:由于我们讨论的是c++ /CLI包装,所以没有PInvoke。PInvoke用于调用导出函数(API),而不是类。
#2
8
You need to use p/invoke from .NET to talk to your unmanaged DLL.
您需要使用. net中的p/invoke来与非托管DLL进行对话。
Essentially you create a function header for each function you want to call in your unmanaged DLL, and tell .NET which DLL the function lives in, then just call that function like any other in your .NET wrapper.
本质上,您为每个想要在非托管DLL中调用的函数创建一个函数头,并告诉. net函数所在的DLL,然后像. net包装器中的其他函数一样调用该函数。
You shouldn't even need any C++ knowledge - as long as you know the function definition of the functions in your unmanaged DLL, and the correct datatypes.
您甚至不需要任何c++知识——只要您知道非托管DLL中的函数的函数定义,以及正确的数据类型。
#1
35
http://www.codeproject.com/KB/mcpp/quickcppcli.aspx#A8
http://www.codeproject.com/KB/mcpp/quickcppcli.aspx A8
This is general direction. You need to create C++/CLI Class Library project, add .NET class to it (StudentWrapper in this sample), create unmanaged class instance as managed class member, and wrap every unmanaged class function. Unmanaged library is added to C++/CLI project using linker dependencies list, and not as reference. In the Project - Properties - Linker open Additional Dependencies and add .lib name there.
这是大方向。您需要创建c++ /CLI类库项目,向其添加. net类(本示例中的StudentWrapper),以托管类成员的身份创建非托管类实例,并包装每个非托管类函数。非托管库使用链接器依赖列表添加到c++ /CLI项目中,而不是作为引用。在项目属性链接器中,打开附加的依赖项并在其中添加.lib名称。
Note: since we are talking about C++/CLI wrapper, no PInvoke! PInvoke is used to call exported functions (API), and not classes.
注意:由于我们讨论的是c++ /CLI包装,所以没有PInvoke。PInvoke用于调用导出函数(API),而不是类。
#2
8
You need to use p/invoke from .NET to talk to your unmanaged DLL.
您需要使用. net中的p/invoke来与非托管DLL进行对话。
Essentially you create a function header for each function you want to call in your unmanaged DLL, and tell .NET which DLL the function lives in, then just call that function like any other in your .NET wrapper.
本质上,您为每个想要在非托管DLL中调用的函数创建一个函数头,并告诉. net函数所在的DLL,然后像. net包装器中的其他函数一样调用该函数。
You shouldn't even need any C++ knowledge - as long as you know the function definition of the functions in your unmanaged DLL, and the correct datatypes.
您甚至不需要任何c++知识——只要您知道非托管DLL中的函数的函数定义,以及正确的数据类型。