首页 > 代码库 > C#调用C++

C#调用C++

c++

extern "C" __declspec(dllexport) char* WINAPI base64_decode( char *data,char base[]){}

C#

private void Form1_Load(object sender, EventArgs e){    IntPtr ptrIn1 = Marshal.StringToHGlobalAnsi("这是一个中文base64加密的测试");    IntPtr ptrIn2 = Marshal.StringToHGlobalAnsi("01234abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ56789+/=");    IntPtr ptrIn3 = base64_encode(ptrIn1, ptrIn2);    string retlust1 = Marshal.PtrToStringAnsi(ptrIn3);    IntPtr ptrIn4 = Marshal.StringToHGlobalAnsi(retlust1);    IntPtr ptrIn5 = Marshal.StringToHGlobalAnsi("01234abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ56789+/=");    IntPtr ptrIn6 = base64_decode(ptrIn4, ptrIn5);    string retlust2 = Marshal.PtrToStringAnsi(ptrIn6);}        [DllImport("FwBase64.dll")]private static extern IntPtr base64_encode(IntPtr a, IntPtr b);[DllImport("FwBase64.dll")]private static extern IntPtr base64_decode(IntPtr a, IntPtr b);

 

注意:传参数用地址的方式。

C#调用C++