Link to home
Start Free TrialLog in
Avatar of festijazz
festijazz

asked on

Pass an integers array from a c++ com automation assembly to a vb.net winform application.

Hello,
I am in front of a subtleties related to a transfer an integers array from a c++ com automation assembly to a vb.net winform application.

the array is defined in vb.net winform and the values are intialized inside the ole automation method. When the method returns to the main application to display the contents of the array, the defined values are lost. Something goes wrong in the heap it seems.
I have tried via a reference of the type library or late binding the effect is the same.

Here is the segment of the code I am trying to cover:

    Private _Layer As Object = Nothing
     Dim ArrValues(10) As Integer
     _Layer.Generate(ArrValues(LBound(ArrValues)), UBound(ArrValues

BOOL CLayer::Generate(long FAR* IntegersArray, long Items)
{
      for (long Index = 0; Index < Items; Index++)
      {
            IntegersArray[Index] = Index + 1;
      }

      return TRUE;
}

The complete sample application is available from the following link: arraybounding
What could be the reason of my issue.
Thank you very much in advance.
Best regards.
MiQi
Avatar of sarabande
sarabande
Flag of Luxembourg image

(ArrValues(LBound(ArrValues)), UBound(ArrValues

it seems that you are not passing the array by reference but a temporary array filled via iterators LBound and UBound. because of that the assignments done in Generate function were put to the temporary array and therefore nothing was done in the original array.

you may try to pass the array byref but don't know whether that could work.
i would say you have to use marshaling for a proper link from vb.net to c++.

Sara
Avatar of festijazz
festijazz

ASKER

Hello Sara,
Thank you for your inputs,
Do you mean I have to specify long** in the c++ side ?

Could you have a view on my demo sample ?
https://www.wetransfer.com/downloads/3a4e8cebddcdacf829aa35ab5e8ccbed20161016110910/1625961d705074f38ce4605a2ff05ace20161016110910/97a299

If you want we could review that online if possible ?
Let me know.
Thank you very much in advance.
Best regards.
MiQi
Do you mean I have to specify long** in the c++ side ?

no, long** is a c method if you want to return a new pointer.

that is not suitable for dll return since the pointer should be freed in the dll and surely is not what the c++ side expects.

what i meant is that you don't pass the original array to the dll but a tempory Array created by using

ArrValues(LBound(ArrValues)), UBound(ArrValues))

this is a copy of your ArrValues using the constructor which takes all Elements from ArrValues from begin to end (or lower bound to upper bound).

since it is a copy all changes made are "lost" after the call, because there is no way from the copy to the source.

if i am right with this suggestion (unfortunately i never learnt programming with vb.net), you simply could try to use the original array:

_Layer.Generate(ArrValues)

and you should have a good chance that it worked.

Could you have a view on my demo sample ?
the link you provided requires me to accept cookies and 'general terms and conditions' which i can't do at my current environment. same applies to a live chat. if the issue is still open at week-end i could operate less restricted in my home office.

Sara
ASKER CERTIFIED SOLUTION
Avatar of festijazz
festijazz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
congratulations.

fyi: VARIANT type is used in COM to take any kind of input. in C it is a union of many struct and the 'type' property determines which struct was used. in your case the type is 'int array' and the variant points internally to the begin of the array you passed as argument.

you don't have to care for the interface in vb.net, but of course you need to know how to declare the com function. also it is important to pass the (array) object in order and not a copy to get the changed values returned.

Sara
I have found a fix.