473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Managed Wrapper for UnManaged c++ code

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::stringsvalue);
};

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

Aug 14 '06 #1
4 2408
>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::stringsvalue);
};

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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 14 '06 #2
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

Bruno van Dooren [MVP VC++] wrote:
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::stringsvalue);
};

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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 14 '06 #3
>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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 14 '06 #4
Yes I see. Thanks!
Bruno van Dooren [MVP VC++] wrote:
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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: kaalus | last post by:
Hello I have problem mixing managed and unmanaged C++ code in VS.NET 2003. I want to create a windows forms application which uses some C++ code that is incompatible with managed extensions...
1
by: Zak | last post by:
It would be nice to have one DLL that contains both my managed and my unmanaged functions. I would like to be able to use the unmanaged parts from C++ without a .NET Framework dependency too. ...
2
by: asanford | last post by:
We use StackWalk(StackWalk64) from dbghelp.dll to walk our callstacksas needed, using the various Sym* methods (SymGetSymFromAddr, SymGetLineFromAddr) to resolve source file, function name, and...
0
by: DotNetJunkies User | last post by:
Background: I am creating a VC++ .NET wrapper for a C++ DLL; the aim is to use this wrapper in C# as shown below: Range r = new Range( 2, 2 ); r = new Cell( “Hello Mum” ); Range is a...
1
by: tsmith | last post by:
Hello, Quick question: Can I mix managed C++ (forms and GUI stuff) with nonmanaged C++ code (Direct3D). Note that I am not interested in managed DX (the C# API). I still am writing C++ code,...
2
by: chuck rudolph | last post by:
The project that I am on is going to require me to mix managed and unmanaged code. I created a vc++ class library (.net) project bane TestLib. I inserted the following test code in testLib.h. The...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
2
by: Sandy | last post by:
I am confused about Unmanaged Code, How .Net Framework treate that code, What is the use of that. Thanks in advance Sandeep Chitode
3
by: Ulrich Sprick | last post by:
Hi, I have to write a managed C++ wrapper around an existing, unmanaged C++ class. First tests suggested it should work, but after adding the complete unmanaged code, I get the following linker...
2
by: Jared Kail | last post by:
I've never mixed managed and unmanaged C++ before, but I've done a lot of "research" out on the web. Using some examples out there, I wrote this: #using <mscorlib.dll> using namespace System; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.