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.