Wrapping Unmanaged c++ classes in Managed C++

P

Paul Kenny

Hi,
I am trying to expose the functionality of an unmanaged
C++ class to the other languages available in the .NET
Framework. I have decided to do this by wrapping the
unmanaged C++ class in a managed one. I have taken a look
at following sample code on how to do this.

http://longhorn.msdn.microsoft.com/lhsdk/ndp/vcmg_appendixs
amplecode.aspx

I have compiled this sample code and it all works fine.
Except that after I add a reference to the resulting
assembly in my C# project and look at it in the Object
Browser I can see all of the unmanaged functionality that
is exposed in the #included atlsecurity.h header file. How
can I hide these definitions so that c# cannot see
them ..after all it cannot use them. I would like only the
managed c++ wrapper class to be exposed.

This is not just an aesthetic issue. For instance if I
reference another assembly (which I don't have control of)
that has a class that is of the same name as one of the
unmanaged classes exposed above then the c# compiler
issues a warning (ambiguous name) when compiling it and
depending on the order in which the references were added
it may attempt to use the unmanaged class rather than the
other managed one which I want to use. A similar situation
in VB.NET results in a compiler error (ambiguous name) so
the application cannot be built at all.

I only used the above sample code to demonstrate my
problem. I have infact implemented my wrapper class and in
it's implementation it wraps a number of unmanaged c++
classes resulting in the contents of a lot of unmanaged
header files to be exposed in the resulting managed
assembly. So again my question is how can I hide the
contents of these unmanaged header files and only expose
my managed classes in my managed assembly??

Any help is greatly appreciated.
Cheers,
Paul

Paul Kenny
IONA Technologies
(e-mail address removed)
 
E

Edward Diener

Paul said:
Hi,
I am trying to expose the functionality of an unmanaged
C++ class to the other languages available in the .NET
Framework. I have decided to do this by wrapping the
unmanaged C++ class in a managed one. I have taken a look
at following sample code on how to do this.

http://longhorn.msdn.microsoft.com/lhsdk/ndp/vcmg_appendixs
amplecode.aspx

I have compiled this sample code and it all works fine.
Except that after I add a reference to the resulting
assembly in my C# project and look at it in the Object
Browser I can see all of the unmanaged functionality that
is exposed in the #included atlsecurity.h header file. How
can I hide these definitions so that c# cannot see
them ..after all it cannot use them. I would like only the
managed c++ wrapper class to be exposed.

Did you try using #pragma unmanaged for your unmanaged code ?
This is not just an aesthetic issue. For instance if I
reference another assembly (which I don't have control of)
that has a class that is of the same name as one of the
unmanaged classes exposed above then the c# compiler
issues a warning (ambiguous name) when compiling it and
depending on the order in which the references were added
it may attempt to use the unmanaged class rather than the
other managed one which I want to use. A similar situation
in VB.NET results in a compiler error (ambiguous name) so
the application cannot be built at all.

Namespaces exist so that your above scenario need never happen,
 
P

Paul Kenny

Did you try using #pragma unmanaged for your unmanaged
code ?

Yes, I tried that but it didn't make any difference.
Namespaces exist so that your above scenario need never
happen

I have no control over either the unmanaged c++ class that
I am trying to wrap or .NET assembly that I am trying to
import (which clashes with the some of the unmanaged types
in the unmanaged c++ class that I am trying to wrap.)

I still don't understand why these unmanaged types are
being exposed at all.
 
E

Edward Diener

Paul said:
Yes, I tried that but it didn't make any difference.


I have no control over either the unmanaged c++ class that
I am trying to wrap or .NET assembly that I am trying to
import (which clashes with the some of the unmanaged types
in the unmanaged c++ class that I am trying to wrap.)

Do you mean to say that the unmanaged c++ class uses the exact same
namespace as the .NET assembly ? If not, you can always refer to the .NET
assembly by its full namespace name rather than use a #using XXX, and this
full name should not clash with any names in your unmanaged c++ class.
I still don't understand why these unmanaged types are
being exposed at all.

I don't either, but I think you need to show a test case which proves your
problem.
 
R

Ronald Laeremans [MSFT]

Thanks for the feedback. It won't be needed with the Whidbey version of the
compiler. I'll look at the article.

Ronald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top