将SWIG包装的c++ .dll集成到c#项目中



我在VS2019下有2个解决方案:RLibrary(包含c++项目)和VTApp(包含c#项目)。

解决方案RLibrary有两个项目:RLib c++ .dll和SwigW(包装器项目)。SwigW生成了以下文件:cxx, RLib.csRLib.csRLibPINVOKE.cs.

RLibPINVOKE.cs我有这样的行:

   class RLibPINVOKE {
    //...
     static RLibPINVOKE() {
    }
          [global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Create")]
          public static extern global::System.IntPtr new_Create();
          [global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Request")]
          public static extern global::System.IntPtr new_Request();
          // ...
    }
因此,在构建RLibrary解决方案后,我将有两个.dll(也是.lib)文件:RLib.dllSwigW.dll。我想使用VTApp中的new_Create(), new_Request()函数。VTApp也有两个项目:UICoreUI有一个对Core的引用。我将RLibPINVOKE.cs文件添加到Core项目中,只需右键单击->添加->现有项目,并尝试在命名空间UI中使用UI项目中的上述功能。,即
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using Core;
using Newtonsoft.Json;
namespace UI {
public partial class MainWindow {
private void Execute_Req(object sender, EREventArgs e)
{
   //...
   IntPtr p = new_Create();
}
}
}

不幸的是,我得到CS01013:名称'new_Create'在当前上下文中不存在。错误。在我看来,即使没有将SwigW.dll添加到Core项目中,我也不应该有这种编译时错误。那么,问题是什么呢?我如何添加SwigW.dllCore项目正确,如果我需要添加它?此外,我是否应该将RLib.dll添加到Core ?

试试RLibPINVOKE.new_Create()。方法在class RLibPINVOKE

class RLibPINVOKE {
//...

您可能需要using,但Visual Studio应该能够在RLibPINVOKE上向您建议。

最新更新