如何将char数组转换为const char * [英] How do I convert a char array to const char*

查看:1483
本文介绍了如何将char数组转换为const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要将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

推荐答案

你只需要插入const限定符:

InformDataContract-> UniqueID = Marshal :: PtrToStringAnsi((IntPtr)(const char *)UniqueID);



确保您不仅指向C ++中的字符串,而且还拥有C ++中的缓冲区。



你需要在C#端创建一个自己的副本,因为它是C ++环境的只读内存。
you only need to insert the const qualifier:
InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (const char *)UniqueID );

Ensure that you not only pointing to the string in C++ but owning the buffer in your C++.

And you need to make a own copy on the C# side, because it is read only memory of the C++ environment.


大家好,



我能够解决我的问题。我的问题与从一个dll到另一个dll访问外部变量

有关。虽然我在Managed C ++标头中包含本机C ++标头,但它不接受。因此,我通过在其他适当的本机C ++文件中添加本机头文件并使其正常工作来解决这个问题。



感谢您的支持和帮助。 />
Sudhakar M
Hi All ,

I am able to resolve my problem. My problem is related to accessing a external variable
from one dll to another.while I am including native C++ headers in Managed C++ headers , it is not accepting. Hence I resolved this by addding the native header file in some other appropriate native C++ file and make it work as I need.

Thanks for your support and Help.
Sudhakar M


Sudhakar;



我知道你修复了你的链接问题。这是对类型转换的另一种看法。



Sudhakar;

I know you fixed your link problem. Here's a different take on the typecast.

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 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆