Re: Call to native function from C#
From: Peter Foot [MVP] (feedback_at_nospam-inthehand.com)
Date: 02/12/04
- Next message: Ignacio Machin \( .NET/ C# MVP \): "Re: threading in CF, anybody good tutorials, sites?"
- Previous message: Tim Wilson: "Re: ListView and multiselect"
- In reply to: Dominik Haneberg: "Call to native function from C#"
- Next in thread: Dominik Haneberg: "Re: Call to native function from C#"
- Reply: Dominik Haneberg: "Re: Call to native function from C#"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 12 Feb 2004 13:06:31 -0000
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" <anonymous@discussions.microsoft.com> wrote in message
news:B8706AC4-CF38-4D70-A08F-CF870062F8E3@microsoft.com...
> 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
>
- Next message: Ignacio Machin \( .NET/ C# MVP \): "Re: threading in CF, anybody good tutorials, sites?"
- Previous message: Tim Wilson: "Re: ListView and multiselect"
- In reply to: Dominik Haneberg: "Call to native function from C#"
- Next in thread: Dominik Haneberg: "Re: Call to native function from C#"
- Reply: Dominik Haneberg: "Re: Call to native function from C#"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|