Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Mixed mode managed c

Back to Forum: Tech Off
  • User profile image
    FluffyDevil​Bunny

    Hi all,

    We have a mixed mode managed c dll here and I need help.

    The internals of the dll are using native c and the interface is exposing managed c so that c# can call it. We are passing a System.String into the dll and need to convert it to char*.

    Currently the developer is looping for each character in the string and placing the value into the char. There has got to be a better way.

    Anyone?

    Thanks

    FDB

  • User profile image
    Minh

    http://support.microsoft.com/?kbid=311259

    disclaimer: C/C++ is yucky to me Tongue Out

  • User profile image
    FluffyDevil​Bunny

    I have my answer. I will post it so that it is in the archives incase anyone else needs it.

    System::String^ CString2DotNetOut()
    {

              char* cString = "20 chars for testing";
              System::String^ sString = System::Runtime::InteropServices::Marshal::PtrToStringAnsi(System::IntPtr(cString));
              return sString;
    }

    void DotNetString2CStringOut(System::String^ sString)
    {

              char* cString = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sString).ToPointer();
              cout << cString;
    }

    FDB

  • User profile image
    amotif

    Don't forget to free the allocation made by StringToHGlobalAnsi... Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.