I have a native C++ function that I have to call from my C# code, the C++ function looks like this:
__declspec(dllexport) int getCameraFrame(void *buffer);
Calling the function in C++ looks like this:
unsigned char* data = new unsigned char[640 * 480 * 4];
getCameraFrame(data);
From C# I import the function with PInvoke as:
[DllImport("CameraSDK.dll")]
public static extern int getCameraFrame(???);
What do I use as a parameter here, and how would I call the function from my C# code?
byte[] buffer;
getCameraFrame(out buffer); //???
UPDATE
I have tried using string, byte[], char[], IntPtr as parameters but I keep getting a PInvokeStackImbalance exception.
A call to PInvoke function 'Test!Test.Form1::getCameraFrame' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
UPDATE - Solved
With the answers given by David and Michael, I've managed to solve my issue with the below code
[DllImport("CameraSDK.dll", CallingConvention.Cdecl)]
public static extern int getCameraFrame([Out] StringBuilder buffer);
StringBuilder data = new StringBuilder(1024 * 768 * 4)
int result = getCameraFrame(data);
David, your answer was indeed correct, note that my StringBuilder init is 1024 * 768 * 4 (and not 640 * 480 * 4 - this is what was throwing the AccessViolationException) - in the C++ code the buffer is scaled from (1024 * 768 * 4) to (640 * 480 * 4) - I must have missed that part, so, that's my bad.
I'm able to get the raw data but cannot convert it to a bitmap (raw data is in some weird format) but that is another question.
Thanks