Home > Computer Programming, Computer Science > Interop – Managed C++ and C#

Interop – Managed C++ and C#

I mentioned about writing some basic stuff for Managed C++ so here I am writing it.

I had to send C# strings to Managed C++ dll(class library). In MC++ the managed reference is represented by ‘^’, in MC++ function’s prototype will be;

String^ foo(String ^ pInputString);

and call from C# will be normal like, Console.WriteLine(foo(str));

for sending array of strings;

void foo(array<String ^>^ pInputAStrings); // yeah looks weird

for sending a string as reference;

void foo(String ^ % pInputRString);

Similarly XmlDocument xdoc = new XmlDocument(); in C# is equivalent to

XmlDocument ^ xdoc = gcnew XmlDocument(); when we initiliaze with ‘gcnew’ the garbage collector will run even if we have enabled mixed types option in Managed C++ in .NET 2005.

The magical API that helped me through out is,

Marshal.Copy Method (IntPtr, Byte[], Int32, Int32) which have a total of 16 overloads and is in,

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

So using this method I convert a Base 64 Encoded string in bytes to native char * as follows.

array<Byte> ^ byteConvertedFromBase64 = nullptr;

String ^ string64bit = String::Empty;

byteConvertedFromBase64 = Convert::FromBase64String( string64bit );

char * bData = 0;

bData = new char[length];

Marshal::Copy(byteConvertedFromBase64, 0, IntPtr(bData), length);

It is a magical API which always worked when ever I needed to convert types from native char * to strings and vice versa and with other types.

Besides that I had to suppress the security checks that are incurred when calling native dlls from managed code, to save performance losses using PInvoke or COM Interop. This is done by SuppressUnmanagedCodeSecurityAttribute Class and applying /CLRUNMANAGEDCODECHECK in the linker settings. You can view MSDN for specific details.

Advertisements
  1. kevin
    August 17, 2006 at 11:01 am

    Hi,
    This was extremely helpful for me.
    passing strings and arrays. Where did you come across this, my C# app using a managed C++ library.

  2. kevin
    August 17, 2006 at 11:02 am

    Hi,
    This was extremely helpful for me! Can you share where you located the information of managed c++ types and there corresponding C# types, particularly in arguments. THANKs again.

  3. sree
    December 4, 2006 at 6:35 am

    Hi ,
    I am using Marshal::Copy to convert Byte array of managed c++ to unmanaged unsigned char* . Everything fine. But I’ve printed the byte buffer before using Marshal::Copy and the unsigned char* after conversion. To my wonder ..both are different. May I know what may be the problem…….

  4. paddy poker power
    October 30, 2007 at 1:09 pm

    Got here and seen your stuff – way to go!

  5. hank
    May 20, 2008 at 2:34 pm

    this is what’s missing on all the searches. Simple one line examples even a dumb ass like me can understand.

    Thank you so much, I found you after hours of searching for exactly this solution.

    I hope you get this reply as your post has been here a number of years now.

    Cheers mate

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: