Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

.net - Convert String^ in c# to CString in c++/CLI

I need a help on one question where I stuck while coding my app in MFC.

I am using CLR i.e Common Language Runtime in my application to integrate c# APIs. but now I stuck on converting System::String^ to CString. I am not able to do that.

I am using Following code.

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 ] );
}

now I want to convert csPass to CString. Can any one help me on this. Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

and

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.Format(str);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...