Re: Problema con PInvoke

Jonatan Anauati <[email protected]> Wed, 31 Mar 2010 20:22:03 -0300
Newsgroups gmane.comp.gnome.mono.hispano
Message-ID <[email protected]>
Lluis, buenas noches te comento que probé tu ejemplo y a mí (con pequeñas
modificaciones) me funciona bien.

A continuación te envío el código modificado:

-----------------------------------------------------------Main.cs-----------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;

namespace testrtnetlink
{

    class MainClass
    {
        const string  PathToLib
="/home/lluis/Projects/testrtnetlink/testrtnetlink/bin/Debug/libnetlinkdll.so";

        [DllImport ("libc")]
        private static extern int getpid ();

        [DllImport("/home/jaa/taller/lluis/libnetlink.so",
EntryPoint="NadaRut")]
        private static extern int NadaRut ();

        public static void Main (string[] args)
        {
            int pid = getpid();
            int nadarut = 0;
            Console.WriteLine ("Hello World! pid = " + pid.ToString());
            nadarut = NadaRut();
            Console.WriteLine ("NadaRut returns = {0}", nadarut);
        }
    }
}
---------------------------------------------------------------------------------------------------------------------

El código cpp lo dejé tal cual.

Para compilar y ejecutar usé:

$ gcc -c -o libnetlink.o libnetlink.cpp  (también puede usarse c++ en este
paso).
$ ld -shared -o libnetlink.so libnetlink.o

Con lo anterior ya generé el shared object (*.so), luego compilé el código
c#:
$ mcs Main.cs

Y por último lo ejecuto:
*$* mono Main.exe
Hello World! pid = 31054
NadaRut returns = 5

$ gcc --version
gcc version:     gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.1)

$ mono --version
Mono JIT compiler version 2.0.1 (tarball)
Copyright (C) 2002-2008 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
    GC:            Included Boehm (with typed GC)
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  x86
    Disabled:      none

$mcs --version
Mono C# compiler version 2.0.1.0

Espero te sirva.

Un Saludo.-

2010/3/31 Lluis <[email protected]>

>
>
> Usando esto como libreria
>
> #include <sys/socket.h>
> #include <bits/sockaddr.h>
> #include <asm/types.h>
> #include <linux/rtnetlink.h>
> #include <arpa/inet.h>
>
>
>
> using namespace std;
>
> extern "C"
> {
>
> int NadaRut()
> {
>        return 5;
> }
>
> int OtraRut()
> {
>        return 6;
> }
>
> }
>
> Y usando esto como programa principal
>
> using System;
> using System.Runtime.InteropServices;
>
> namespace testrtnetlink
> {
>
>
>        class MainClass
>        {
>                const string  PathToLib =
>
> "/home/lluis/Projects/testrtnetlink/testrtnetlink/bin/Debug/libnetlinkdll.so";
>
>            [DllImport ("glib.so")]
>                private static extern int getpid ();
>
>                [DllImport
>
> ("/home/lluis/Projects/testrtnetlink/testrtnetlink/bin/Debug/libnetlinkdll.so",
> EntryPoint="NadaRut")]
>                private static extern void NadaRut ();
>
>                public static void Main (string[] args)
>                {
>                        int pid = getpid();
>                        Console.WriteLine ("Hello World! pid = " +
> pid.ToString());
>                        NadaRut();
>                }
>        }
> }
>
> Se me dispara una excepcion :
>
> Hello World! pid = 6304
>
> Unhandled Exception: System.EntryPointNotFoundException: NadaRut
>  at (wrapper managed-to-native) testrtnetlink.MainClass:NadaRut ()
>  at testrtnetlink.MainClass.Main (System.String[] args) [0x0001c] in
> /home/lluis/Projects/testrtnetlink/testrtnetlink/Main.cs:22
> La aplicación terminó con una señal: SIGHUP
>
> No parece encontrar NadaRut.
>
> Alguien sabe lo que me falta?
> Con getpid si funciona.
>
> --
> Saludos
>
> Lluis
>
> _______________________________________________
> Mono-hispano mailing list
> [email protected]
> https://listas.hispalinux.es/mailman/listinfo/mono-hispano
>



-- 
por favor evita el enviarme archivos adjuntos de MS Word.
Ver: http://www.gnu.org/philosophy/no-word-attachments.es.html
----------------------
printk("fent linux\n");
----------------------

_______________________________________________
Mono-hispano mailing list
[email protected]
https://listas.hispalinux.es/mailman/listinfo/mono-hispano