2010-12-08 95 views
2

我被困在c#实现端,因为我对它很新颖。问题是,我想从c#代码传递一个'指针'(有内存),这样My C++应用程序就可以将pchListSoftwares缓冲区复制到pchInstalledSoftwares。我无法弄清楚如何从C#端传递指针。将C#中的字符指针传递给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()); 
} 

任何形式的帮助非常感谢...

回答

2

尝试使用一个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

我的错误...删除0致电GetInstalledSoftwares(0, b);

+0

干得好(计算出来) – 2010-12-08 16:28:29

0

尝试改变原型行:

private static extern int GetInstalledSoftwares(ref string pchInstalledSoftwares); 

(通过引用发送字符串)。

相关问题