简体   繁体   English

创建简单的c ++ .net包装器。一步步

[英]Creating simple c++.net wrapper. Step-by-step

I've a c++ project. 我有一个c ++项目。 I admit that I'm a complete ZERO in c++. 我承认我在c ++中是一个完整的零。 But still I need to write a c++.net wrapper so I could work with an unmanaged c++ library using it. 但是我仍然需要编写一个c ++ .net包装器,这样我就可以使用非托管的c ++库。 So what I have: 1) unmanaged project's header files. 所以我有:1)非托管项目的头文件。 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 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. 我甚至不知道如何设置对非托管库的引用。

SOS SOS

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. 您需要创建C ++ / CLI类库项目,向其添加.NET类(此示例中为StudentWrapper),创建非托管类实例作为托管类成员,并包装每个非托管类函数。 Unmanaged library is added to C++/CLI project using linker dependencies list, and not as reference. 非托管库使用链接器依赖项列表添加到C ++ / CLI项目,而不是作为引用。 In the Project - Properties - Linker open Additional Dependencies and add .lib name there. 在Project - Properties - Linker中打开Additional Dependencies并在那里添加.lib名称。

Note: since we are talking about C++/CLI wrapper, no PInvoke! 注意:既然我们在谈论C ++ / CLI包装器,那么没有PInvoke! PInvoke is used to call exported functions (API), and not classes. PInvoke用于调用导出的函数(API),而不是类。

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中函数的函数定义以及正确的数据类型即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM