【发布时间】:2012-07-04 17:24:52
【问题描述】:
(学习 c# 的第 2 天)我正在将缓冲区从 C# 传递给 C dll。 C 函数将字符串“text”复制到缓冲区中。回到 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.Ansi)]
public static extern int cFunction(StringBuilder theString, int bufSize);
static void Main(string[] args)
{
StringBuilder s = new StringBuilder(55);
int result = cFunction(s, 55);
Console.WriteLine(s);
string zz = "text";
if (s.Equals(zz))
Console.WriteLine( "strings compare equal");
else
Console.WriteLine("not equal");
Console.ReadLine();
}
}
}
【问题讨论】:
标签: c# c string dll marshalling