Passing a C# struct to a C-function...


A

Arne Styve

Hi,

I have an API written in C that came with some graphics
cards we are going to use in a project. I need to write a
small application where this API is to be used, and I
decided to try out C#.
The API is imported by using the [DllImport("somedll.dll"-
method.

Now, the function in the DLL to call, has a parameter that
is a struct, which is fine, but one of the members of the
struct is a char-array:

in C:
typedef struct
{
unsigned short Size;
char DeviceName[16];
unsigned short DeviceType;
unsigned long Reserved0;
unsigned long Reserved1;
} VSWLOAD;

When creating a simelar struct in C# everything is fine
except for the char DeviceName[16];

How should a C# struct look like in order for it to be
possible to use as a parameter when calling the C-function
in the imported DLL ?

Also, the size-member of the struct should be initialized
with the entire size of the struct before calling the C-
function. In C this is done as follows:

VSWLOAD load;
load.Size = sizeof( VSWLOAD );

How do I get the size of the C# struct ?

Thanks for helping me out here. I'm really stuck here.

Regards
Arne
 
N

Nicholas Paldino [.NET/C# MVP]

Arne,

You would define the structure like this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct VSWLOAD
{
[MarshalAs(UnmanagedType.U2)]
public short Size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
public string DeviceName;
[MarshalAs(UnmanagedType.U2)]
public short DeviceType;
[MarshalAs(UnmanagedType.U4)]
public int Reserved0;

[MarshalAs(UnmanagedType.U4)]
public int Reserved1;
}

To get the size of this struct, you can call the static SizeOf method on
the Marshal class. Of course, for a structure like this, the size is always
the same, so you might want to set the size once and then store a copy of
the blank structure somewhere.

Hope this helps.
 
Ad

Advertisements

A

Arne Styve

Hi Nicholas,

Thanks for your help! I've tried it and it works just
perfect (off course).

I didn't quite understand how you get the size of the
struct. You said I should call the SizeOf-method of the
Marshal class. Which Marshal class is this ?

Thanks again for your help!

Regards
Arne Styve
-----Original Message-----
Arne,

You would define the structure like this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct VSWLOAD
{
[MarshalAs(UnmanagedType.U2)]
public short Size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
public string DeviceName;
[MarshalAs(UnmanagedType.U2)]
public short DeviceType;
[MarshalAs(UnmanagedType.U4)]
public int Reserved0;

[MarshalAs(UnmanagedType.U4)]
public int Reserved1;
}

To get the size of this struct, you can call the static SizeOf method on
the Marshal class. Of course, for a structure like this, the size is always
the same, so you might want to set the size once and then store a copy of
the blank structure somewhere.

Hope this helps.

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

Arne Styve said:
Hi,

I have an API written in C that came with some graphics
cards we are going to use in a project. I need to write a
small application where this API is to be used, and I
decided to try out C#.
The API is imported by using the [DllImport ("somedll.dll"-
method.

Now, the function in the DLL to call, has a parameter that
is a struct, which is fine, but one of the members of the
struct is a char-array:

in C:
typedef struct
{
unsigned short Size;
char DeviceName[16];
unsigned short DeviceType;
unsigned long Reserved0;
unsigned long Reserved1;
} VSWLOAD;

When creating a simelar struct in C# everything is fine
except for the char DeviceName[16];

How should a C# struct look like in order for it to be
possible to use as a parameter when calling the C- function
in the imported DLL ?

Also, the size-member of the struct should be initialized
with the entire size of the struct before calling the C-
function. In C this is done as follows:

VSWLOAD load;
load.Size = sizeof( VSWLOAD );

How do I get the size of the C# struct ?

Thanks for helping me out here. I'm really stuck here.

Regards
Arne

.
 

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

Similar Threads

Problem passing structs in C#? 4
C struct to C# 3
C structs in C# 1
C struct in C# 2
c++ structs in c# 0
Struct C to C# 0
c++ function call with struct... 3
Structs in C# 12

Top