Pass std::vector to VB

Posted on 2003-03-26
Medium Priority
Last Modified: 2013-12-14
Hi everyone,

Before I ask the question I apologise if this should be in the VB section, however I'm more of a C++ programmer than a VB one!

I'm trying to pass a std:vector from my C++ DLL to a VB app, the vector contains a struct object I defined in my dll. What is the best way to do this, I've heard rumours about using Collections although I'm new to VB and am a little confused about it all.

Thanks in advance,

Question by:slber
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
LVL 12

Expert Comment

ID: 8210465
It is very possible that you should use the VB collection idiom. If so you have to make a class that wraps the std::vector class and provide two crucial functions used by VB. One function is to get the first element of a collection and the other is to get the next element of a collection. If you implement those properly the VB code can use the 'for each' statement to iterate through the std::vector class.

Is this for VB6 or is it VB.Net? If it is for VB.Net you simply use the generic .Net mechanism for walking through collections by writing a class that implements the IEnumerable interface. In VB6 you do it by making an object that implements the VB6 enumerator "interface" and it is done by declaring and implementing the above two mentioned functions. How the exact mechanism is in VB6 to get those functions - I am not sure but I believe it relies on some form of COM object interface and as such you have to write an object that exports two interfaces (minimum). One is the IUnknown interface and that interface simply provide the QueryInterface function and there you just test if the GUID is equal to the enumerator GUID and if it is you return a pointer to the other interface which is the enumerator interface. The enumerator interface then export the two functions mentioned above. Note that in the COM model you don't export objects but interfaces instead while in the .NET the interface really is an interface that is implemented by an object and as such is very different from the COM world.

So for one thing it is important to know if you mean VB6 or VB.NET when you say 'VB' above.

LVL 86

Accepted Solution

jkr earned 200 total points
ID: 8210466
This is not trivial. However, this MSDN article shows the long and winding road and how to do it: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/vcconimplementingstlbasedcollection.asp ("Implementing an STL-Based Collection")

Expert Comment

ID: 9550806
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: jkr

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

801 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