python调用C#project方法

1. 下载需要的包

visual studio: Tool -> Nuget Package Manager ->  Package Manager Console

PM> Install-Package UnmanagedExports

2. Build dll 

右键要引用的Project ->Properties ->  Build -> platform target  选择x64 或者x86 (important: 不要选择Any CPU) 

3.我的代码, 在自己的代码中一定要有#must 标识的语句

C#

using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;  # must

class UnitTest1
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)] #must
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}

 python

import ctypes
a = ctypes.cdll.LoadLibrary(r'C:\Users\May Li\Documents\visual studio 2015\Projects\May\May\bin\Debug\May.dll')
b= a.add(3,5)
print(b)

  

posted @ 2017-10-24 10:27  pangxiaohai  阅读(297)  评论(0编辑  收藏  举报