Solved

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

Posted on 2016-10-15
6
29 Views
Last Modified: 2016-10-22
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
0
Comment
Question by:festijazz
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 41845618
(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
0
 
LVL 1

Author Comment

by:festijazz
ID: 41845632
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
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41846589
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Accepted Solution

by:
festijazz earned 0 total points
ID: 41846609
Hello Sara,

I have finally found the reason of my issue.

1. The old parameter needs to be set [in, out]
2. The type of the c++ method argument is VARIANT*
3. From the vb.net caller, the argument needs to be passed as an object type.

This will return the expected result.
Oef, I am happy somebody was requesting me $100 to fix this basic issue.

Best regards.
MiQi
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41846708
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
0
 
LVL 1

Author Closing Comment

by:festijazz
ID: 41855095
I have found a fix.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question