Unmanaged API returning an array of pointers

G

gbraux

Hello,

I am using the Advapi32.dll CredEnumerate function through P/Invoke.
The CredEmunmerate is declared like this :

BOOL CredEnumerate(
LPCTSTR Filter,
DWORD Flags,
DWORD* Count,
PCREDENTIAL** Credentials
);

As you see, Credentials returns an array of pointers ... I know how to
work with single pointers (with PtrToStrucutre ...) but how to access
an array of pointers in C# ?

Thanks,

Guillaume
 
W

Willy Denoyette [MVP]

gbraux said:
Hello,

I am using the Advapi32.dll CredEnumerate function through P/Invoke.
The CredEmunmerate is declared like this :

BOOL CredEnumerate(
LPCTSTR Filter,
DWORD Flags,
DWORD* Count,
PCREDENTIAL** Credentials
);

As you see, Credentials returns an array of pointers ... I know how to
work with single pointers (with PtrToStrucutre ...) but how to access
an array of pointers in C# ?

Thanks,

Guillaume


You need to enumerate the buffer returned marshaling each pointer into an IntPtr, something
like this will do:

[DllImport("advapi32", SetLastError = true, CharSet=CharSet.Unicode)]
static extern bool CredEnumerate(string filter, int flag, out int count, out IntPtr
pCredentials);

.....
int count = 0;
IntPtr pCredentials = IntPtr.Zero;
IntPtr[] credentials = null;
bool ret = CredEnumerate(null, 0, out count, out pCredentials);
if (ret != false)
{
credentials = new IntPtr[count];
IntPtr p = pCredentials;
for (int n = 0; n < count; n++)
{
if(Marshal.SizeOf(p) == 4) //32 bit CLR?
p = new IntPtr(p.ToInt32() + n);
else
p = new IntPtr(p.ToInt64() + n);
credentials[n] = Marshal.ReadIntPtr(p);
}
}
else
// failed....
.....

Willy.
 
G

gbraux

Thanks Willy, it is working !

Guillaume

Willy Denoyette [MVP] a écrit :
gbraux said:
Hello,

I am using the Advapi32.dll CredEnumerate function through P/Invoke.
The CredEmunmerate is declared like this :

BOOL CredEnumerate(
LPCTSTR Filter,
DWORD Flags,
DWORD* Count,
PCREDENTIAL** Credentials
);

As you see, Credentials returns an array of pointers ... I know how to
work with single pointers (with PtrToStrucutre ...) but how to access
an array of pointers in C# ?

Thanks,

Guillaume


You need to enumerate the buffer returned marshaling each pointer into anIntPtr, something
like this will do:

[DllImport("advapi32", SetLastError = true, CharSet=CharSet.Unicode)]
static extern bool CredEnumerate(string filter, int flag, out int count, out IntPtr
pCredentials);

....
int count = 0;
IntPtr pCredentials = IntPtr.Zero;
IntPtr[] credentials = null;
bool ret = CredEnumerate(null, 0, out count, out pCredentials);
if (ret != false)
{
credentials = new IntPtr[count];
IntPtr p = pCredentials;
for (int n = 0; n < count; n++)
{
if(Marshal.SizeOf(p) == 4) //32 bit CLR?
p = new IntPtr(p.ToInt32() + n);
else
p = new IntPtr(p.ToInt64() + n);
credentials[n] = Marshal.ReadIntPtr(p);
}
}
else
// failed....
....

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top