如何使用COM从C#中的字符串传递给C ++? [英] How to use COM to pass a string from c# to c++?
本文介绍了如何使用COM从C#中的字符串传递给C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试从C ++调用C#代码,我也跟着从这篇文章说明
when i try to call c# code from c++, i followed instructions from this article
的 http://support.microsoft.com/kb/828736
我的C#的部分是:
[Guid("6A2E9B00-C435-48f8-AEF1-747E9F39E77A")]
public interface IGameHelper
{
void getInfo(out string result);
}
public class GameHelper : IGameHelper
{
void getInfo(out string result)
{
result = new StringBuilder().Append("Hello").ToString();
}
}
我的C ++代码的一部分:
part of my c++ code:
#import "../lst/bin/Release/LST.tlb" named_guids raw_interfaces_only
using namespace LST;
using namespace std;
...
HRESULT hr = CoInitialize(NULL);
IGameHelperPtr pIGame(__uuidof(GameHelper));
BSTR ha = SysAllocString(NULL);
pIGame->GetInfo(&ha);
wprintf(_T(" %s"),ha);
SysFreeString(ha);
但我不能得到字符串结果值,它工作正常,当我试图让整数结果,但不串。
but I just cannot get the string result value, it works fine when i try to get integer results,but not string.
我不知道COM非常多。请帮帮我。
谢谢
I dont know COM very much. PLEASE HELP ME. Thank you.
推荐答案
C#代码更改为:
[Guid("6A2E9B00-C435-48f8-AEF1-747E9F39E77A")]
public interface IGameHelper
{
string getInfo();
}
public class GameHelper : IGameHelper
{
public string getInfo()
{
return "Hello World";
}
}
这时你的C ++客户端:
Then your C++ client to:
HRESULT hr = CoInitialize(NULL);
IGameHelperPtr pIGame(__uuidof(GameHelper));
_bstr_t ha = pIGame->GetInfo();
wprintf(_T(" %s"),ha);
这应该工作
这篇关于如何使用COM从C#中的字符串传递给C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文