封送结构包含结构指针 [英] Marshaling Structure contained structure pointer
问题描述
我有一个问题是将包含从VB.net到C ++ dll的结构指针的结构传递给它。
当我尝试以下代码时,会引发此异常:
'System.Runtime.InteropServices.COMException
在VB.net方面我做:
Hi,
I have a problem to pass a structure contained a structure pointer from VB.net to a C++ dll.
When i try the following code, this exception is raised :
'System.Runtime.InteropServices.COMException
In VB.net side i do :
Private Declare Function Cpp_NOR Lib "XLSTATLINK.dll" (<MarshalAs(UnmanagedType.Struct)> ByRef iData As ST_DataSet) As Double
<StructLayout(LayoutKind.Sequential, Pack:=8)>
Structure ST_DataSet
'doubles1 data
Public doubles1Loaded As Boolean
Public doubles1 As ST_MVectDbl
End Structure
<StructLayout(LayoutKind.Sequential, Pack:=8)>
Structure ST_MVectDbl
Public nbcols As Long
Public nbcolsreserved As Long
Public samenbrows As Boolean
Public nbrowsmax As Long
Public nbrows() As Long
Public vectors() As ST_VectDbl
Public cLabels() As String
End Structure
<StructLayout(LayoutKind.Sequential, Pack:=8)>
Structure ST_VectDbl
Public count As Long
Public label As String
Public vect() As Double
End Structure
在C ++代码中:
And in C++ code :
struct ST_DataSet
{
//doubles1 data
VARIANT_BOOL Doubles1Loaded;
ST_MVectDbl Doubles1;
}
struct ST_MVectDbl
{
int nbcols;
int nbcolsreserved;
VARIANT_BOOL samenbrows;
int nbrowsmax;
SAFEARRAY *nbrows;
ST_VectDbl *Vectors;
SAFEARRAY *CLabels;
};
struct ST_VectDbl
{
int Count;
BSTR Label;
SAFEARRAY *Vect;
};
XLSTATLINK_API double __stdcall Cpp_NOR(ST_DataSet *iData)
{
}
任何人都可以帮助我吗?
提前致谢
Anyone please can help me ?
Thanks in advance.
推荐答案
当你不需要成为数据封送专家时,不要自杀。
创建一个新的VB.NET程序集(比如Types)并将所有数据结构放在那里。
添加对这个程序集的引用原生和现有的VB.NET项目。
启用托管例如。本机项目中的C ++ / CLI。
而不是......
Don't kill yourself trying to become an expert at data marshaling when you don't need to be one.
Create a new VB.NET assembly (say "Types") and put all your data structures there.
Add a reference to this assembly in both the native and existing VB.NET projects.
Enable managed eg. C++/CLI in the native project.
Instead of ...
XLSTATLINK_API double __stdcall Cpp_NOR(ST_DataSet *iData)
{
}
...你可以写点类似......
... you can write something like ...
ref class CPP
{
public:
static double NOR(Types::ST_DataSet^ iData)
{
if (iData->Doubles1Loaded) { ... }
return 0.0;
};
从VB.NET中你可以使用...来调用上面的函数。
From VB.NET you can call the above function using ...
dim iData as ST_DataSet
CPP.NOR(iData)
我不是VB.NET编码器所以我不能保证VB.NET语法是100%。
I'm not a VB.NET coder so I can't promise the VB.NET syntax is 100%.
这篇关于封送结构包含结构指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!