Call to native function from C#

G

Guest

Hi
I have a problem with calling a native library from C# on PocketPC 2003. I want to call a function that has a pointer to a structure as parameter. The structure itself has this form
struct sockaddr
u_short sa_family
char sa_data[14]
}
The signature of the function to call is
int rtBind (uint s, struct sockaddr *addr, int namelen)

I used DllImport to import the function
[DllImport("rt_api.dll")
static extern int rtBind(uint s, ref sockaddr name, int namelen)

I created a struct to represent the structure
public struct sockadd

public ushort sa_family;
public byte[] sa_data;

Then I call the function with
uint newsock
...
sockaddr name
name.sa_family = 0
name.sa_data = new byte[14]

int result = rtBind(newsock, ref name, System.Runtime.InteropServices.Marshal.SizeOf(name))

This results in a NotSupportedException at runtime
How can I do this call to a function in C# and the .NET CompactFramework

Thanks for your help
Kind regards
Dominik Haneber
 
P

Peter Foot [MVP]

You cannot marshal a structure with a nested array in .NETCF, so instead you
should pass a 16 byte length byte array and place and retrieve your fields
from it using Buffer and BitConverter objects.

Since a byte[] is marshalled as a pointer to a memory buffer you should not
declare your function with a ref argument hence:

[DllImport("rt_api.dll")]
static extern int rtBind(uint s, byte[] name, int namelen);



byte[] name = new byte[16];
//setup sockaddr

int result = rtBind(newsock, name, name.Length);


Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

Dominik Haneberg said:
Hi,
I have a problem with calling a native library from C# on PocketPC 2003. I
want to call a function that has a pointer to a structure as parameter. The
structure itself has this form:
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
The signature of the function to call is:
int rtBind (uint s, struct sockaddr *addr, int namelen);

I used DllImport to import the function:
[DllImport("rt_api.dll")]
static extern int rtBind(uint s, ref sockaddr name, int namelen);

I created a struct to represent the structure:
public struct sockaddr
{
public ushort sa_family;
public byte[] sa_data;
}
Then I call the function with:
uint newsock;
...
sockaddr name;
name.sa_family = 0;
name.sa_data = new byte[14];

int result = rtBind(newsock, ref name, System.Runtime.InteropServices.Marshal.SizeOf(name));

This results in a NotSupportedException at runtime.
How can I do this call to a function in C# and the .NET CompactFramework?

Thanks for your help!
Kind regards.
Dominik Haneberg
 
G

Guest

Thanks for your help, now I got the function call working as desired

Best regards
Dominik
 

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