I'm writing COM to expose some unmanaged imagery generation functions to c#.
My question is how to best (easiest, clearest coding style) pass a managed
pointer to COM to receive back a loaded image:
example 1: using safearray
COM:
process([in,out] SAFEARRAY* bytes)
C# call:
byte[] bytes = byte[256];
process(bytes);
Question: does this pass a pointer, or is it marshalled (i.e inefficient)?
example 2: using fixed array size
COM:
process([in,out] byte[256] bytes]
Disadvantage: fixed size must be specified up front -- or is it safe to
write past the apparently "fixed" array size internal to the com function if
the true size of array is also passed as a parameter? (i.e in the case that
image size should vary fro 256?)
What it the current wisdom of how to do this best?
thanks in advance.
-Robert
Robert,
>Question: does this pass a pointer, or is it marshalled (i.e inefficient)?
Ir requires marshaling.
>example 2: using fixed array size
>COM:
[quoted text clipped - 4 lines]
>the true size of array is also passed as a parameter? (i.e in the case that
>image size should vary fro 256?)
Why don't you just declare it as an array without a fixed length (or a
byte* which is the same thing)?
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Robt Schnitzr - 13 Jun 2005 21:29 GMT
hi Mattias,
Thank you for your response. I tried to use various forms of the "c-style" array, however I was able to verify in the debugger that the CLR marshaller is copying the data array, (probably twice, once for in, and once for out). This performance is not satisfactory for my application.
This isn't an uncommon requirement, so I wonder what solution the dotnet team had in mind.
This is the only technique I've found to work so far without any copying:
<<COM function>>
HRESULT GetImagePixels([in] LONG bytes); //receives a pointer to where it should load the pixels
<<managed code>>
byte[] rgbdata = new byte[numBytes];
//Pin the array so it can't be moved
GCHandle pinHandle = GCHandle.Alloc(rgbdata,GCHandleType.Pinned);
try
{
//Grab pointer to element zero, as an int
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(rgbdata,0);
int ptrAsInt = ptr.ToInt32();
//Load the pixels from unmanaged COM
rdr.GetImagePixels(ptrAsInt);
}
catch{throw;}
finally{pinHandle.Free();}
> Robert,
>
[quoted text clipped - 15 lines]
>
> Mattias