[问题] 请问VB.net call C++'s DLL问题

楼主: jerelee   2015-06-29 09:49:57
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Eclipse+Vb.net(vs2010)
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
sqlite3
问题(Question):
请问VB.net call C++'s DLL问题,会出现错误
但使用C call C++'s DLL 是可以正确传回数值
麻烦各位前进帮忙,感谢
喂入的资料(Input):
预期的正确结果(Expected Output):
return structure
错误结果(Wrong Output):
但使用VB.NET call C++'s DLL会出现下列错误
无法封送处理 'parameter #3': 无效的 Managed/Unmanaged 型别组合 (此实值型别必须搭配 Struct)
程式码(Code):(请善用置底文网页, 记得排版)
C++程式码
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#include <stdio.h>
extern "C"
{
#include <sqlite3.h>
}
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <windows.h> //取系统时间
#include <math.h>
#include <time.h>
#include <sstream>
typedef struct Tconditions {
double str_double0 ; //0
double str_double1 ;//1
double str_double2 ;//2
double str_double3 ;//3
double str_double4 ;//4
}Tconditions1;
typedef struct Tswitches {
int str_int0 ;//0
int str_int1 ;//1
int str_int2 ;//2
int str_int3 ;//3
int str_int4 ;//4
}Tswitches1;
typedef struct Toutputs {
double str_output0;//0
double str_double1;//1
double str_double2;//2
double str_double3;//3
double str_double4 ;//4
}str_Outputs1;
extern "C" __declspec( dllexport ) double __cdecl fun1(Tconditions str_conditions ,Tswitches str_Switches,Toutputs &str_output11)
{
double *vi_cond;
double *outputs1,*cond;
int *Switch;
int Switches[7];
double *Conditions;
// 开启 database 档
sqlite3_initialize( );
SYSTEMTIME st;
int rc = sqlite3_open("selection.db", &db);
if ( rc != SQLITE_OK)
{
sqlite3_close( db );
exit(-1);
}
Conditions[0]=str_conditions.str_double0;
Conditions[1]=str_conditions.str_double1;
Conditions[2]=str_conditions.str_double2;
Conditions[3]=str_conditions.str_double3;
Conditions[4]=str_conditions.str_double4;
Switches[0]=str_Switches.str_int0;
Switches[1]=str_Switches.str_int1;
Switches[2]=str_Switches.str_int2;
Switches[3]=str_Switches.str_int3;
Switches[4]=str_Switches.str_int4;
cond=Conditions;
Switch=Switches;
outputs1=fun2(cond,Switch,vi_cond);
str_output11.str_output0=outputs1[0];
str_output11.str_output1=outputs1[1];
str_output11.str_output2=outputs1[2];
str_output11.str_output3=outputs1[3];
str_output11.str_output4=outputs1[4];
}
double *fun2(double *Conditions,int *Switches,double * vi_cond)
{
}
VB.net 程式码
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure Tconditions
<MarshalAs(UnmanagedType.R8)> Dim str_double0 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_double1 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_double2 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_double3 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_double4 As Double
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure Tswitches
<MarshalAs(UnmanagedType.I4)> Dim str_int0 As Integer
<MarshalAs(UnmanagedType.I4)> Dim str_int1 As Integer
<MarshalAs(UnmanagedType.I4)> Dim str_int2 As Integer
<MarshalAs(UnmanagedType.I4)> Dim str_int3 As Integer
<MarshalAs(UnmanagedType.I4)> Dim str_int4 As Integer
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure Toutputs
<MarshalAs(UnmanagedType.R8)> Dim str_output0 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_output1 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_output2 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_output3 As Double
<MarshalAs(UnmanagedType.R8)> Dim str_output4 As Double
End Structure
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Conditions As Tconditions
Dim Switches As Tswitches
Dim Outputs As Toutputs
Dim Conditions2(11), Outputs2(25), Outputs3(25) As Double
Dim Switches2(7) As Integer
Dim aaa() As String
Dim test2 As Double
For i = 1 To 1
Conditions.str_double0 = Double.Parse(TextBox1.Text)
Conditions.str_double1 = Double.Parse(TextBox2.Text)
Conditions.str_double2 = Double.Parse(TextBox3.Text)
Conditions.str_double3 = Double.Parse(TextBox4.Text)
Conditions.str_double4 = Double.Parse(TextBox5.Text)
Switches.str_int0 = Integer.Parse(ComboBox1.SelectedIndex)
Switches.str_int1 = Integer.Parse(ComboBox2.SelectedIndex)
Switches.str_int2 = 140 'Integer.Parse(ComboBox3.SelectedIndex) + 1
Switches.str_int3 = Integer.Parse(ComboBox4.SelectedIndex)
Switches.str_int4 = Integer.Parse(ComboBox5.SelectedIndex)
test2 = Selections(Conditions, Switches, Outputs)
RichTextBox1.Clear()
RichTextBox1.AppendText(Outputs.str_output0.ToString)
RichTextBox1.AppendText(Outputs.str_output1.ToString)
RichTextBox1.AppendText(Outputs.str_output2.ToString)
RichTextBox1.AppendText(Outputs.str_output3.ToString)
RichTextBox1.AppendText(Outputs.str_output4.ToString)
Next
End Sub
<DllImport("libselections.DLL", EntryPoint:="Selections", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function Selections(<MarshalAs(UnmanagedType.LPArray), [In]()> ByVal Conditions As Tconditions, <MarshalAs(UnmanagedType.LPArray), [In]()> ByVal Switches As Tswitches, <MarshalAs(UnmanagedType.LPArray), [Out]()> ByVal Outputs As Toutputs) As Double 'String() 'ByRef Outputs As Toutputs , <[Out](), MarshalAs(UnmanagedType.LPArray)> ByVal Outputs() As Double
End Function
End Class
补充说明(Supplement):
楼主: jerelee   2015-06-30 15:28:00

Links booklink

Contact Us: admin [ a t ] ucptt.com