Moore's Mind

Saturday, September 24, 2005

PeerGetNextItem Trick

I'm wrapping the PeerCollab SDK using C#/DllImport.

I was struggleing with PeerGetNextItem returning garbage data until I finally realized its was returning an array of pointers.

HRESULT WINAPI PeerGraphGetNextItem(
IN HPEERENUM hPeerEnum,
IN OUT PULONG pCount,
OUT PVOID * ppvItems);

[DllImport("p2p.dll", PreserveSig=true)]
internal static extern uint PeerGetNextItem(IntPtr hPeerEnum, ref uint count, ref IntPtr ppItems);

To call this function and extract the data, you'll need to do the following...

uint ulItem = 1;
IntPtr capptr = IntPtr.Zero;
uint result = PeerCollabNative.PeerGetNextItem(hPeerEnum, ref ulItem, ref capptr );

The trick is to marshal the first item in the array as a pointer before marshalling the type...

IntPtr itemptr = Marshal.ReadIntPtr(capptr);
PEER_CAPABILITY ncap = (PEER_CAPABILITY)Marshal.PtrToStructure(itemptr, typeof(PEER_CAPABILITY));

Hope this helps someone.

0 Comments:

Post a Comment

<< Home