markiemooster

I've created a COM object that returns a SAFEARRAY of image data. My C#
app receives this as a byte[] array and then passes the data to a second,
third-party, ActiveX component for display.

Well I say it passes the data, it doesn't. The third party ActiveX object
returns a pointer for me to copy the data to and I attempt to copy the data
to it in an unsafe code block using Marshal.Copy() as shown below:

byte[] data; // Image data received from my COM object
....
IntPtr ptrImageData; // Pointer to address for image data provide by 3rd
party activeX
...
...
Marshal.Copy(data, 0, ptrImageData, data.Length);

This is where it all goes wrong. The c# app exits abruptly with code
0xc0000005. I'm unable to debug the C# app beyond Marshal.Copy().

The data returned in the SAFEARRAY (byte[]) from my COM object appears to have been populated correctly so I believe it to be fine.


I've been completely stumped. Is it something to do with the SAFEARRAY data
still being locked when it is received as the byte[] array

Please help. Thanks.



Re: Visual C# General SAFEARRAYs, C# and Marshal.Copy()

Mario Cossi

Looks like you have an access violation. Are you sure that the pointer you get from the ActiveX points to a buffer that is large enough

--mc





Re: Visual C# General SAFEARRAYs, C# and Marshal.Copy()

Peter Ritchie

If your COM method signature is something like:

Code Snippet

HRESULT DoSomething([in] SAFEARRAY( unsigned char ) ar);

Then you can have it marshaled when you define the managed interface:

Code Snippet

void DoSomething([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_I1)] int[] ar) ;






Re: Visual C# General SAFEARRAYs, C# and Marshal.Copy()

markiemooster

I assume you mean marshalling manually in the MSIL code. I generated the interop using tlbimp.exe without using the /sysarray option.

My IDL code is like this:

HRESULT GetSomething([out] SAFEARRAY(BYTE)* data);

and tlbimp.exe produces an interop with this signature:

void GetSomething(out byte[] data);





Re: Visual C# General SAFEARRAYs, C# and Marshal.Copy()

Peter Ritchie

Yuck, I thought tlbimp would have been smarter than that. You'd have to re-implement all the interfaces that tlbimp generated in order to do what I outlined.






Re: Visual C# General SAFEARRAYs, C# and Marshal.Copy()

markiemooster

To clarify, I meant that when I view the interop DLL as an object in the Object Browser in VS2005 it portrays the class as having the method I outlined above. I'm NOT referring to MSIL code. Tlbimp.exe produces a DLL and not an IL file.

Is this what you'd expect