我想PInvoke从C#用VB6编写的函数.我们可以在VB6中调用这个函数,如下所示:
Declare Function ApplyNNet Lib " location of the DLL file" (MyNNetType As String, MyAddress As String, MyInput() As Double) As Variant
Run Code Online (Sandbox Code Playgroud)
我在C#中的代码是:
[DllImport("NNetApply.dll", EntryPoint = "ApplyNNet", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr ApplyNNet(string type, string add, double[,] data);
//run ANN with data
string address = @"C:\Users\PNGE-User\Desktop\Faegheh\Project\Neural Network For Pressure Vs q,x,y\P ve x,y,q\P ve x,y,q";
double[,] P = new double[no_data,2];
var P_ = ApplyNNet("Back Prop", address, data);
Run Code Online (Sandbox Code Playgroud)
当我调试我的代码时,出现错误:
尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
首先,我假设DLL实际上并不是用VB6编写的.您显示Declare
用于调用该函数的代码的事实与此相矛盾.所以我假设DLL是用其他语言编写的(例如C++),并且您当前正在从VB6调用DLL.
调用DLL的VB6代码声明如下:
Declare Function ApplyNNet Lib "DLLFileName" (MyNNetType As String,
MyAddress As String, MyInput() As Double) As Variant
Run Code Online (Sandbox Code Playgroud)
您的C#版本如下所示:
[DllImport("NNetApply.dll", EntryPoint = "ApplyNNet",
CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr ApplyNNet(string type, string add, double[,] data);
Run Code Online (Sandbox Code Playgroud)
有两个明显的区别:
double[,]
你需要double[]
.IntPtr
.您需要object
在C#代码中声明返回值是类型.所以pinvoke应该是:
[DllImport("NNetApply.dll")]
public static extern object ApplyNNet(string type, string add, double[] data);
Run Code Online (Sandbox Code Playgroud)
更新
使用此声明,您会收到pinvoke错误:
PInvoke限制:无法返回变体.
我不知道你怎么能在pinvoke中解决这个问题.在你的情况下,我想我会创建一个包装DLL并将其作为COM接口公开的VB6项目.然后,我将为您的C#项目添加一个COM引用,然后继续.
归档时间: |
|
查看次数: |
631 次 |
最近记录: |