Reputation: 4295
So i was following the solution posted in this question
Calling a C# library from python
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;
class Test
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int TestExport(int left, int right)
{
return left + right;
}
}
Python
import ctypes
a = ctypes.cdll.LoadLibrary('ClassLibrary1.dll')
a.add(3, 5)
Error
AttributeError: function 'add' not found
What i did was to copy the dll onto desktop and run a python shell in desktop and run the following lines in the python shell. Am i doing something wrong?
I am using python 2.7
Upvotes: 2
Views: 586
Reputation: 101633
When trying to reproduce this, got the same behavior. But the reason is really simple - most likely your .NET project, like mine, is targeting AnyCPU. Since we are trying to create unmanaged export - we need separate versions for x86 and x64. So to resolve your problem - just target x86 for your .NET assembly for example. Here is how I found that out (this info might be useful as well): in Visual Studio go to Tools > Options > Projects and Solutions > Build and Run. There you will see MSBuild output verbosity level - set that to Diagnosts. Then build your project and among other stuff you will see:
Skipped method exports, because the platform target is neither x86 nor x64. Set the MsBuild property 'NoDllExportsForAnyCpu' to false if you want to create separate versions for x86 and x64. (e.g. you can do that in the package manager console: Set-NoDllExportsForAnyCpu -value $false) (TaskId:35)
Upvotes: 2