我只是想从 C# 将缓冲区传递给 C dll,让 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);
}
}
}