Complete Communications Engineering

This can be done by declaring the functions in C# using the DllImport attribute.  This attribute tells C# which shared library each function should be loaded from, and which function to load.  The function declaration in C# must match the function prototype in the shared library according to rules that are described in the .NET documentation.  Once the function declaration with DllImport attribute is in place, everything else is automatic.  Calling the function from C# code, will cause the shared library to be loaded and call the corresponding function from the shared library.  The following is a minimal example that calls a function print_hello from a shared library called libtest.so:

using System;

using System.Runtime.InteropServices;

 

namespace my_cs_namespace {

 

    class MyCSApp

    {

        /* Declare a function in a shared library */

        [DllImport(“test”, EntryPoint = “print_hello”)]

        static extern int print_hello();

 

        static void Main()

        {

            /* Call the function from the shared library */

            print_hello();

        }

    }

};

The DllImport attribute requires using the ‘System.Runtime.InteropServices’ directive.  With that, the declaration itself is just a normal C# function declaration without a function body.  Above the function declaration is the DllImport attribute that tells which library to load and which function to call.  In this case, only “test” is specified for the library.  The Linux runtime knows to look for a file called ‘libtest.so’ from this information.  The shared library needs to be in a findable location for this to work.  The entry point property tells which function to call.  It must match the name of the function in the shared library, and the shared library must export that function.  With the declaration in place, the C# program can call the shared library function.