returning a <ma> from a dll

Hi,

How do I return a <map> or <string> or <vector> from a COM dll in VC++.I am not using .net

I have a method in the dll which fills a map.But how do I return this map from the method.


Thanks
Regards
Jasmina
jas123Asked:
Who is Participating?
 
ambienceConnect With a Mentor Commented:
Taking the address of a map and returning it as a long , and later typecasting the pointer to a map doesnt seem to be a reasonable answer , BUT IT CAN WORK, provided your client is also written in c++ and that the component is an INPROC_SERVER.

But when it comes to Vb you have to be very careful in chosing the data-types, only types that are autoamtion types are given respect by VB.

As i see that you want to create a kind of dictionary in your component.

The right way to do is to either implement IDictionary interface on your main component, which in its implementation may use stl maps or what ever you like.

Or that you create a separate component that implements IDictionary, and implement al the logic there, then your testcomponent can aggregate that Dictionary component and return an interface pointer to VB, which will have no problem with it then.
0
 
SeregaCommented:
I think the simplest method is to convert pointer to it in dll to (void*) and make back conversion where it is called from (void*) to pointer to your map, vector, etc.
0
 
jas123Author Commented:
Hey how do I get a pointer to the map object created.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
jimbucciCommented:
One way is to pass it as a VARIANT setting var.lVal as an (unsigned long *) to your <map>.
On the receiving side unpack the VARIANT and cast it to a <map>.

..or..

unpack the <map> into an array of <type> and pass the array to the caller.  The caller will then reconstruct the array.

I've actually been using SAFEARRAYs to pass data between COM objects and clients.  It works fine except you have to unpack the safearray - which is not a big deal, but it's extra code.
Let me know if this helps.
Jim


0
 
SeregaCommented:
You probably have declaration something like this:
std::map<int,std::string> my_map;

Pass the param as void*:
   my_func((void*)&my_map);

Function declaration:
my_func(void* p);

Function body:
my_func(void* p)
{
std::map<int,std::string>* pMyMap = (std::map<int,std::string>*)p;
....
}

pMyMap - pointer to your map...
0
 
himmyaCommented:
hi,

  yes u can written a void* from ur dll and when u get it in ur client side then just use static_cast<map> so that u r able to use that map at client side..

rgds,
himmya
0
 
jkrCommented:
Check out the ALLINONE COM sample (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/_sample_com_allinone.asp): "ALLINONE: Implements a Server Using ATL, Exposing STL Collections, Controlled by Compiler COM Support in an MFC Application" (The code can be downloaded from http://msdn.microsoft.com/code/default.asp?URL=/code/sample.asp?url=/MSDN-FILES/026/001/656/msdncompositedoc.xml) - this sample demonstrates how to expose STL collections (such as a map) from a COM DLL.
0
 
jas123Author Commented:
I have a method called method2 in the baove dll as

STDMETHODIMP TestComponent1::method2(VARIANT *retval)
{
     // TODO: Add your implementation code here

     //CComBSTR ret;
     //ret.Append("IN ATL DLL - method2");
     //*retval = ret.Copy();

     HASHMAP map;
     HASHMAP::iterator it;

     map.insert(HASHMAP::value_type(0,"HASHMAP VALUE 1"));
     map.insert(HASHMAP::value_type(1,"HASHMAP VALUE 2"));
     map.insert(HASHMAP::value_type(2,"HASHMAP VALUE 3"));
     map.insert(HASHMAP::value_type(3,"HASHMAP VALUE 4"));
     map.insert(HASHMAP::value_type(4,"HASHMAP VALUE 5"));



        retval = (VARIANT *)&map;

     return S_OK;
}


I call this method from VB clien as

Private Sub Command1_Click()
    Dim myobj As Object
    Dim mydict As Dictionary
    Set myobj = CreateObject("atldll.TestComponent1.1")
    Set mydict = myobj.method2()
End Sub

And when I run the VB client, it gives me an error saying
"Object doesn't support this property or method"

What could be the problem in the dll.

Thanks
Regards
Jasmina


0
 
ambienceCommented:
>> What could be the problem in the dll

Yikes!! , sorry to say but the problem appears to be in the thinking.

There is no way on earth you can directly pass back a std::map from some VC component and treat it as a Dictionary in VB. no way !!!!!!!!
 
0
 
ambienceCommented:
>> I  have a method in the dll which fills a map.But how do I return this map from the method.

you are not required to !!, you are required to return an IDictionary interface pointer here (considering the exmaple in VB that you have posted).
0
 
ambienceCommented:
One more thought, maybe its too much to implement your own dictionary object, so i recommend that you use a dictionary object that is already installed on your system.
"Scripting.Dictionary" should be installed if you have scripting installed (comes wih IE 4.0 also).

Its much easier to use it for your needs.

CComPtr<IDictoinary> pDict;
pDict.CoCreateInstance("Scripting.Dictionary");
0
 
SeregaCommented:
Hi jas123,

you can leave your TestComponent1::method2 as it is, but you should to use it only with VC++  :-)  
For using it with VB, you have to implement the way proposed by ambience.

0
 
griesshCommented:
Dear jas123

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "ambience"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
MindphaserCommented:
Force accepted

** Mindphaser - Community Support Moderator **
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.