I've looked at other posts on this topic, but I was wondering if there was some way of doing this without changing directly into the directory of the library or explicitly specifying the absolute path such as through sys.path.append or using the PATH or CLASSPATH environment variables on Windows. What I'm trying to do right now is load "jvm.dll"
Asked
Active
Viewed 1,038 times
1 Answers
0
If the DLL in question didn't have any dependencies, it would be possible to load the library by passing the ctypes factory the absolute path to the DLL (which you could determine however you wish). However, DLLs are often bundled together and have inter-dependencies. Your most robust bet is probably to add the DLL's containing directory to os.environ['PATH']
to ensure that the dependent DLLs can be automatically resolved by the OS library loader. Note, you can do this just temporarily if you're worried about polluting your environment.
dll_dir = r'c:\foo\bar\path'
tmp = os.environ['PATH']
os.environ['PATH'] = dll_dir + ';' + tmp
ctypes.LoadLibrary( dll_name )
os.environ['PATH'] = tmp

Rakis
- 7,779
- 24
- 25
-
1This also did not work. In fact, putting just the directory in for PATH that holds the dll (ie setting os.environ['PATH'] = r'"C:\Program Files (x86)\Java\jdk1.7.0_04\jre\bin\client"') does not work. On the other hand os.chdir('C:/Program Files (x86)/Java/jdk1.7.0_04/jre/bin/client') does work. – Cenoc May 25 '12 at 16:56
-
Did you try adding it to the front of the existing path rather than completely replacing the path? The additional PATH entries may be required. – Rakis May 25 '12 at 19:07
-
I did, no luck there. I think it might not be using PATH at all, which is very odd. – Cenoc May 25 '12 at 19:23