Skip to content Skip to sidebar Skip to footer

Access A Function Present In C# Dll Using Python

I want to access a function my_function() present in a c# file which is compiled into a .net dll - abc.dll. C# file using System; using System.Collections.

Solution 1:

You haven't made the function visible in the DLL.

There are a few different ways you can do this. The easiest is probably to use the unmanagedexports package. It allows you to call C# functions directly like normal C functions by decorating your function with [DllExport] attribute, like P/Invoke's DllImport. It uses part of the subsystem meant to make C++/CLI mixed managed libraries work.

C# code

class Example
{
     [DllExport("ExampleFunction", CallingConvention = CallingConvention.StdCall)]
     public static int ExampleFunction(int a, int b)
     {
         return a + b;
     } 
}

Python

import ctypes
lib = ctypes.WinDLL('example.dll')
print lib.ExampleFunction(12, 34)

Post a Comment for "Access A Function Present In C# Dll Using Python"