Solved

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

Posted on 2016-10-15
6
49 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 34

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 34

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 34

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

729 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