【发布时间】:2016-11-28 21:07:31
【问题描述】:
我想访问一个存在于 c# 文件中的函数 my_function(),该文件被编译为 .net dll - abc.dll。
C# 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
public class Class1
{
public string my_function()
{
return "Hello World.. :-";
}
}
}
将以上代码编译成abc.dll后
使用下面的 python 尝试访问 my_function()
import ctypes
lib = ctypes.WinDLL('abc.dll')
print lib.my_function()
以上代码抛出错误
lib.my_function() 回溯(最近一次通话最后): 文件“”,第 1 行,在 getattr 中的文件“C:\Anaconda\lib\ctypes__init__.py”,第 378 行 func = self.getitem(名称) getitem 中的文件“C:\Anaconda\lib\ctypes__init__.py”,第 383 行 func = self._FuncPtr((name_or_ordinal, self)) AttributeError:找不到函数“my_function”
【问题讨论】:
-
我猜你应该使用完整函数的命名空间。你试过
print lib.Test.Class1.my_function()吗? -
你必须让你的 .net dll COM 可见。
标签: c# python .net ctypes dllimport