Link to home
Start Free TrialLog in
Avatar of jas123
jas123

asked on

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
Avatar of Serega
Serega
Flag of Belarus image

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.
Avatar of jas123
jas123

ASKER

Hey how do I get a pointer to the map object created.
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


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...
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
Avatar of jkr
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.
Avatar of jas123

ASKER

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


>> 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 !!!!!!!!
 
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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).
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");
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.

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
Force accepted

** Mindphaser - Community Support Moderator **