Re: Call to native function from C#

From: Peter Foot [MVP] (feedback_at_nospam-inthehand.com)
Date: 02/12/04


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
>


Relevant Pages

  • [NEWS] D-Link DWL-G700AP httpd DoS
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... By crafting a special HTTP GET string, attackers can DoS the D-Link ... int check_httpd; ... if(connect(sockfd, (struct sockaddr *)&servaddr, ...
    (Securiteam)
  • [EXPL] Telindus Router 10xx and 11xx Remote Exploit
    ... The 11xx router series by Telindus has a very serious remotely exploitable ... struct sockaddr sa; ... int len; ... signal(SIGINT, exitnow); ...
    (Securiteam)
  • surprisingly slow accept/connect cycle time
    ... The program forks and the child attempts to accept 1000 connections. ... socket_or_die (int domain, int type, int protocol) ... connect_or_die (int fd, const struct sockaddr *addrp, socklen_t len) ...
    (Linux-Kernel)
  • [PATCH] syscalls.h update #9 (open/close)
    ... extern int do_truncate ... extern struct file * dentry_open; ... asmlinkage long sys_setsockopt(int fd, int level, int optname, ... asmlinkage long sys_connect(int, struct sockaddr *, int); ...
    (Linux-Kernel)
  • Re: Help With PInvoke and SetupDiEnumDriverInfo/SP_DRVINFO_DATA
    ... public const int DIGCF_PRESENT =; ... public static extern IntPtr SetupDiGetClassDevs(ref Guid ... ref SP_DEVINFO_DATA DeviceInfoData); ... ref SP_DEVINFO_DATA DeviceInfoData, int Property, ref int ...
    (microsoft.public.dotnet.languages.csharp)