PInvoke & structures

A

Andrea

Hello everybody,

I have the followng problem using PInvoke:

Consider the following C structure

typedef struct ATTRIBUTE {
unsigned long type;
void* pValue;
unsigned long ulValueLen;
};

that is used in the following way

char[] alias = "pippo";
bool token = true;

ATTRIBUTE Template[] =
{
{ 0, &token, sizeof(token)},
{ 1, alias, strlen(alias)},
};

this array of ATTRIBUTE has to be passed to such function as
int C_FindObjectsInit(ATTRIBUTE [] attrArr);



If I "traslate" the previous C structure in C# as
[StructLayout(LayoutKind.Sequential)]

public struct ATTRIBUTE

{

public UInt32 type;

public IntPtr pValue;

public UInt32 ulValueLen;

}

I have problems using the C# structure ( in the second field of the
structure) :

ATTRIBUTE[] a = new ATTRIBUTE();

a.type = 0;

a.pValue = ????

a.ulValueLen = 5;

Any idea ? Do I declare the structure wrongly ?

Thank you
Andrea
 
N

Nicholas Paldino [.NET/C# MVP]

Andrea,

You aren't declaring the structure incorrectly (although I would use a
CLS compliant type like int, and make sure that the MarshalAs attribute was
set correctly, but that is just me).

The problem comes from the second field. You will have to custom
marshal the field in most cases, as it is a pointer to void. In each case,
you will have to allocate memory for the variable (the boolean value, and
the string), and then get a pointer to that. Once you have that, you can
set the value of the second field to that pointer.

This will require a few calls to the static methods on the Marshal class
(AllocCoTaskMem, FreeCoTaskMem, WriteByte).

You can alleviate this somewhat by using unsafe code. This will allow
you to just set the second field to the pointers that you want. However,
there is a reason it is called unsafe =) Whether or not it is a good idea
for you to use it is something you have to determine for yourself.

Hope this helps.
 
A

Andrea

Thank you very much
Andrea
--

Nicholas Paldino said:
Andrea,

You aren't declaring the structure incorrectly (although I would use a
CLS compliant type like int, and make sure that the MarshalAs attribute was
set correctly, but that is just me).

The problem comes from the second field. You will have to custom
marshal the field in most cases, as it is a pointer to void. In each case,
you will have to allocate memory for the variable (the boolean value, and
the string), and then get a pointer to that. Once you have that, you can
set the value of the second field to that pointer.

This will require a few calls to the static methods on the Marshal class
(AllocCoTaskMem, FreeCoTaskMem, WriteByte).

You can alleviate this somewhat by using unsafe code. This will allow
you to just set the second field to the pointers that you want. However,
there is a reason it is called unsafe =) Whether or not it is a good idea
for you to use it is something you have to determine for yourself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrea said:
Hello everybody,

I have the followng problem using PInvoke:

Consider the following C structure

typedef struct ATTRIBUTE {
unsigned long type;
void* pValue;
unsigned long ulValueLen;
};

that is used in the following way

char[] alias = "pippo";
bool token = true;

ATTRIBUTE Template[] =
{
{ 0, &token, sizeof(token)},
{ 1, alias, strlen(alias)},
};

this array of ATTRIBUTE has to be passed to such function as
int C_FindObjectsInit(ATTRIBUTE [] attrArr);



If I "traslate" the previous C structure in C# as
[StructLayout(LayoutKind.Sequential)]

public struct ATTRIBUTE

{

public UInt32 type;

public IntPtr pValue;

public UInt32 ulValueLen;

}

I have problems using the C# structure ( in the second field of the
structure) :

ATTRIBUTE[] a = new ATTRIBUTE();

a.type = 0;

a.pValue = ????

a.ulValueLen = 5;

Any idea ? Do I declare the structure wrongly ?

Thank you
Andrea
 

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