8

I am trying to call a function from a custom .dll file. But when I try to load my library SDK.dll, i get the following error. I am following the indications found here: Python import dll

Does anyone know what the problem is? I only found references of this problem for MAC enviroments.

>>> from ctypes import *
>>> lib = ctypes.WinDLL('C:/Develop/test/SDK.dll')

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    lib = ctypes.WinDLL('C:/Develop/test/SDK.dll')
NameError: name 'ctypes' is not defined
Community
  • 1
  • 1
toni
  • 713
  • 3
  • 7
  • 17

2 Answers2

11

By doing from ctypes import * you are pulling everything from ctypes module to local namespace, so you should be calling WinDLL directly:

>>> from ctypes import *
>>> lib = WinDLL('C:/Develop/test/SDK.dll')

Another (and as mentioned by NPE usually better) way to do it is to import just ctypes:

>>> import ctypes
>>> lib = ctypes.WinDLL('C:/Develop/test/SDK.dll')
Piotr Hajduga
  • 608
  • 4
  • 13
  • Thanks a lot, it worked. Now I get another error, `Traceback (most recent call last): File "", line 1, in lib = ctypes.WinDLL('C:/Develop/test/SDKApplication/Debug/SDKCore.dll') File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__ self._handle = _dlopen(self._name, mode) WindowsError: [Error 193] %1 is not a valid Win32 application`, but I think this might be due to the fact that my python is 32bit, and the dll 64. – toni Apr 08 '13 at 11:11
5

Change

from ctypes import *

to

import ctypes

The former imports all names from ctypes into the current namespace. It is generally considered to be a bad practice and is best avoided.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks a lot, it worked. Now I get another error, `Traceback (most recent call last): File "", line 1, in lib = ctypes.WinDLL('C:/Develop/test/SDKApplication/Debug/SDKCore.dll') File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__ self._handle = _dlopen(self._name, mode) WindowsError: [Error 193] %1 is not a valid Win32 application`, but I think this might be due to the fact that my python is 32bit, and the dll 64. – toni Apr 08 '13 at 11:10