C# Wrapper into unmanged code

J

Jason

Hello

I've got a DLL that is written in C++ and I want to create a C# wrapper for
it, instead of using Pinvoke. I know this can be done, but I'm not sure how
to go about this. I've got VS 2005 and I've been looking on the net, but
not alot of clear definitions out there. For now, until I get he hang of it,
my c++ DLL is pretty basic, as it only has functions that take in and return
integers.

Can anyone provide an example, or point to a good place on the web?

Thanks
 
P

Peter Ritchie [C# MVP]

You'll have to use PInvoke to get at the C++ code from your C# wrapper.

If your C++ Dll has C++ classes in it, you won't be able to PInvoke them.

I suggest you write a managed wrapper in C++/CLI. C++/CLI uses C++ Interop
(or Implicit PInvoke) and wouldn't require that you manually PInvoke. This
is especially useful if you want to use a native C++ class in C#.
 
J

Jason

Thats what I was think I would need to do, write a managed wrapper in C++
but I'm not sure how to accomplisj that. Is there a good example that you
can point out?
 
P

Peter Ritchie [C# MVP]

MSDN has a decent example of wrapping a native C++ class for use by any
managed language (the title details C#, but any managed language will work):
http://msdn2.microsoft.com/en-us/library/ms235281(VS.80).aspx

It's just a snippet of code with a native class, and the managed class that
wraps it as an opaque pointer (or uses the pimpl idiom)[1].

[1]http://en.wikipedia.org/wiki/Opaque_pointer

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
 
L

Leon Lambert

I use IJW to interface my C# applications to unmanaged C++ DLLS. It
involves making a managed C++ interface assembly though. Doing it this
way the C++ can expose entire classes and their methods and not just C
style methods. I find it really easy to do. Following is a link that
helps to understand how to use IJW.
http://www.codeproject.com/KB/mcpp/ijw_unmanaged.aspx

Hope this helps
Leon Lambert
 

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