我被困在c#实现方面,因为我对它很陌生。问题是,我想从c#代码传递一个“指针”(有内存),这样我的c ++应用程序就可以将pchListSoftwares缓冲区复制到pchInstalledSoftwares。我无法弄清楚如何从c#侧传递指针。
本机c ++代码(MyNativeC ++ DLL.dll)
void GetInstalledSoftwares(char* pchInstalledSoftwares){
char* pchListSoftwares = NULL;
.....
.....
pchListSoftwares = (char*) malloc(255);
/* code to fill pchListSoftwares buffer*/
memcpy(pchInstalledSoftwares, pchListSoftwares, 255);
free(pchListSoftwares );
}
传递简单的“字符串”无效...
C#实施
[DllImport("MyNativeC++DLL.dll")]
private static extern int GetInstalledSoftwares(string pchInstalledSoftwares);
static void Main(string[] args)
{
.........
.........
string b = "";
GetInstalledSoftwares(0, b);
MessageBox.Show(b.ToString());
}
非常感谢任何形式的帮助......
答案 0 :(得分:3)
尝试使用StringBuilder
[DllImport("MyNativeC++DLL.dll")]
private static extern int GetInstalledSoftwares(StringBuilder pchInstalledSoftwares);
static void Main(string[] args)
{
.........
.........
StringBuilder b = new StringBuilder(255);
GetInstalledSoftwares(0, b);
MessageBox.Show(b.ToString());
}
答案 1 :(得分:1)
我的错误...在致电GetInstalledSoftwares(0, b);
时删除0。
答案 2 :(得分:0)
尝试将原型行更改为:
private static extern int GetInstalledSoftwares(ref string pchInstalledSoftwares);
(通过引用发送字符串)。