Pregunta

I am trying to pass an array of objects back from a c++/cli method back to the calling VB.NET routine. The function call returns a bool to indicate success or fail and extended information is returned in an Array of Objects via a parameter to the call.

I have stripped the code down to two example code units for demonstration of the problem.

The TSAResult Array should be filled in after the call to TestReplaceArray. e.g.:

    Dim TSAResult() As TestStruct = Nothing
    Dim MTCP As New TestCppPart()
    MTCP.TestReplaceArray(TSAResult)

I'm not getting back what I expect and it's probably something dumb on my part that's fouling the return.

To get more diagnostic inforamtion to see where things were going apart, I created this calling routine which populates the TestStructureArray just to see what showed up in the called method:

'This is the VB.Net caller routine
Dim TestStructArray() As TestStruct = Nothing
TestStructArray = New TestStruct(1) {}
Dim TSAResult() As TestStruct = Nothing

Dim TS As New TestStruct 'put some junk in it
TS.somevar = 10
TestStructArray(0) = TS
TS = New TestStruct
TS.somevar = 11
TestStructArray(1) = TS

Dim MTCP As New TestCppPart()
MTCP.TestReplaceArray(TestStructArray) 'Call the cpp/cli routine
TSAResult = TestStructArray 'take a gander and what came back

The C++/CLI routine which is called is:

// compiled with /clr
public ref struct TestStruct {
    int somevar;
};

public ref class TestCppPart {
public:
  void TestReplaceArray(array<TestStruct ^>^ IPInfoArray) {
    IPInfoArray = gcnew array<TestStruct ^>(5);
  }
};

I can see that the (for testing only) passed in array of objects is what I passed. I can also see that I can replace the array with the "IPInfoArray = gcnew array(5);" line. It doesn't have anything in the elements, but a 5 element array is now referenced by IPInfoArray.

On return to the VB.Net code, the original (testing only) array is still present and populated.

What I can't figure out is how to tell the c++/cli that I want to update the reference to the array (ByRef if you will) so it will go back to VB.Net with the new array.

Thanks, I feel this should be obvious, but I'm stuck!

¿Fue útil?

Solución

Your C++/CLI code needs to declare the array as below (note the '%' symbol):

void TestReplaceArray(array<teststruct^> ^%IPInfoArray)

Also mentioned in this post: Does the ^ symbol replace C#'s "ref" in parameter passing in C++/CLI code?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top