I have a C function that I would like to call from C# program. Function compresses inputted byte array and outputs new compressed array. It looks like this:
extern __declspec(dllexport) int Compress(chandle handle,
unsigned char *inputBuf, unsigned char **outputBuf, unsigned long *outputSize);
I have translated it into this on C# part. But output I get is array with one item.
[DllImport("compresslib.dll", CallingConvention = CallingConvention.Cdecl)]
internal extern static int Compress(IntPtr handle, byte[] input, out byte[] output, out uint outputSize);
What should I do to get this working?
Here is working code I was able to write with the help of Hans Passant
[DllImport("compresslib.dll", CallingConvention = CallingConvention.Cdecl)]
internal extern static int Compress(IntPtr handle, byte[] input, out IntPtr output, out uint outputSize);
// and this is how i call it
byte[] outputData;
int outputDataSize;
IntPtr outputDataP = IntPtr.Zero;
try
{
int success = NativeMethods.Compress(handle,
inputData, out outputDataP, out outputDataSize);
if (success == -1)
{
throw new Exception("Compression failed.");
}
outputData = new byte[outputDataSize];
Marshal.Copy(outputDataP , outputData , 0, (int)outputDataSize);
}
finally
{
if (outputDataP != IntPtr.Zero)
NativeMethods.tjFree(outputDataP);// release unmanaged buffer
}
return outputData ;