Convert String^ in c# to CString in c++/CLI

Got My answer.
Thanks for your support @Elliot Tereschuk.

I have gone through some references like

  1. How to: Extend the Marshaling Library
  2. Overview of Marshaling in C++
  3. For CString.Format()


include header files

#include <msclr/marshal_windows.h>
#include <msclr/marshal.h>

using Library
using namespace msclr::interop;

And finally My source code is.

String^ csPass = gcnew String(strPassword.GetBuffer());
array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass);
for (int i = 0; i < Value->Length; i++ )
csPass += String::Format( "{0:X2}", Value[ i ] );

marshal_context^ context = gcnew marshal_context();

const char* str = context->marshal_as<const char*>(csPass);


csMypass is a CString type Variable.
Thank you for support.

Leave a Comment