Accessing C++ functions in C#

  • Thread starter Thread starter James Crouch
  • Start date Start date
J

James Crouch

A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C and
C++ that I am trying to access via C#. Is there an easy way to do this? If
not, should I just rewrite the SDK in C#?

Thanks.

James
 
James Crouch said:
A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C and
C++ that I am trying to access via C#. Is there an easy way to do this? If
not, should I just rewrite the SDK in C#?

There are undoubtedly some horrid ways to access C++ directly from C#. The
best way though would be to write a managed C++ wrapper for your SDK. It
would be very quick and painless.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
Hi,

depend of how it's implemented,
if it's a COM component you can access it very easily.

if it's a native dll, you can use P/Invoke with this you can get in trouble
dealing with memory allocation, converting data from managed to unmanaged
memory , etc , how easy ( or how ) complicated depend of the library itself.

of course, the idea of rewrite all in c# is valid too :)

cheers,
 
James Crouch said:
A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C
and C++ that I am trying to access via C#. Is there an easy way to do
this? If not, should I just rewrite the SDK in C#?

Thanks.

James

It depends, C# can call exported "C functions", that is functions with "C
linkage" exported from a native DLL can be called through the PInvoke
interop layer (see MSDN for details, search PInvoke and DllImport).
C++ class members however cannot be accesses directly, however unmanaged
classes can be wrapped by a managed equivalent and as such be consumed by
C#.
Consult MSDN for detailed info on both PInvoke and native interop at:
http://msdn2.microsoft.com/en-us/library/ms140906

Willy.
 
Back
Top