问题描述
大家好,
我需要将char数组,char UniqueID [88]转换为const char *
实际上有一个数据契约类型字符串,C#中的UniqueID。
我在另一个文件中有一个extern char UniqueID [LEN],我可以在我的cpp文件中访问
,我们需要将它从托管C ++传递给C#。 br />
所以我想按照以下方式做。但是不能成功。
DataContracts :: InformDataContract ^ InformDataContract = gcnew DataContracts :: InformDataContract();
InformDataContract-> UniqueID = Marshal :: PtrToStringAnsi((IntPtr)(char *)UniqueID);
请告诉我您对此的建议/想法。
谢谢,
Sudhakar
Hi All ,
I need to convert a char array, char UniqueID[88] to a const char*
actually there is a data contract type string, UniqueID in C#.
and I have a extern char UniqueID[LEN] in another file that i can access in my cpp file
and we need to pass it from managed C++ to C#.
so I am trying to do as follows. but not able to succeed.
DataContracts::InformDataContract^ InformDataContract=gcnew DataContracts::InformDataContract();
InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (char *)UniqueID );
Please let me know your suggestions /ideas on this.
Thanks,
Sudhakar
推荐答案
InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (char *)UniqueID );
由于UniqueID被声明为一个简单的数组,例如。
Since UniqueID is declared as a simple array, eg.
char UniqueID[88];
你只需要一个类型转换,就像这样。
You only need one typecast, like so.
InformDataContract->UniqueID = Marshal::PtrToStringAnsi((IntPtr)UniqueID );
如果UniqueID是一个CString,你想把它强制转换为
If UniqueID was a CString, you'd want to typecast it to
const char *
首先,但不是必须进行额外的类型转换。
first, but it's not so the extra typecast is unnecessary.
这篇关于如何将char数组转换为const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!