473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Unmanaged API returning an array of pointers

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

Dec 1 '06 #1
2 4414
"gbraux" <gb****@supinfo.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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.

Dec 1 '06 #2
Thanks Willy, it is working !

Guillaume

Willy Denoyette [MVP] a écrit :
"gbraux" <gb****@supinfo.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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.
Dec 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Weston Fryatt | last post by:
(Sorry for spamming multiple groups, But I need a solution to this problem) I think this should be a simple question on Memory Allocation in a managed DLL and passing a memory pointer over to an...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
5
by: Saya | last post by:
Hi, Please help with the following: How to implement a C++ code like this: MediaFunction( char* filename, void* info, int* size, void** buffer, int first, int* last);
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.