Solved

returning a <ma> from a dll

Posted on 2002-07-23
14
255 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:jas123
[X]
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
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 2

Expert Comment

by:Serega
ID: 7171212
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
 

Author Comment

by:jas123
ID: 7171255
Hey how do I get a pointer to the map object created.
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7171542
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Serega
ID: 7171581
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
 
LVL 1

Expert Comment

by:himmya
ID: 7171651
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
 
LVL 86

Expert Comment

by:jkr
ID: 7171667
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
 

Author Comment

by:jas123
ID: 7174014
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
 
LVL 22

Expert Comment

by:ambience
ID: 7179321
>> 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
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 7179326
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
 
LVL 22

Expert Comment

by:ambience
ID: 7179331
>> 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
 
LVL 22

Expert Comment

by:ambience
ID: 7179333
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
 
LVL 2

Expert Comment

by:Serega
ID: 7179474
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
 
LVL 11

Expert Comment

by:griessh
ID: 7378419
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
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7420036
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

724 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