2
這是從的libeay32.dll(openssl project)功能:ç無符號`字符** out`到C#`的byte []`
int i2o_ECPublicKey (EC_KEY * key, unsigned char ** out)
如何描述它在C#中(如果我想獲得一個字節[])?
代碼:
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static int i2o_ECPublicKey (IntPtr encKey, StringBuilder outPar);
我不喜歡這一點,因爲我覺得這個結果是unicode。
回答
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static int i2o_ECPublicKey(IntPtr encKey, ref IntPtr outPar);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int i2o_ECPublicKey(IntPtr encKey, int outPar);
//Pass *out as null for required buffer length.
int reqLen = i2o_ECPublicKey(k, 0);
Byte[] outBuf = new Byte[reqLen];
IntPtr unmanagedOut = Marshal.AllocCoTaskMem(outBuf.Length);
int res = i2o_ECPublicKey(k, ref unmanagedOut);
if (res == reqLen)
{
unmanagedOut -= reqLen; // because i2o_ECPublicKey add size to unmanagedOut
Marshal.Copy(unmanagedOut, outBuf, 0, outBuf.Length);
}
Marshal.FreeCoTaskMem(unmanagedOut);