Solved

returning a <ma> from a dll

Posted on 2002-07-23
14
250 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now