473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ctypes - unloading implicitly loaded dlls

(my apologies if this is a repost, but it sure seems like the first
attempt disappeared into the ether...)

I'm writing a program that uses functionality from two different sets of
cdlls which reside in two different directories, call them 'libA.dll'
and 'libB.dll'. Although I don't directly use it, both directories
contain a dll with the same name, although they aren't in fact
identical. Call them, "libC.dll". However, the c-functions I call from
the clls I do use seem to implicitly use "libC.dll". The problem that
occurs after I load one dll and call functions in it, when I try to load
the second dll I get windows errors because the second dll tries to call
a function in its version of libC.dll, but it finds the version meant
for libB.dll, which doesn't contain that function.

Oy, I hope some sample code makes it clearer:

def demo():

A = ctypes.cdll.LoadLibrary('/path1/libA.dll')
A.foo() # implicitly uses '/path1/libC.dll'

_ctypes.FreeLibrary(A._handle)

# CRASH!
B = ctypes.cdll.LoadLibrary('/path2/libB.dll')
# "The procedure entry point some_func could not be located
# in the dynamic link library libC.dll.":

# libB.dll wants to use code from '/path2/libC.dll', but
# instead it finds '/path1/libC.dll' already loaded
# in memory, which doesn't
# contain the function call it wants.
Assuming my understanding of things is correct, then I believe what I
need to do is to remove /path1/libC.dll from memory before I try loading
libB.dll, but I haven't found any way of doing that. Can anyone offer
my some suggestions? Or, am I S.O.L.?
Notes:
* the two sets of dlls are supplied by a vendor for working with its
COTS packages; I don't have any control over the dll names used or the
code therein.
* If I leave out the call to A.foo(), then I don't crash, but if I leave
out the FreeLibrary call as well then I do crash.
* I've tried manipulating the PATH before loading the dlls, to no effect.
* I've tried del'ing A and running gc.collect() before loading B.

Thanks,

Scott
Jul 28 '08 #1
2 5253
pigmartian <sc*******@comcast.netwrote:
I'm writing a program that uses functionality from two different sets of
cdlls which reside in two different directories, call them 'libA.dll'
and 'libB.dll'. Although I don't directly use it, both directories
contain a dll with the same name, although they aren't in fact
identical. Call them, "libC.dll". However, the c-functions I call from
the clls I do use seem to implicitly use "libC.dll". The problem that
occurs after I load one dll and call functions in it, when I try to load
the second dll I get windows errors because the second dll tries to call
a function in its version of libC.dll, but it finds the version meant
for libB.dll, which doesn't contain that function.

Oy, I hope some sample code makes it clearer:

def demo():

A = ctypes.cdll.LoadLibrary('/path1/libA.dll')
A.foo() # implicitly uses '/path1/libC.dll'

_ctypes.FreeLibrary(A._handle)

# CRASH!
B = ctypes.cdll.LoadLibrary('/path2/libB.dll')
# "The procedure entry point some_func could not be located
# in the dynamic link library libC.dll.":

# libB.dll wants to use code from '/path2/libC.dll', but
# instead it finds '/path1/libC.dll' already loaded
# in memory, which doesn't
# contain the function call it wants.
Assuming my understanding of things is correct, then I believe what I
need to do is to remove /path1/libC.dll from memory before I try loading
libB.dll, but I haven't found any way of doing that. Can anyone offer
my some suggestions? Or, am I S.O.L.?
You could try loading C explicitly with ctypes.LoadLibrary() before
loading A, then you'll have a handle to unload it before you load B.

I think I'd probably split the code into two or three processes
though. Perhaps use http://pypi.python.org/pypi/processing to
communicate between them. That should get you out of DLL Hell!
(Don't load any of the DLLs before you start the worker processes
off.)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 28 '08 #2
Nick Craig-Wood wrote:
You could try loading C explicitly with ctypes.LoadLibrary() before
loading A, then you'll have a handle to unload it before you load B.
I did think of that, but no luck. Guess the cdll doesn't look for a dll
loaded already by python. I guess that does make sense.

I think I'd probably split the code into two or three processes
though. Perhaps use http://pypi.python.org/pypi/processing to
communicate between them. That should get you out of DLL Hell!
(Don't load any of the DLLs before you start the worker processes
off.)
That was quite a helpful suggestion, thank you. I had been using the
subprocess module actually, but really didn't like that approach.
processing is much nicer. Pipes, in particular, is quite handy.

~Scott

Jul 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
9
by: Daniel Watrous | last post by:
Hello, I am interested in using python to script access to some hardware for which there are existing drivers in the form of DLLs. The DLLs each have four exported functions and a host of COM...
1
by: mmacrobert | last post by:
Hi Everyone, I've created a 'C' dll that is accessed via ctypes library containing a bunch of functions. I've successfully been able to use the functions. However, I would like to throw python...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
0
by: dudeja.rajat | last post by:
Hi, I'm using the ctypes module to load my dlls. I have some 10 dlls the names of those are passed to a fucntion which then loads the passed dll. Now every dll has a getversion function....
0
by: Gabriel Genellina | last post by:
En Tue, 26 Aug 2008 07:42:50 -0300, <dudeja.rajat@gmail.comescribi�: Use getattr - same as with any other object. Suppose some_dll is your loaded DLL, then: function = getattr(some_dll,...
0
by: Terry Reedy | last post by:
dudeja.rajat@gmail.com wrote: For CPython, an importable module written in C. There is a doc Extending and Embedding the Python Interpreter. But I expect you can write the class in Python with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.