c ++和c#之间的字符串或字符串构建器 [英] string or stringbuilder between c++ and c#
问题描述
我创建了一个在我的C ++项目中使用它的C#Dll。首先我从该dll文件注册并创建一个tlb文件,然后我在我的C ++项目中使用它:
我的c ++项目中的
:
#import.. \\\\dll \ PMI_EPD.tlbraw_interfaces_only
I将字符串传递给dll的函数,作为参数。但在dll中它是空的!
$ C $ b在C#dll代码中:
public int GetText(string ss)
{
返回5;
}
c ++代码中的
:
InterfacePtr pmi(__ uuidof (我的课));
int i;
pmi-> GetText(L2279947463,& i);
如果我在c#中更改字符串类型,StringBuilder,这是正确的。但当函数返回时崩溃。
< pre > public int GetText(StringBuilder ss)
{
return 5;
} < / pre >
我该怎么做,请帮助我。
< blockquote>我解决了自己,
$ C $ b的C ++代码,我应该通过AllocSysString()传递字符串:
CString str = L2279947463;
pmi-> GetText(str.AllocSysString(),& i);
Hi,
I Created a C# Dll that use it in my C++ project. First i register and create a tlb file from that dll file, then i use it in my C++ project:
in my c++ project:
#import "..\..\dll\PMI_EPD.tlb" raw_interfaces_only
I pass a string to function of dll, as a parameter. but in dll it is empty!
in C# dll code:
public int GetText(string ss) { return 5; }
in c++ code:
InterfacePtr pmi(__uuidof(MY_Class)); int i; pmi->GetText(L"2279947463", &i);
if i change string type in c#, StringBuilder, it will be correct. but when the function returns it crashes.
<pre>public int GetText(StringBuilder ss)
{
return 5;
}</pre>
what do i do, please help me.
I solved myself,
in C++ code , I should pass string by AllocSysString():
CString str = L"2279947463"; pmi->GetText(str.AllocSysString(), &i);
这篇关于c ++和c#之间的字符串或字符串构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!