none
How to use unmanaged function that takes pointer to pointer of byte array? RRS feed

  • Question

  • Hello, I am using p/invoke to use a legacy DLL that has a function that takes a pointer to a pointer.  This method allocates a byte array.  I then have to fill that byte array with data.  The C++ method signature is:

    CVIEL_API ECVL CVIEL_DECL CVLSharedMalloc(void **buffer,unsigned long size);

    I have translated that to:

            [DllImport(CvieSdk.CvieDllPath, CallingConvention = CvieSdk.CallConvention, ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = false)]
            internal static extern int CVLSharedMalloc(ref IntPtr buffer, ulong size);

    now I am trying to
    void MyMethod(byte[] originalBuffer)
    {
                    byte* inImage;
                    IntPtr inPointer = IntPtr.Zero;

                   // what to do here??
                    Marshal.AllocHGlobal(param, ptr, true);

                    CVLSharedMalloc(ref inPointer, 3072*3072*2);

                   // now I need to but data in the byte array that the DLL created..
                  Marshal.Copy(originalBuffer, inImage??, 0, 3072*3072*2);
    }


    any pointers (pun intended) on how to do this properly would be appreciated.

    thanks,

    dan

    Wednesday, September 2, 2009 5:06 PM

Answers

  • I actually figured it out, now too difficult.  here is the code in case anyone else needs it....

    internal static extern int CVLSharedMalloc(ref IntPtr buffer, ulong size);
    
    void MyMethod(byte[] originalBuffer)
    {
      IntPtr inPointer = IntPtr.Zero;
      
      CVLSharedMalloc(ref inPointer, (ulong) originalBuffer.Length);
    
       Marshal.Copy(originalBuffer, 0, inPointer, originalBuffer.BufferSize);
    
    }


    • Marked as answer by dan blanchard Wednesday, September 2, 2009 7:35 PM
    Wednesday, September 2, 2009 7:35 PM