2

I'm a green hand in python and now facing a problem when importing dll in python. By refering some hints found online, i tried using ctypes as below with error prompt.

>>> import ctypes
>>> dl=ctypes.WinDll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    dl=ctypes.WinDll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')
AttributeError: 'module' object has no attribute 'WinDll'

>>> dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    dl=ctypes.cdll.LoadLibrary('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')
  File "C:\Python27\lib\ctypes\__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] 

Am I doing something wrong or this dll was not scripted complied with python standard? I have uploaded to MediaFire for your analysis. Any help is highly appreciated!

argNA
  • 23
  • 1
  • 1
  • 4

3 Answers3

1

I loaded the DLL UdfManagerPython.dll into Dependency Walker, and it pointed out that this DLL has a dependency on python22.dll. When I tried to load this DLL in the Python (2.7) interpreter, I got a message box which more-or-less told me the same thing:

The program can't start because python22.dll is missing from your computer. Try reinstalling the program to fix this problem.

So it seems this DLL was intended to be used with Python 2.2, not Python 2.7 as you're using.

I don't have Python 2.2 installed. If you do, perhaps you get different error messages to me.

It's also worth pointing out that you can't use ctypes with Python 2.2, because ctypes is only supported for Python 2.3 onwards.

I have no idea where this DLL comes from. I Googled its name and got all of four results, one of which was this question.

Incidentally, I've seen an error of the form 'The specified module cannot be found' if the DLL itself can be found but a dependency of the DLL is missing. So if you get a message like this, and you're sure the DLL itself is present, check its dependencies.

EDIT: I tried installing Python 2.2 to see whether I could load this DLL. With Python 2.2 installed you can at least load this DLL, but Python crashes if you try to call either of the init... methods. (I don't know what parameters to pass them, so I passed them none.)

Here's what happened when I tried calling one of these methods:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> l = CDLL("UdfManagerPython.dll")
>>> l.initPyUdfNumber()
Fatal Python error: Interpreter not initialized (version mismatch?)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I Googled the first line of this error message, and the recurring theme I got from most of the results was that this error indicates you're attempting to load an extension module linked against one version of Python with a different version of Python.

So in answer to your question in the comments, no, I don't believe there is a way to load this DLL in Python 2.7.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • We're using a molecular dynamics simulation program [link](http://octa.jp), the output data is in udf format. The program incorporated python 2.2 with primitive IDE environment and Numeric and Scientific modules only. The program is written in java and provides two modules to access udf data files for c++ and python. I installed python 2.7 to extract data from udf file for postprocess by popular modules such as numpy, vpython. It seemed that the dll scripted under python 2.2 standard and unsupported in python >=2.2. From your opinion, is there a workaround to use this dll in python 2.7? – argNA Jul 06 '12 at 05:10
  • Thank you very much, you give me the most valuable and detailed answer! – argNA Jul 09 '12 at 16:03
0

WinDll should be all lowercase.

dl = ctypes.windll('C:\\Python27\\Lib\\site-packages\\UdfManagerPython.dll')
Christian Witts
  • 11,375
  • 1
  • 33
  • 46
  • I tried. But still don't work. it said "TypeError: 'LibraryLoader' object is not callable" – argNA Jul 05 '12 at 18:43
  • 1
    Is the DLL built in Release or Debug version ? – Christian Witts Jul 05 '12 at 18:47
  • windll is not lowercase, that's a sub-module or something. It's `WinDLL`. See my answer for more info. – FogleBird Jul 05 '12 at 19:34
  • Neither uppercase nor lowercase of "windll" work for me. Python drops me with the same error prompt. I'm sure this dll was release version. Thank you Christian Witts and FogleBird. – argNA Jul 06 '12 at 05:24
0

Try WinDLL or CDLL.

I never used LoadLibrary directly in ctypes, but it looks like it still might not be finding the DLL. Make sure it's on your system path. (Or in the same directory as your Python module.)

FogleBird
  • 74,300
  • 25
  • 125
  • 131
  • I use default installation setting of python 2.7 in C:\Python27\ directory. I copied the dll into \Lib\site-packages\ and tried to load it with ctypes.cdll('FILE'), ctypes.WinDll('FILE'), it don't work. I detailed the whole story in comment under @LukeWoodward reply. Thanks for the hint. – argNA Jul 06 '12 at 05:38