Solved

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

Posted on 2016-10-15
6
40 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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