Passing structs in a call to a DLL

A

Arnau Font

Hi,

I'm using a DLL that has a function which receives a pointer to a struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 
S

Sergey Bogdanov

Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).
 
A

Arnau Font

Thanks Sergey! Couldn't they have done this any better?

All the DLL calls that contain structures must be done this way?

Sergey Bogdanov said:
Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Hi,

I'm using a DLL that has a function which receives a pointer to a struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 
S

Sergey Bogdanov

The calls should be done in this way when a structure contains non
blittable types [1]. e.g. the following structure could be marshalled
without problems:

struct Test
{
int a;
int b;
byte c;
Rect rect;
IntPtr somePointer;
}

struct Rect
{
int x;
int y;
}

[1] "Blittable Types" table,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfadvinterop.asp



--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Thanks Sergey! Couldn't they have done this any better?

All the DLL calls that contain structures must be done this way?

Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Hi,

I'm using a DLL that has a function which receives a pointer to a struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 
S

Sergey Bogdanov

sorry, gave you wrong link:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfintrointerp.asp


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Sergey said:
The calls should be done in this way when a structure contains non
blittable types [1]. e.g. the following structure could be marshalled
without problems:

struct Test
{
int a;
int b;
byte c;
Rect rect;
IntPtr somePointer;
}

struct Rect
{
int x;
int y;
}

[1] "Blittable Types" table,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfadvinterop.asp




--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Thanks Sergey! Couldn't they have done this any better?

All the DLL calls that contain structures must be done this way?

Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau Font wrote:

Hi,

I'm using a DLL that has a function which receives a pointer to a
struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 
P

Peter Hartlén

Only structures with certain complex fields, such as arrays of some kinds
(bytes, chars etc..)

Here are some links to information about this:
http://www.pinvoke.net
http://msdn.microsoft.com/library/d...ide/html/cpconblittablenon-blittabletypes.asp
http://msdn.microsoft.com/library/d.../dnnetcomp/html/ProcessManager.asp?frame=true
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/PInvokeLib.asp
http://www.opennetcf.org/PermaLink.aspx?guid=abe6694a-ff15-4617-a21f-8163d427a89c

There are major limitations to what Compact Framework can handle compared to
the full framework in this topic!

/ Peter


Arnau Font said:
Thanks Sergey! Couldn't they have done this any better?

All the DLL calls that contain structures must be done this way?

Sergey Bogdanov said:
Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Hi,

I'm using a DLL that has a function which receives a pointer to a
struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 
A

Arnau Font

Thanks a lot, Sergey!

sorry, gave you wrong link:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfintrointerp.asp


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Sergey said:
The calls should be done in this way when a structure contains non
blittable types [1]. e.g. the following structure could be marshalled
without problems:

struct Test
{
int a;
int b;
byte c;
Rect rect;
IntPtr somePointer;
}

struct Rect
{
int x;
int y;
}

[1] "Blittable Types" table,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfadvinterop.asp




--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau said:
Thanks Sergey! Couldn't they have done this any better?

All the DLL calls that contain structures must be done this way?

"Sergey Bogdanov" <[email protected]> escribio en el mensaje

Since the marshaller does not support array of bytes in the structure
you have to pass array of 25 bytes instead (4 bytes for int, 20 bytes
for array and 1 byte for char).


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Arnau Font wrote:

Hi,

I'm using a DLL that has a function which receives a pointer to a
struct
like this (in c++):

struct Example{
int var1;
BYTE buff[20];
char res;
}

The function:

int function (Example* ex) {
return 0;
}

I'd like to have something like this (in C#):

public struct Example {
int var1;
byte buff[20]; //Yes, it is wrong, but I want a fixed size array!
//byte[] buff; //this wouldn't be passed correctly, would it?
char res;
}


[DllImport("MyDLL.dll")]
static public extern int function(ref Example ex);

Or should I receive the structure as a byte array and convert it?

Thanks!
 

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