2
我有一個用C#調用的非託管C++寫的COM dll。我調用的方法是傳遞一個緩衝區,然後填充它。當它的長度固定時,它的工作方式是可變的,當它是可變長度時,它將失敗,並出現「訪問數組邊界之外」錯誤。將可變長度的字節數組從C#傳遞到非託管C++ dll
這裏是固定長度的工作原理:
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)
這是我在一個可變長度數組失敗嘗試:
C#
PATTERNSLib.PatternDraw P2 =新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)