2012-07-04 101 views
0

我只是試圖從C#中傳遞緩衝區到C DLL,讓C func填充緩衝區,然後在C#代碼中使用緩衝區做些什麼。但是我在緩衝區中回收垃圾。這裏是C:修改字符數組傳遞給C#

extern "C" __declspec(dllexport)  
    int cFunction(char *plotInfo, int bufferSize) 
    { 
     strcpy(plotInfo, "text"); 
     return(0); 
    } 

C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 
namespace ConsoleApplication1 
{ 
class Program 
{ 
[DllImport("mcDll.dll", 
    CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Unicode)] 

    public static extern int cFunction(StringBuilder theString, int bufferSize); 
    static void Main(string[] args) 
    { 
     StringBuilder s = new StringBuilder(55); 
     int result = cFunction(s, 55); 
     Console.WriteLine(s); 
    } 
    } 
} 

回答

2

本地函數與ANSI字符操作。從您的導入定義中刪除CharSet.Unicode