c++提供的函数原型
DELIB_API void DEEncode(BYTE** pSrc, BYTE** pDest, int* pSize, int nCount, int nMethod, unsigned long lKey);请问c#调用时,BYTE**类型参数如何声明。
DELIB_API void DEEncode(BYTE** pSrc, BYTE** pDest, int* pSize, int nCount, int nMethod, unsigned long lKey);请问c#调用时,BYTE**类型参数如何声明。
然后用的时候int x = 2;
int y = 4;
byte[] array = new byte[x * y];
//假设你的数据是2*4的,具体赋值我也剩了
IntPtr[] buffers = new IntPtr[x];
for (int i = 0; i < x; i++)
{
buffers[i] = Marshal.AllocHGlobal(y * sizeof(byte));
Marshal.Copy(array, i * y, buffers[i], y);
}//调用你的函数for (int i = 0; i < x; i++)
{
Marshal.FreeHGlobal(buffers[i]);
}
数据可以看成是一个数组,数组里的元素为Byte[],你给的例子满足不了啊
Byte[] pSrc1 = new Byte[]{97,98};
Byte[] pSrc2 = new Byte[]{101,102};
把pSrc1 pSrc2组合成一个参数,对应c++里的BYTE**类型。
Byte[] pSrc2 = new Byte[]{101,102};
这2句后还有其他处理才调用函数的, 贴出来才行.
Encoding Ecode = Encoding.GetEncoding("GB2312");
Byte[] pSrc1, pSrc2;
pSrc1 = Ecode.GetBytes(str2);
string str1 = "opqrst";
pSrc2 = Ecode.GetBytes(str1); IntPtr[] pSrc = new IntPtr[2];
pSrc[0] = Marshal.AllocHGlobal(7 * sizeof(byte));
Marshal.Copy(pSrc1, 0, pSrc[0], 7);
pSrc[1] = Marshal.AllocHGlobal(6 * sizeof(byte));
Marshal.Copy(pSrc2, 7, pSrc[1], 6);
按照四楼的做,直接就报错了。“其他信息: 请求的范围扩展超过了数组的结尾。”为pSrc[1]赋值时报错
Encoding Ecode = Encoding.GetEncoding("GB2312");
Byte[] pSrc1, pSrc2;
pSrc1 = Ecode.GetBytes(str2);
string str1 = "opqrst";
pSrc2 = Ecode.GetBytes(str1); IntPtr[] pSrc = new IntPtr[2];
pSrc[0] = Marshal.AllocHGlobal(7 * sizeof(byte));
Marshal.Copy(pSrc1, 0, pSrc[0], 7);
pSrc[1] = Marshal.AllocHGlobal(6 * sizeof(byte));
Marshal.Copy(pSrc2, 7, pSrc[1], 6);
public static extern void DEEncode(IntPtr[] pSrc, IntPtr[] pDest, int[] pSize, int nCount, int nMethod, long lKey);
//public static extern void DEEncode(string[] pSrc,StringBuilder[] pDest, int[] pSize, int nCount, int nMethod, long lKey);
[DllImport("..\\EnAndDeFun.dll", CharSet = CharSet.Unicode)]
public static extern void DEDecode(IntPtr[] pSrc, IntPtr[] pDest, int[] pSize, int nCount, int nMethod, long lKey);
//public static extern void DEDecode(string[] pSrc, StringBuilder[] pDest, int[] pSize, int nCount, int nMethod, long lKey);
//将输入字符串转为 Byte[]
string str1 = textBox1.Text;
Encoding Ecode = Encoding.GetEncoding("GB2312");
Byte[] pSrc1, pSrc2;
pSrc1 = Ecode.GetBytes(str1);
string str2 = textBox2.Text;
pSrc2 = Ecode.GetBytes(str2); //计算Byte[] 长度
int nLen1 = pSrc1.GetLength(0);
int nLen2 = pSrc2.GetLength(0); int[] n = new int[] { nLen1,nLen2 }; //将Byte[] 转为地址形式
IntPtr p1 = Marshal.AllocHGlobal(nLen1 * sizeof(byte));
Marshal.Copy(pSrc1, 0, p1, nLen1);
IntPtr p2 = Marshal.AllocHGlobal(nLen2 * sizeof(byte));
Marshal.Copy(pSrc2, 0, p2, nLen2); //准备传入和传出 参数 地址数组
IntPtr[] pSrc = new IntPtr[] { p1, p2 };
IntPtr[] pDest = new IntPtr[2]; //为传出参数分配大小
pDest[0] = Marshal.AllocHGlobal(nLen1 * sizeof(byte));
pDest[1] = Marshal.AllocHGlobal((nLen2 - 1) * sizeof(byte)); Byte[] bt1 = new Byte[nLen1];
Byte[] bt2 = new Byte[nLen2 - 1]; newinstance.DEEncodeFun(pSrc, pDest, n, 2, 1, 2); //将传出的地址转为Byte[]
Marshal.Copy(pDest[0], bt1, 0, nLen1);
Marshal.Copy(pDest[1], bt2, 1, nLen2 - 1); //MessageBox显示结果
str1 = Encoding.Default.GetString(bt1);
str1 = "pDest[0] = " + str1;
str2 = Encoding.Default.GetString(bt2);
str2 = "pDest[1] = " + str2;
string str = str1 + "\n" + str2;
MessageBox.Show(str); //释放内存
for (int i = 0; i < 2; i++ )
{
Marshal.FreeHGlobal(pSrc[i]);
Marshal.FreeHGlobal(pDest[i]);
}