一个简单的Python程序无法访问我的dll

发布于 2024-12-04 07:49:03 字数 777 浏览 1 评论 0原文

我创建了一个简单的 MFC .dll 文件,并且可以通过另一个 MFC 应用程序访问它。我试图使用Python 3.2.2做同样的事情,但在加载我的库后,它没有检测到我的dll中的函数并给出如下错误:

    Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python32\lib\ctypes\__init__.py", line 353, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python32\lib\ctypes\__init__.py", line 358, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'SayHello' not found

..

我试图找到一个简单的程序来访问我的MFC dll使用Python编写文件,但到目前为止还没有成功。只是为了让大家知道,我已经阅读了 ctypes 并进行了很多搜索以获得这个在职的。我的主要目标是使用 Python 作为脚本语言来与我的 MFC C++ dll 交互。我读了很多书,最接近的一本是 PythonWin。请在这方面提供帮助。

干杯。

I have created a simple MFC .dll file and I am able to access it via another MFC Application program. I was trying to do the same thing using Python 3.2.2 but after I load my library, It does not detect the function in my dll and gives an error like:

    Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python32\lib\ctypes\__init__.py", line 353, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python32\lib\ctypes\__init__.py", line 358, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'SayHello' not found

..

I have trying to find a simple program to access my MFC dll file using Python but have got no success as of yet. Just to let everyone know, I have read on ctypes and have been searching lots to get this working. My main aim is to use Python as a scripting language to interface with my MFC C++ dll. I have been reading lots and the closest one I could get to was with PythonWin. Please help in this regard.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-11 07:49:03

我已经能够抓住我的问题并解决它。对于那些将来可能期望这样做的人,我只想粘贴 python 脚本代码,以便使您的函数可以在 python 脚本中访问。使用 C++ 函数的方法显然是在 .dll 文件中声明它。

extern "C" __declspec(dllexport) int SayHello(int strNo);

你的 Python 脚本应该是这样的:(需要 getattr() 函数来访问你的 python 脚本中的 c++ 函数)

mydll = cdll.LoadLibrary("Location.dll")
hellofunc = getattr(mydll,'SayHello')
func2.restype = c_int
func2.argtypes = [c_int]
x = func2(34)
print(x)

...

如果有人认为我可能是错的,请随时纠正我。我是 Python 的初学者,这种方式对我很有用,之后我就可以继续编写脚本了。
希望这对其他人有帮助。

干杯。

I have been able to get a hold of my problem and have solved it. For those who might expect this in the future, I would just like to paste the python script code in order to make your functions accessible in your python scripts. The way to go with your C++ function is obviously to declare this in your .dll file.

extern "C" __declspec(dllexport) int SayHello(int strNo);

and your Python script should go something like this: (getattr() function is required to access your c++ function in your python script)

mydll = cdll.LoadLibrary("Location.dll")
hellofunc = getattr(mydll,'SayHello')
func2.restype = c_int
func2.argtypes = [c_int]
x = func2(34)
print(x)

...

If anyone thinks I might be wrong, please feel free to correct me. I am a beginner in Python, it worked for me this way and I am able to go ahead with my scripting after this.
Hope this helps others.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文