Solved

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

Posted on 2016-10-15
6
19 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 32

Expert Comment

by:sarabande
Comment Utility
(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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
festijazz earned 0 total points
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
I have found a fix.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now