Click here to Skip to main content
14,970,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

VB
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


And in C++ code :

C++
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.
Posted
Updated 14-Oct-15 23:24pm
v2
Comments
Richard MacCutchan 15-Oct-15 5:24am
   
You need to catch the exception and check the details.
Member 10772496 15-Oct-15 5:27am
   
How can i catch this exception ? And in which code side (C++ or VB.net) should i do this ?
Richard MacCutchan 15-Oct-15 5:40am
   
Use a Try/Catch block (or whatever VB.NET equivalent there is. And you catch it in the place that it gets thrown. Use your debugger first so you can quickly see what is going on.
Member 10772496 15-Oct-15 6:20am
   
In debugger i have also the following exception :
Raised Exception  : 'System.Reflection.TargetInvocationException' dans mscorlib.dll
Richard MacCutchan 15-Oct-15 8:15am
   
Sorry but that could mean anything.
Kenneth Haugland 15-Oct-15 7:31am
   
Not an expert in this but don't you need to have the PreserveSig on imported structures and classes from a dll file?

1 solution

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 ...

C++
XLSTATLINK_API double __stdcall Cpp_NOR(ST_DataSet *iData)
{
 
}


... you can write something like ...

C++
ref class CPP 
{
public:
static double NOR(Types::ST_DataSet^ iData)
{
    if (iData->Doubles1Loaded) { ... }

    return 0.0;
};


From VB.NET you can call the above function using ...

VB
dim iData as ST_DataSet

CPP.NOR(iData)


I'm not a VB.NET coder so I can't promise the VB.NET syntax is 100%.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900