COM Server returning array of objects

Posted on 2005-05-05
Last Modified: 2013-11-25
In summary I have an in-process COM Server written in C++ that I want to return an array or collection of objects so that a VB Client app or VC++ Client app can then enumerate through that list.

COM Server has 2 interfaces, WidgetManager and Widget.  COM Server is created using ATL.

WidgetManager has one method, GetList, that I want to return a list/array/collection of Widget objects.  The WidgetManager should return its list of global Widget data.  The idea is that the Client calls WidgetManager.GetList() to get a list of Widgets.  I only want the Client to have a copy of th Widgets.  Client then goes through the list calling methods on the Widget interface for each Widget in the collection.

The Client can be Visual Basic or VC++ only so don't need to support Automation.

Question :- what should my GetList() method signature look like ?  I need to define this for the application programmers - VB or VC++.

Could anybody give me some sample code ?
Question by:ascot
    LVL 86

    Accepted Solution

    Check out;en-us;205277 ("How to pass arrays and strings between Visual Basic and C functions or between Visual Basic and C++ functions by using Visual Basic 6.0"). The idea is to use SAFEARRAYs for that purpose.

    Author Comment

    Thanks for the response.  The link is very useful for VB Clients.  What I'm really looking for is "Pass an array of user defined types by using type libraries"
    From the example that looks like I need
    HRESULT ATL_Func1([in, out] SAFEARRAY (myType)*ppsaMyArray, [out, retval] long *plResult);

    But then how do I call an interface that is defined using SAFEARRAY from a C++ client ?
    LVL 8

    Expert Comment

    Why wouldn't you just declare a SAFEARRAY variable in your c++ client?


    Author Comment

    The links mentioned by jkr were very useful - however I have now discovered the COM enumerators and I think that is the way forward  as I understand that it will enable me to expose an STL collection of Widgets that a VB Client can then iterate through as though it were a VB Collection of Widgets.  
    I am closing this and awarding the points to jkr

    Featured Post

    Highfive Gives IT Their Time Back

    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!

    Join & Write a Comment

    As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
    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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    734 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

    17 Experts available now in Live!

    Get 1:1 Help Now