0

我只是想从 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);
    }
   }
}
4

1 回答 1

2

本机函数使用 ANSI 字符进行操作。只需CharSet.Unicode从您的导入定义中删除。

于 2012-07-04T07:08:36.937 回答