Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

interop: Passing pointer to COM to efficiently receive large data chunk

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Schnitzer - 12 Jun 2005 14:21 GMT
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
Mattias Sjögren - 13 Jun 2005 21:13 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.