Unable to import NHunspell.dll using ctypes in Python

A

akshay.ksth

Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.

When I use this code.

from ctypes import *
hunspell = cdll.LoadLibrary['Hunspellx64.dll']

I get an error

hunspell = cdll.LoadLibrary['Hunspellx64.dll']
TypeError: 'instancemethod' object has no attribute '__getitem__'

I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.
 
M

Mark Lawrence

Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.

When I use this code.

from ctypes import *
hunspell = cdll.LoadLibrary['Hunspellx64.dll']

I get an error

hunspell = cdll.LoadLibrary['Hunspellx64.dll']
TypeError: 'instancemethod' object has no attribute '__getitem__'

I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.
Help on method LoadLibrary in module ctypes:

LoadLibrary(self, name) method of ctypes.LibraryLoader instance.

So looks as if you need:-

hunspell = cdll.LoadLibrary('Hunspellx64.dll')

--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.

Mark Lawrence
 
A

akshay.ksth

Thanks for the reply Mark. I did what you suggested.
But now I'm getting an error like this.

Traceback (most recent call last):
File "start.py", line 15, in <module>
hunspell = cdll.LoadLibrary('/home/kuro/Desktop/notepad/Hunspellx64.dll')
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/kuro/Desktop/notepad/Hunspellx64.dll: invalid ELF header

I am really new to using ctypes and dll files. Can you please guide me out.
 
D

Dave Angel

Thanks for the reply Mark. I did what you suggested.
But now I'm getting an error like this.

Traceback (most recent call last):
File "start.py", line 15, in <module>
hunspell = cdll.LoadLibrary('/home/kuro/Desktop/notepad/Hunspellx64.dll')
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/kuro/Desktop/notepad/Hunspellx64.dll: invalid ELF header

I am really new to using ctypes and dll files. Can you please guide me out.

This is why it's frequently useful to supply the information with your
original question: what version of Python, and what OS.

You're on Linux or similar, and dll's are the way a Windows executable
is named. So chances are you're trying to install a Windows dll on
Linux, which is unlikely to work except under very special circumstances
(eg. within WINE).

Try going back to where you downloaded this file, and see if you can get
the one for your OS.
 
C

Chris “Kwpolska†Warrick

You're on Linux or similar, and dll's are the way a Windows executable is
named.

dll’s are libraries for windows, not executables (/lib not /bin)
Try going back to where you downloaded this file, and see if you can get the
one for your OS.

That is not necessary. Because it is a sane OS, it is likely that
hunspell is packaged in the repositories of OP’s system, and might be
even installed there. And if the package is installed, hunspell
should reside in /usr/lib/libhunspell.so or a similar place.
 
A

akshay.ksth

Thanks Dave.
I'm using Python 2.7 and am working on Linux Mint.
Does it mean that I cant load the functions within the dll whilst on Linux. I thought that was what ctypes was used for.

Please correct me if I misunderstood what you meant.
 
A

akshay.ksth

Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.



When I use this code.



from ctypes import *

hunspell = cdll.LoadLibrary['Hunspellx64.dll']



I get an error



hunspell = cdll.LoadLibrary['Hunspellx64.dll']

TypeError: 'instancemethod' object has no attribute '__getitem__'



I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.

@Chris
I understand that. But then I am supposed to create something that works on a Windows environment using a Windows Dll.Aaandd Im stuck.
 
C

Chris “Kwpolska†Warrick

@Chris
I understand that. But then I am supposed to create something that works on a Windows environment using a Windows Dll.Aaandd Im stuck.

Then you need to get your hands on a copy of Windows. Or try messing
with Wine (you would need to install the Python interpreter for
Windows there), but that might not work properly.

And ctypes is used to use functions provided by C libraries on your
system, that is .dll files on Windows and .so files (shared object
files) everywhere else.
 
D

Dave Angel

dll’s are libraries for windows, not executables (/lib not /bin)


That is not necessary. Because it is a sane OS, it is likely that
hunspell is packaged in the repositories of OP’s system, and might be
even installed there. And if the package is installed, hunspell
should reside in /usr/lib/libhunspell.so or a similar place.

Sorry, since I was talking about a DLL, I used Microsoft's terminology.
Certainly in Unix terms, a DLL is roughly equivalent to a shared library.

DLL is one of the dozens of file extensions commonly used for Microsoft
executables, or PE files. Microsoft doesn't generally make the
distinction between an executable which can be called from the command
line and one which can only be used with LoadLibraryEx(). In common
use, only the "main" executable of a program will have an entry point,
but both common types may be used for library code.

http://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx


As for finding it in the repository, you're certainly right. I had
naiively assumed that the OP would have looked there first, and wouldn't
be in this state if it were in the repository (eg. Synaptic). It is in
Ubuntu 12.04.
 
D

Dave Angel

Thanks Dave.
I'm using Python 2.7 and am working on Linux Mint.
Does it mean that I cant load the functions within the dll whilst on Linux. I thought that was what ctypes was used for.

Please correct me if I misunderstood what you meant.

ctypes is not for cross-platform development, it's for cross-language
development. If you have a shared library for your own system whose
interfaces are designed for a static-typed language (usually C), ctypes
lets you bridge the gap, and call it from Python.

Your best bet is probably either to find a Windows machine, or to run an
actual Windows inside a Virtual Box. I do that whenever I have to
support an application that's not available for Linux. In either case,
you're actually running Windows, so you'll need a Windows python, which
knows how to call the Windows LoadLibrary and getProcAddress and other
such OS-specific interfaces.

There's a possibility that running a Windows version of Python under
WINE will work to access a Windows DLL, but the likelihood is so high
that there'll be inconsistencies that I wouldn't bother.

VirtualBox and WINE are both available in most Linuxes, for example in
Synaptic. And they should also be available via apt-get, but I don't
know how to find them that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top