如何将'System :: String ^'转换为'TCHAR'? [英] How do you convert a 'System::String ^' to 'TCHAR'?

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

问题描述

我提出了一个问题这里涉及C ++和C#沟通。

i asked a question here involving C++ and C# communicating. The problem got solved but led to a new problem.

这会传回一个String(C#)

this returns a String (C#)

return Marshal.PtrToStringAnsi(decryptsn(InpData));

这需要TCHAR *(C ++)

this expects a TCHAR* (C++)

lpAlpha2[0] = Company::Pins::Bank::Decryption::Decrypt::Decryption("123456");

我已经google了如何解决这个问题,但我不知道为什么String有胡萝卜(^)就可以了。最好将String的返回值更改为C ++接受的值吗?或者我需要在赋值之前做一个转换。

i've googled how to solve this problem, but i am not sure why the String has a carrot(^) on it. Would it be best to change the return from String to something else that C++ would accept? or would i need to do a convert before assigning the value?

推荐答案

这是一个真正的问题,如果你意味着如何将 String ^ 转换为 char * ,那么你使用以前使用的相同编组器,只返回:

That is a really rambling way to ask the question, but if you mean how to convert a String ^ to a char *, then you use the same marshaller you used before, only backwards:

char* unmanagedstring = (char *) Marshal::StringToHGlobalAnsi(managedstring).ToPointer();

编辑:不要忘记释放完成使用 Marshal :: FreeHGlobal

don't forget to release the memory allocated when you're done using Marshal::FreeHGlobal.

这篇关于如何将'System :: String ^'转换为'TCHAR'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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