[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

API function returning a pointer to a structure

Francois PIETTE

10/3/2003 12:19:00 PM

I have an API function (unmanaged code in a DLL). This function returns a
pointer to a structure:

typedef struct _MYSTRUCT
{
UINT Version;
char *Info;
char** InfoArray;
} MYSTRUCT;

MYSTRUCT *GetInfo(char *Name);

I have two problems:
1) How to acces the whole structure ? I guess I have to declare the function
as returning an IntPtr. But then, how to retrieve each member ?
2) How to declare and access "char **InfoArray" member

Sorry for my newbies questions...

--
francois.piette@overbyte.be
http://www.o...



3 Answers

(Mattias Sjögren)

10/3/2003 9:35:00 PM

0

Francois,

The microsoft.public.dotnet.framework.sdk groups is a better target
for this kind of question.


>1) How to acces the whole structure ? I guess I have to declare the function
>as returning an IntPtr. But then, how to retrieve each member ?

Either that, or you can use C#'s unsafe code feature and use "real"
pointers. So you either declare the the struct as

unsafe struct MYSTRUCT
{
public uint Version;
public sbyte* Info;
public sbyte** InfoArray;
}

and the function as

static unsafe extern MYSTRUCT* GetInfo(string Name);

and call it something like this

MYSTRUCT* pms = GetInfo(...);
int i = 0;
Debug.Assert( pms != null && pms->InfoArray != null );
sbyte* ps = null;
while ( (ps = pms->InfoArray[i]) != null ) {
string s = new string( ps );
Console.WriteLine( s );
i++;
}

Or you go the IntPtr and Marshal way and do it like this

struct MYSTRUCT
{
public uint Version;
public IntPtr Info;
public IntPtr InfoArray;
}

static unsafe extern IntPtr GetInfo(string Name);

IntPtr pms = GetInfo(...);
Debug.Assert( pms != IntPtr.Zero );
MYSTRUCT ms = (MYSTRUCT)Marshal.PtrToStructure( pms, typeof(MYSTRUCT)
);
Debug.Assert( ms.InfoArray != IntPtr.Zero );
int i = 0;
IntPtr ps = IntPtr.Zero;
while ( (ps = Marshal.ReadIntPtr( ms.InfoArray, i )) != IntPtr.Zero )
{
string s = Marshal.PtrToStringAnsi( ps );
Console.WriteLine( s );
i += IntPtr.Size;
}


>2) How to declare and access "char **InfoArray" member

See above.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.

Francois PIETTE

10/4/2003 8:35:00 AM

0

Thanks Mattias,

> The microsoft.public.dotnet.framework.sdk groups is a better target
> for this kind of question.

This is where I posted my message !

> >1) How to acces the whole structure ? I guess I have to declare the
function
> >as returning an IntPtr. But then, how to retrieve each member ?
>
> Either that, or you can use C#'s unsafe code feature and use "real"
> pointers.

What are the pros and cons about using unsafe code ? I've been told to not
use unsafe code as much as possible. Any suggested book or article to read ?

> Or you go the IntPtr and Marshal way and do it like this

I will try this one.

--
francois.piette@overbyte.be
http://www.o...


(Mattias Sjögren)

10/4/2003 9:44:00 AM

0

Francois,

>> The microsoft.public.dotnet.framework.sdk groups is a better target
>> for this kind of question.
>
>This is where I posted my message !

Sorry, I meant to say microsoft.public.dotnet.framework.interop


>What are the pros and cons about using unsafe code ?

Unsafe often gives you cleaner code. For example, it's nicer to use
the -> operator to dereference a pointer than Marshal.PtrToStructure.

But C#'s pointer support is somewhat limited, and you can only take
the address of types composed only of value types. With Marshal and
MarshalAs you get a bit more flexibility, as they support more complex
data types and marshaling options.


>I've been told to not use unsafe code as much as possible.

The reason for saying that is usually security. Using unsafe gives you
unverifiable code that will typically only work if run from the local
machine (not in a partially trusted context). But any time you call
unmanaged code you need full trust anyway, so in this sense it doesn't
really matter which way you do it.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.