I have a function in C that returns an array of pointers in a parameter
NAGR(
OUT OB_R ** pprResources,
OUT int * pnNumResources
)
No matter how I marshall it, i only get the first element of an array
of 6. I get the first element quite well with all its data correct, but
my array of pointer does not return any other element...
I'm marshalling like this:
[DllImport("whatever.dll")]
public static extern int NAGR(
[In,Out, MarshalAs(UnmanagedType.LPArray,SizeConst=6)]
IntPtr[] pprResources,
out IntPtr pnNumResources
);
Then in my code i'm retrieveing like this:
p=NAGR(rrr,out j);
if(p==0)
{
for(int iii=0;iii<j.ToInt32();iii++)
{
oo = Marshal.PtrToStructure(rrr[iii],typeof(OB_R));
}
}
After that, only rrr[0] has a valid OB_R structure, the rest is empty,
and i'm sure that there are 6 elements.
Any info will help
Thanks
David
Bart Mermuys - 19 Jan 2005 01:26 GMT
Hi,
NAGR(
OUT OB_R ** pprResources,
OUT int * pnNumResources
)
>I have a function in C that returns an array of pointers in a parameter
The function is actually returning a pointer ( OB_R* ) to the first element
in an array, you loose one pointer (*) because one pointer is used to make
it an OUT parameter.
Try this:
[DllImport("whatever.dll")]
public static extern int NAGR(
out IntPtr prResources,
out int pnNumResources);
int n;
IntPtr pRes;
p=NAGR(out pRes, out n);
if(p==0)
{
IntPtr pIt = pRes;
for(int iii=0; iii < n; iii++)
{
oo = Marshal.PtrToStructure(pIt, typeof(OB_R));
pIt = (IntPtr)(pIt.ToInt64() + Marshal.SizeOf(typeof(OB_R));
}
}
Keep in mind that if the dll assigned dynamic memory it isn't released.
HTH,
greetings
> After that, only rrr[0] has a valid OB_R structure, the rest is empty,
> and i'm sure that there are 6 elements.
[quoted text clipped - 3 lines]
>
> David
David Moreno - 19 Jan 2005 15:53 GMT
Thank you Very MUCH BART, it works nice and easy.
Bye.