[問題] 請問VB.net call C++'s DLL問題

看板C_and_CPP (C/C++)作者時間10年前 (2015/06/29 09:50), 編輯推噓0(004)
留言4則, 1人參與, 最新討論串1/1
開發平台(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): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.54.20 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1435542600.A.673.html

06/30 15:28, , 1F
C++程式碼
06/30 15:28, 1F

06/30 15:28, , 2F

06/30 15:29, , 3F
VB.net 程式碼
06/30 15:29, 3F

06/30 15:29, , 4F
文章代碼(AID): #1LaAH8Pp (C_and_CPP)
文章代碼(AID): #1LaAH8Pp (C_and_CPP)