Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Pass std::vector to VB

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,

1 Solution
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.

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")
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now