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 / April 2004

Tip: Looking for answers? Try searching our database.

marshal array of structure into a com component

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael - 13 Apr 2004 00:06 GMT
Hi

two questions,
1. how to debug trace into the com componet developed using vc++ 6? I had the enable unmanaged debugging set to true in c# project
2. what's the correct way to pass an array of structure into a com method.

below is what I have so far and it is not working

Any help is appreaciated

Michae

I have a COM component which has a method like thi

getdata(int num, mystruct** structarr

the structarr is an array of structure.

in the c# program, I use the following code to call the method getdat

public IntPtr Marshalptr(mystruct[] arr

int size=Marshal.sizeOf(arr[0])

IntPtr buf=Marshal.AllocCoTaskMem(arr.length*size)

int offset=0
foreach(mystruct item in arr

marshal.StructuretoPtr(item, (IntPtr)(but.ToInt32()+offset), true)
offset+=size

return buf

the

IntPtr buf=Marshalptr(myarray)
comclass.getdata(3, buf)

now when I run the code I got an COMException

I suspect that problem lies in the marsheling, but dont' know how to trace it
Idael Cardoso - 24 Apr 2004 12:29 GMT
Hi,
Yo don't need to do any manual marshaling to call your COM object fucntion
if the object expect a struct array. The managed definition of your method
should be:

getdata(int num, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]
ref mystruct[] structarr);
//If the code of your com object will modify array element you must apply
the Out attribute.

To call that function do the following:
mystruct[] arr = new mystruct[ArraySize];
//Fill your array with useful values.
YouComObject.getdata(arr.length, ref arr);

If you can change the definition of your COM object I advise you to use
SAFEARRAYs instead of C-Style array. You can find more examples of
marshaling structures and arrays in my article:
http://www.thecodeproject.com/useritems/ManWMF.asp (C# translation of the
Windows Media Format SDK)

The error you receive is because you pass to you COM function a pointer to
mystruct* and the function is expecting mystruct** (that's why I cluded the
ref keyword). Your code could work if you do that:

IntPtr buf=Marshalptr(myarray);
GCHandle h = GCHandle.Alloc(buff, GCHandleType.Pinned);
try
{
 comclass.getdata(3, h.AddressOfPinnedObject());
}
finally
{
 h.Free();
}

Take care of who must be free the allocated memory.
Regards,
Idael.

> Hi,
>
[quoted text clipped - 39 lines]
>
> I suspect that problem lies in the marsheling, but dont' know how to trace it.

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.