1

I'm trying to work with a C# DLL on Python. The code used to create to C# DLL is included with this question.

from ctypes import*
mydll = cdll.LoadLibrary("Z:\\9. Personal\\Jeet\\New 
folder\\ClassLibrary1.dll")
mydll.Add(3,4)`

The code doesn't work and the error is:- AttributeError: function 'Add' not found

Is there any alternative? Iron Python is installed on the PC I'm using but I have never used it. Please give me a detailed answer with all steps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    // Interface declaration.
    public interface ICalculator
    {
        int Add(int Number1, int Number2);
    };
    public class Class1 : ICalculator
    {
        public int Add(int Number1, int Number2)
        {
            return Number1 + Number2;
        }
    }
}
  • 1
    You can't call into a C# dll in this way. You can only call C style functions that have been explicitly exported. – Sean May 15 '19 at 10:45
  • Possible duplicate of [How to load a C# dll in python?](https://stackoverflow.com/questions/2077870/how-to-load-a-c-sharp-dll-in-python) – X39 May 15 '19 at 11:10
  • Additionally, you may want to look into https://www.nuget.org/packages/UnmanagedExports – X39 May 15 '19 at 11:11
  • @X39 I have tried using the import clr method described in the link in the Iron Python console. But when I try to execute clr.AddReference(r'filepath) in the console it throws the following error message:- The given assembly name or codebase was invalid. – Jeet Juneja May 15 '19 at 11:30
  • Do you know what's wrong? – Jeet Juneja May 15 '19 at 11:31
  • @Sean I'm trying Iron Python since I won't have to modify the DLL if I use Iron Python. – Jeet Juneja May 15 '19 at 11:32

0 Answers0