Click here to Skip to main content
15,896,207 members

Comments by Member 10772496 (Top 11 by date)

Member 10772496 15-Oct-15 6:20am View    
In debugger i have also the following exception :
Raised Exception  : 'System.Reflection.TargetInvocationException' dans mscorlib.dll
Member 10772496 15-Oct-15 5:27am View    
How can i catch this exception ? And in which code side (C++ or VB.net) should i do this ?
Member 10772496 8-Jun-15 4:22am View    
if it can help you, i post my code with more details.
Thanks you in advance for your help !


In the VB code i do something like :

Private Declare Function Cpp_Function Lib "XLSTATLINK.dll" (<marshalas(unmanagedtype.struct)> ByRef Input As Input_data, <marshalas(unmanagedtype.struct)> ByRef Ouput As Ouput_data) As Double

Structure Input_data
<marshalas(unmanagedtype.safearray)> Public input1() As string
<marshalas(unmanagedtype.safearray)> Public input2() As string
Public input3 As Boolean
Public input4 As Long
End Structure

<structlayout(layoutkind.sequential, pack:="8)">
Structure Ouput_data
<marshalas(unmanagedtype.safearray, safearraysubtype:="VarEnum.VT_BSTR)"> Public ouput1() As String
End Structure

<webmethod()> _
Public Function Test(ByRef Input As Input_data, ByRef Ouput As Ouput_data) As Double
ReDim Ouput.ouput1(0 To 0)
Ouput.ouput1(0) = 0
test = Cpp_Function(Input, Ouput)
End Function
And my C++ code is something like :

#pragma pack(push, 8)
struct DCA_input_data
{
SAFEARRAY *input1;
SAFEARRAY *input2;
VARIANT_BOOL input3;
long input4;
};

struct DCA_output_data
{
SAFEARRAY *ouput1;
SAFEARRAY *ouput2;
};
#pragma pack(pop) //back to whatever the previous packing mode was

void ArrayToSafearrayCopy(BSTR *array, SAFEARRAY *&safeArray)
{
int moLenght = 0;
do
{
moLenght++;
} while (array[moLenght] != nullptr);
// Create a safe array of BSTR
CComSafeArray<bstr> saData(moLenght);

for (int i = 0; i < moLenght; i++)
{
saData[i] = array[i];
}

safeArray = saData.Detach();
saData.Destroy();
}


Test_API double __stdcall Cpp_Function(Input_data *pInput, Output_data *pOutput)
{

BSTR *mInput1 = LoadSAFEARRAYStr(&pInput->input1); // convert the SafeArray table into a BSTR table
BSTR *mInput2 = LoadSAFEARRAYStr(&pInput->input2); // convert the SafeArray table into a BSTR table

BSTR *moInput1;
BSTR *moInput2;

...
...
...
//Some computation on mInput1 and mInput2 ... and results are store in moInput1 and moInput2
...
...
...

ArrayToSafearrayCopy(moInput1, pOutput->ouput1);
ArrayToSafearrayCopy(moInput2, pOutput->ouput2);

}
For you information, I done some tests in my side :

1) When I send only the input data, i have no problems !

2) I checked the C++ code with a VBA test code, i have no problem !
Member 10772496 4-Jun-15 11:33am View    
The objectif of the vb.net code is to read the contained of the structure completed by the C++ DLL.

But When I try to read the ouput_data in the VB.net side, the System.AccessViolationException exception is raised.

I think my problem is that a don't use correctly the marshalling of the structure composed of SAFEARRAY coming from the C++ DLL.

What should i do to make this correctly ?

Member 10772496 4-Jun-15 10:50am View    
The C++ just complete the ouput_data structure ...
What you wanted more ?