(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();
}
}
}