dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]pInvoke params problem


From: Rhys Weatherley
Subject: Re: [DotGNU]pInvoke params problem
Date: Tue, 08 Oct 2002 09:03:11 +1000

Chris Smith wrote:
> 
> On Sunday 06 October 2002 06:31, Gopal V wrote:
> 
> > With XSharp to refer, I think PInvoke has been tortured enough .
> > GW Api cannot not prove to be more troublesome ;-)
> 
> Okay, What's the matter with the attached code then?
> 

I think I see the problem.  I'll assume that you are running
this on a 32-bit system like an i386.

> I've a C function that looks like this:
> 
>    void
>    test_params( long level, const char *str, int len )
>    {
>      printf( "Level: %ld   Str: %p   Level: %d\n", level, str, len );
>    }

In C, both "long" and "int" are 32 bits in size.

> I export it as:
> 
>    [DllImport("libexample")]
>    extern public static void test_params( long level, String msg, int len );

In C#, "long" is 64 bits and "int" is 32 bits.  This causes
a mismatch when the PInvoke marshalling is performed.  You
should instead use this:

    [DllImport("libexample")]
    extern public static void test_params( int level, String msg, int len );

Note the change to the declaration of "level".

This is one of the most annoying things about PInvoke: you have
to declare the types with their actual size, which can make the
code less portable.  I found a clever way around this with Xsharp,
but it requires some autoconf voodoo to set it up (see Xlib.cs.in).

> Parsing PNative.cs
> Parsing PTest.cs
> PNative.cs:9: `DllImportAttribute' is not declared in the current scope

This one's easy.  You misspelt "InteropServices" in PNative.cs.
The "int" error will go away when you fix this.

You will also get an error for "DllImportMapAttribute".  That
is because you need to declare "DllImportMap" yourself as it
isn't a standard class, but a pnet hack for doing library
renaming.  In your example, you don't need it at all so you
can leave it out.

Cheers,

Rhys.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]