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 / January 2005

Tip: Looking for answers? Try searching our database.

Help with array of pointers marshalling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dmorenos@gmail.com - 18 Jan 2005 21:57 GMT
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.

Rate this thread:







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.