我在VS2019下有2个解决方案:RLibrary(包含c++项目)和VTApp(包含c#项目)。
解决方案RLibrary有两个项目:RLib c++ .dll和SwigW(包装器项目)。SwigW生成了以下文件:cxx, RLib.cs, RLib.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.dll和SwigW.dll。我想使用VTApp中的new_Create()
, new_Request()
函数。VTApp也有两个项目:UI和Core。UI有一个对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.dll到Core项目正确,如果我需要添加它?此外,我是否应该将RLib.dll添加到Core ?
试试RLibPINVOKE.new_Create()
。方法在class RLibPINVOKE
class RLibPINVOKE {
//...
您可能需要using
,但Visual Studio应该能够在RLibPINVOKE
上向您建议。