C# 调用 API调用成功但是结果不对,请高手指教
C++编写的API
头文件为
extern "C"
{
__declspec(dllexport) char* PinJie(char* pA, char* pB);
}
CPP中的函数为,目的是连接连个字符串
char* PinJie(char* pA, char* pB)
{
int nA = strlen(pA);
int nB = strlen(pB);
int nOut = nA + nB;
char* pOut = new char [nOut + 1];
if(pOut != NULL)
{
memcpy(pOut, pA, nA);
memcpy(pOut + nA, pB, nB);
pOut[nOut] = 0;
}
return pOut;
}
我在C#中是这样调用的
using System.Runtime.InteropServices;
namespace ImportDll
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
[DllImport("PinJie.dll",ExactSpelling = true,CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
unsafe public static extern char* PinJie(char* a, char* b);
private void Page_Load(object sender, System.EventArgs e)
{
unsafe
{
char str_a = '1';
char str_b = '2';
char* chr_a = &str_a;
char* chr_b = &str_b;
Page.Response.Write(*PinJie(chr_a,chr_b));
}
}
}
}
我想返回的结果是12也就是字符串连接,但是现在返回的东西比较怪异不知道为什么在哪里,请高手指教