1

After using the Dotnet Core on Linux to generate my DLL -- which was successful --, I tried calling it through Python with ctypes using the connection provided through DllExport -- as shown in this example here.

How to solve this issue?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.InteropServices; // Calling conventions
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using RGiesecke.DllExport;

public class FormManager3
{
        [DllExport("test", CallingConvention = CallingConvention.Cdecl)]
        public static void Main() {
            Console.WriteLine("Test");
        }

       ...
}
cSharp = os.path.join(scriptDirectory, "legacy/bin/Debug/netcoreapp2.2/legacy.dll")
writeToCSharp = ctypes.cdll.LoadLibrary(cSharp)
writeToCSharp.test()

After running the script:

E   OSError: /home/farm/Documents/project/legacy/bin/Debug/netcoreapp2.2/legacy.dll: invalid ELF header
  • 1
    To clarify, its a *managed* DLL? `ctypes` loads *unmanaged* (native) DLLs. You'd need Python.Net to load a managed DLL. – Mark Tolonen Jul 06 '19 at 00:44
  • @MarkTolonen, I did not know that difference, I did a research and I understood better. I started using pyhtonnet and used `dotnet build --runtime linux-x64`, worked as expected. – Lucas Almeida Carotta Jul 10 '19 at 14:42

1 Answers1

0

Thanks to @MarkTolonen I was able to correct my mistake. Using pythonnet and mono, also changing the project from Console to a Class Library and removing DllExport.

I managed to run as expected:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace legacy
{
    public class FloatLicManager3
    {
        public static int TestExport(int left, int right)
        {
            return left + right;
        }
    }
}

Building it with:

dotnet build --runtime linux-x64

And using as:

import os
import clr

scriptDirectory = os.path.dirname(os.path.abspath(__file__))
cSharp = os.path.join(scriptDirectory, "legacy/bin/Debug/netstandard2.0/linux-x64/legacy.dll")
clr.AddReference(cSharp)

from legacy import FloatLicManager3

testing = FloatLicManager3()
testing.TestExport(1, 2)