python导入dll模块_无法在python中导入自定义DLL

I am trying to expose a C++ class to python with boost::python, so I am going through this tutorial. I created a visual studio .dll project, with this source code:

#include

using namespace boost::python;

struct World

{

void set(std::string msg) { this->msg = msg; }

std::string greet() { return msg; }

std::string msg;

};

BOOST_PYTHON_MODULE(hello)

{

class_("World")

.def("greet", &World::greet)

.def("set", &World::set)

;

}

And I built it as a 64-bit dll. The next step in the tutorial says:

Here, we wrote a C++ class wrapper that exposes the member functions greet and set. Now, after building our module as a shared library, we may use our class World in Python. Here's a sample Python session:

>>> import hello

>>> planet = hello.World()

>>> planet.set('howdy')

>>> planet.greet()

'howdy'

However, after launching python in the same directory and typing import hello I get

>>> import hello

Traceback (most recent call last):

File "", line 1, in

ImportError: No module named 'hello'

>>>

I have tried renaming the 'dll' files to hello.dll, and also copying every output file (dll, exp, ilk, lib, and pdb) to %PYTHONPATH%\DLLs, yet I am still unable to import the module into python.

Much googling brought me to this article recommending I use ctypes to import the dll. This lets me load the dll, but I am still unable to call the "World" class. For example:

>>> import ctypes

>>> mydll = ctypes.cdll.LoadLibrary("hello")

>>> mydll

>>> hello = mydll.World()

Traceback (most recent call last):

File "", line 1, in

File "C:\Program Files\Python35\lib\ctypes\__init__.py", line 360, in __getatt

r__

func = self.__getitem__(name)

File "C:\Program Files\Python35\lib\ctypes\__init__.py", line 365, in __getite

m__

func = self._FuncPtr((name_or_ordinal, self))

AttributeError: function 'World' not found

>>>

So a couple questions:

Is it possible to import a dll in python without using ctypes? The tutorial seems to indicate that it is, but doesn't provide very much detail about the right way to get the dll into python.

Which files do I need and where? It seems like I should only need the dll file from visual studio in the working directory of my python shell, but this is clearly not working for me.

Why can't I call World through ctypes?

Some more important details: I am using windows 7 64-bit, python 3.5.2 64-bit, and Visual Studio 2015 with Boost 1.61.

解决方案

I actually just figured out the answer shortly after posting the question. Thanks to this blog post I found out that simply renaming the hello.dll to hello.pyd was enough. From some more googling, I think that ctypes is only for C DLLs, not C++, and not with boost!. The point of boost::python, is to remove the need for ctypes and make the DLL compatible with python. So to answer all of my own questions:

Yes, but it must have a .pyd extension.

You only need the compiled dll file and the boost_python_vc140...dll (may vary). However, like I said, the dll file must be renamed.

Because ctypes is not the right tool for loading a boost::python dll!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值