I have a COM dll written in unmanaged C++ that I call from C#. There is a method I call that I pass a buffer to that it then fills in. When it is fixed length it works, when it's variable length it fails with a "access outside of array bounds" error.
Here is the fixed length that works:
C#
PATTERNSLib.PatternDraw p2 = new PATTERNSLib.PatternDraw();
Byte[] buf = new Byte[X * Y * 32 / 8]; // X=756, Y=360 in this case
p2.DrawIntoBuffer(buf, X, Y);
IDL
[id(53), helpstring("method DrawIntoBuffer")]
HRESULT DrawIntoBuffer([in, out] unsigned char buf[756*360*32/8], [in] int width,
[in] int height); // size hard coded which is a problem
C++
STDMETHODIMP CPatternDraw::DrawIntoBuffer(unsigned char *buf, int width, int height)
Here is my attempt at a variable length array that fails:
C#
PATTERNSLib.PatternDraw p2 = new PATTERNSLib.PatternDraw();
Byte[] buf = new Byte[X * Y * 32 / 8]; // Goal is variable length
p2.DrawIntoBuffer(ref buf[X * Y * 32 / 8], X, Y); // compiler error indicated ref was required
IDL
[id(53), helpstring("method DrawIntoBuffer")]
HRESULT DrawIntoBuffer([in, size_is(width*height*32/8), out] unsigned char *buf,
[in] int width, [in] int height);
C++
STDMETHODIMP CPatternDraw::DrawIntoBuffer(unsigned char *buf, int width, int height)