Newbie - When to use MarshalAs.UnmanagedType??

D

Dirk Schulze

Hello,

please have a look at the fallowing code:

/* NetWkstaGetInfo - WKSTA_INFO_100
typedef struct WKSTA_INFO_100
{
DWORD wki100_platform_id;
LPWSTR wki100_computername;
LPWSTR wki100_langroup;
DWORD wki100_ver_major;
DWORD wki100_ver_minor;
}*/


[StructLayout(LayoutKind.Sequential)]
struct WKSTA_INFO_100
{
public int id;
[MarshalAs(UnmanagedType.LPWStr)]
public string computername;
[MarshalAs(UnmanagedType.LPWStr)]
public string langroup;
public int ver_major;
public int ver_minor;
}


or


[StructLayout(LayoutKind.Sequential)]
struct WKSTA_INFO_100
{
public int id;
public string computernme;
public string langroup;
public int ver_major;
public int ver_minor;
}


or


[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct WKSTA_INFO_100
{
public int id;
public string computernme;
public string langroup;
public int ver_major;
public int ver_minor;
}

I do not really understand the difference between these 3 structures
of WKSTA_INFO_100 :(
Are they all correct because they all seems to work??
Which one should be used and can anybody explain me the exact
difference between them?

Please, put the light on :)))

Many thanks,

Dirk
 
M

Mattias Sjögren

I do not really understand the difference between these 3 structures
of WKSTA_INFO_100 :(
Are they all correct because they all seems to work??

The first and third are effectively the same. I wouldn't expect the
second one to work, because the strings should be marshaled as LPStr
by default there.

Which one should be used and can anybody explain me the exact
difference between them?

In number three you're setting the default charset for all string
members in the struct with StructLayout.CharSet. That's the easiest
way (less typing) since all string members should be marshaled as
Unicode strings.

In the first one you're setting the marshaling option for each string
member separately. That also works, but is more typing and probably
adds a little more bloat to the metadata (although it's so little it
hardly matters).



Mattias
 

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