Managed Wrapper for UnManaged c++ code

  • Thread starter Thread starter devmentee
  • Start date Start date
D

devmentee

I want to write a managed wrapper( kind of proxy) which will call into
unmanaged C++ code.
I've got a general idea and have read some articles on how to do it.
But I cannot find any information on some specific things..

for example, I have unmanaged class
class Foo
{
void SetData(std::map<std::string, std::string> svalue);
};

I am not sure how I would represent this method in managed C++
especially when it comes to passing in/returning STL data structure
to/from unmanaged class.

Can someone please point me to any decent articles/documentation on
this?? I cannot find anything on microsoft site except how to use
managed String data type!

Your help will be much appreciated. Thanks
 
I want to write a managed wrapper( kind of proxy) which will call into
unmanaged C++ code.
I've got a general idea and have read some articles on how to do it.
But I cannot find any information on some specific things..

for example, I have unmanaged class
class Foo
{
void SetData(std::map<std::string, std::string> svalue);
};

I am not sure how I would represent this method in managed C++
especially when it comes to passing in/returning STL data structure
to/from unmanaged class.

Can someone please point me to any decent articles/documentation on
this?? I cannot find anything on microsoft site except how to use
managed String data type!

AFAIK the STL components cannot be used directly in the .NET framework, so
you have to do the conversion between (for example) a hashtable and map.

Your proxy class could take a hashtable as input, and then create a new
stl::map and copy the contents of the hashtable into it for unmanaged use.
For output you'd do the same, but in the other direction: fill the hashtable
with copies of the contents of the stl::map.

The 1 exception is that you can use the stl::map as an IntPtr in .NET, as
long as you don't want to do anythin with it.
In that case you could return it from your unmanaged code as an IntPtr,
store the ptr in .NET code, and supply it back to your unmanaged code later.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
I see so my managed wrapper method can take hasmap in as a param and
then I can use stl:map within the managed code to read from that
hashmap? I thought that managed code won't let me use the STL map even
though it is C++! I prefer this solution to the one with intptr as i am
not too familiar with that...

Thanks
 
I see so my managed wrapper method can take hasmap in as a param and
then I can use stl:map within the managed code to read from that
hashmap? I thought that managed code won't let me use the STL map even
though it is C++! I prefer this solution to the one with intptr as i am
not too familiar with that...

Maybe I was not clear enough.
There is no way that I know of to use stl::map as an argument in a method
that has to be used by other .NET languages.

But there is another option.
You create a proxy class that has a hashtable as an argument. A hashtable
and an STL map are functionally equivalent for most purposes.

inside your function call (managed C++) you can use both hashtable and
stl::map, since you are working in mixed mode.
So you create a new instance of an stl::map and fill it with the information
that is already contained in the .NET hashtable.
This means that the same information exists now as a managed hashtable, and
an unmanaged stl::map.

your unmanaged code can then invoke the original C++ class, and supply the
stl::map as an argument.
any changes to the stl::map during your invocation of the original class has
to be made to the hashtable as well afterwards.

There is a lot of overhead this way (because of all the copying) but there
is little that you can do about it.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Yes I see. Thanks!
Bruno said:
Maybe I was not clear enough.
There is no way that I know of to use stl::map as an argument in a method
that has to be used by other .NET languages.

But there is another option.
You create a proxy class that has a hashtable as an argument. A hashtable
and an STL map are functionally equivalent for most purposes.

inside your function call (managed C++) you can use both hashtable and
stl::map, since you are working in mixed mode.
So you create a new instance of an stl::map and fill it with the information
that is already contained in the .NET hashtable.
This means that the same information exists now as a managed hashtable, and
an unmanaged stl::map.

your unmanaged code can then invoke the original C++ class, and supply the
stl::map as an argument.
any changes to the stl::map during your invocation of the original class has
to be made to the hashtable as well afterwards.

There is a lot of overhead this way (because of all the copying) but there
is little that you can do about it.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top