开发者

PInvoke problem

This is the signature of the native c method:

bool nativeMethod1
(unsigned char *arrayIn,
unsigned int arrayInSize,
unsigned char *arrayOut,
unsigned int *arrayOutSize);

I have no idea why arrayOutSize is a pointer to unsigned int but not int itself.

This is how I invoke it from C#:

byte[] arrayIn= Encoding.UTF8.GetBytes(source);
uint arrayInSize = (uint)arrayIn.Length;
byte[] arrayOut = new byte[100];
uint[] arrayOutSize = new uint[1];
arrayOutSize[0] = (uint)arrayOut.Length;

fixed (byte* ptrIn = arrayIn, ptrOut = arrayOut)
{
    if (nativeMethod1(ptrIn, arrayInSize, ptrOut, arrayOutSize))
    {
        Console.WriteLi开发者_Python百科ne("True");
    }
    else
    {
        Console.WriteLine("False");
    }
}

and some DllImport code

[DllImport(@"IcaCert.dll", EntryPoint = "CreateCert2", CallingConvention = CallingConvention.Cdecl)]<br>
public unsafe static extern bool CreateCert2WithArrays(
        byte* data, uint dataSize,<br>
        byte* result, uint[] resultSize);

According to the documentation, native method should return arrayOut fulfilled with the values depending on arrayIn. If its size is less than needed, it returns false. True otherwise. I figured that it's needed 850 elements in arrayOut. So, when I create new byte[100] array, function should return false, but it always returns true. WHY?


You don't need unsafe code and fixed here. The standard P/Invoke marshaller is more than up to the task:

[DllImport(@"IcaCert.dll", EntryPoint = "CreateCert2", CallingConvention = CallingConvention.Cdecl)]
public static extern bool CreateCert2WithArrays(
    byte[] arrayIn, 
    uint arrayInSize,
    byte[] arrayOut,
    ref uint arrayOutSize
);

byte[] arrayIn = Encoding.UTF8.GetBytes(source);
uint arrayInSize = (uint)arrayIn.Length;
uint arrayOutSize = 0;

CreateCert2WithArrays(arrayIn, arrayInSize, null, ref arrayOutSize);
byte[] arrayOut = new byte[arrayOutSize];
CreateCert2WithArrays(arrayIn, arrayInSize, arrayOut, ref arrayOutSize);

I don't know for sure what the protocol of the function is, but it is normal for such functions to be able to receive NULL if the output array has size 0.


I don't think an array is what you're looking for. It's a pointer to the size of the array, not a pointer to an array of sizes. Try this:

[DllImport(@"IcaCert.dll", EntryPoint = "CreateCert2", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern bool CreateCert2WithArrays(
byte* data, uint dataSize,
byte* result, ref uint resultSize);

byte[] arrayIn= Encoding.UTF8.GetBytes(source);

uint arrayInSize = (uint)arrayIn.Length;
byte[] arrayOut = new byte[100];
uint arrayOutSize = (uint)arrayOut.Length; 
CreateCert2WithArrays (arrayIn, (uint) arrayIn.Length, arrayOut, ref arrayOutSize);
uint[] arrayOutSize = new uint[1];
arrayOut = new byte[(int)arrayOut];
CreateCert2WithArrays (arrayIn, (uint) arrayIn.Length, arrayOut, ref arrayOutSize);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜