I have a problem passing void pointer from unmanaged code to managed. There is a function's pointer in .cpp file
TESTCALLBACK_FUNCTION testCbFunc;
TESTCALLBACK_FUNCTION takes C++ structure
typedef void (*TESTCALLBACK_FUNCTION )(TImage image);
struct TImage
{
int Width; //width
int Height; //height
void *Buf; //data buffer
};
C# function and structure
public void TImageReceived(TImage image)
{
// logic
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
public struct TImage
{
public int Width;
public int Height;
public IntPtr Buf;
}
TImageReceived passed to unmanaged code and when it's called I receive exception.
System.Runtime.InteropServices.SafeArrayTypeMismatchException
If I passed NULL in field Buf from unmanaged code everything will works fine.
I know about MarshalAs atrribute, but the problem is that I cannot use SizeConst because Buf size is always different. But it always has size of Width*Height.
[MarshalAs(UnmanagedType.ByValArray,SizeConst=???)]
How to cast void* from unmanaged to managed code?