Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Checking for dlls in ctypes

Reply
Thread Tools

Checking for dlls in ctypes

 
 
Wanderer
Guest
Posts: n/a
 
      10-12-2012
I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

try:
self.dll = windll.pvcam64
except:
print "No pvcam64"
try:
self.dll = windll.pvcam32
except:
print "No pvcam32"
try:
self.dll = windll.pvcam
except:
print "No pvcam"
return
else:
print "installed pvcam"
else:
print "installed pvcam32"
else:
print "installed pvcam64"

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      10-12-2012
On 10/12/2012 11:36 AM, Wanderer wrote:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
>
> try:
> self.dll = windll.pvcam64
> except:
> print "No pvcam64"
> try:
> self.dll = windll.pvcam32
> except:
> print "No pvcam32"
> try:
> self.dll = windll.pvcam
> except:
> print "No pvcam"
> return
> else:
> print "installed pvcam"
> else:
> print "installed pvcam32"
> else:
> print "installed pvcam64"
>


I can't help you find the dll's, because I don't run Windows. But I
could help you write a clearer question:

"doesn't work" is thoroughly useless for describing errors. If you're
getting an exception, show us the full traceback. That will show which
statement got the exception that wasn't caught. Next question is which
of the dlls is missing. Are you getting an exception because it's
missing or because of something more fundamental, like nesting exception
handlers?

Using bare excepts is almost never a good idea. If it "works" you get
no clues what went wrong. For example, a typo in source code can
trigger a bare exception, as can a user typing Ctrl-C. So when you're
using bare excepts, you have robbed the user of any way to terminate the
program.

If I were you, I'd be writing a loop so there's only one try block. Too
much duplicated code in the way you're doing it.



--

DaveA

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      10-12-2012
On 2012-10-12 16:36, Wanderer wrote:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
>
> try:
> self.dll = windll.pvcam64
> except:
> print "No pvcam64"
> try:
> self.dll = windll.pvcam32
> except:
> print "No pvcam32"
> try:
> self.dll = windll.pvcam
> except:
> print "No pvcam"
> return
> else:
> print "installed pvcam"
> else:
> print "installed pvcam32"
> else:
> print "installed pvcam64"
>

This works for me:

for name in ("pvcam64", "pvcam32", "pvcam"):
try:
self.dll = getattr(windll, name)
except OSError:
print "No " + name
else:
print "Installed " + name
return

 
Reply With Quote
 
Wanderer
Guest
Posts: n/a
 
      10-12-2012
On Friday, October 12, 2012 12:57:06 PM UTC-4, MRAB wrote:
> On 2012-10-12 16:36, Wanderer wrote:
>
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

>
> >

>
> > try:

>
> > self.dll = windll.pvcam64

>
> > except:

>
> > print "No pvcam64"

>
> > try:

>
> > self.dll = windll.pvcam32

>
> > except:

>
> > print "No pvcam32"

>
> > try:

>
> > self.dll = windll.pvcam

>
> > except:

>
> > print "No pvcam"

>
> > return

>
> > else:

>
> > print "installed pvcam"

>
> > else:

>
> > print "installed pvcam32"

>
> > else:

>
> > print "installed pvcam64"

>
> >

>
> This works for me:
>
>
>
> for name in ("pvcam64", "pvcam32", "pvcam"):
>
> try:
>
> self.dll = getattr(windll, name)
>
> except OSError:
>
> print "No " + name
>
> else:
>
> print "Installed " + name
>
> return


Yes that works for me, too. Thanks
 
Reply With Quote
 
Wanderer
Guest
Posts: n/a
 
      10-12-2012
On Friday, October 12, 2012 12:57:06 PM UTC-4, MRAB wrote:
> On 2012-10-12 16:36, Wanderer wrote:
>
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

>
> >

>
> > try:

>
> > self.dll = windll.pvcam64

>
> > except:

>
> > print "No pvcam64"

>
> > try:

>
> > self.dll = windll.pvcam32

>
> > except:

>
> > print "No pvcam32"

>
> > try:

>
> > self.dll = windll.pvcam

>
> > except:

>
> > print "No pvcam"

>
> > return

>
> > else:

>
> > print "installed pvcam"

>
> > else:

>
> > print "installed pvcam32"

>
> > else:

>
> > print "installed pvcam64"

>
> >

>
> This works for me:
>
>
>
> for name in ("pvcam64", "pvcam32", "pvcam"):
>
> try:
>
> self.dll = getattr(windll, name)
>
> except OSError:
>
> print "No " + name
>
> else:
>
> print "Installed " + name
>
> return


Yes that works for me, too. Thanks
 
Reply With Quote
 
Wanderer
Guest
Posts: n/a
 
      10-12-2012
On Friday, October 12, 2012 12:29:02 PM UTC-4, Dave Angel wrote:
> On 10/12/2012 11:36 AM, Wanderer wrote:
>
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

>
> >

>
> > try:

>
> > self.dll = windll.pvcam64

>
> > except:

>
> > print "No pvcam64"

>
> > try:

>
> > self.dll = windll.pvcam32

>
> > except:

>
> > print "No pvcam32"

>
> > try:

>
> > self.dll = windll.pvcam

>
> > except:

>
> > print "No pvcam"

>
> > return

>
> > else:

>
> > print "installed pvcam"

>
> > else:

>
> > print "installed pvcam32"

>
> > else:

>
> > print "installed pvcam64"

>
> >

>
>
>
> I can't help you find the dll's, because I don't run Windows. But I
>
> could help you write a clearer question:
>
>
>
> "doesn't work" is thoroughly useless for describing errors. If you're
>
> getting an exception, show us the full traceback. That will show which
>
> statement got the exception that wasn't caught. Next question is which
>
> of the dlls is missing. Are you getting an exception because it's
>
> missing or because of something more fundamental, like nesting exception
>
> handlers?
>
>
>
> Using bare excepts is almost never a good idea. If it "works" you get
>
> no clues what went wrong. For example, a typo in source code can
>
> trigger a bare exception, as can a user typing Ctrl-C. So when you're
>
> using bare excepts, you have robbed the user of any way to terminate the
>
> program.
>
>
>
> If I were you, I'd be writing a loop so there's only one try block. Too
>
> much duplicated code in the way you're doing it.
>
>
>
>
>
>
>
> --
>
>
>
> DaveA


Sorry. It was a WindowsError, but the code I posted now works for me and I can't reproduce the problem. I'll be more diligent in the future.
 
Reply With Quote
 
Wanderer
Guest
Posts: n/a
 
      10-12-2012
On Friday, October 12, 2012 12:29:02 PM UTC-4, Dave Angel wrote:
> On 10/12/2012 11:36 AM, Wanderer wrote:
>
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

>
> >

>
> > try:

>
> > self.dll = windll.pvcam64

>
> > except:

>
> > print "No pvcam64"

>
> > try:

>
> > self.dll = windll.pvcam32

>
> > except:

>
> > print "No pvcam32"

>
> > try:

>
> > self.dll = windll.pvcam

>
> > except:

>
> > print "No pvcam"

>
> > return

>
> > else:

>
> > print "installed pvcam"

>
> > else:

>
> > print "installed pvcam32"

>
> > else:

>
> > print "installed pvcam64"

>
> >

>
>
>
> I can't help you find the dll's, because I don't run Windows. But I
>
> could help you write a clearer question:
>
>
>
> "doesn't work" is thoroughly useless for describing errors. If you're
>
> getting an exception, show us the full traceback. That will show which
>
> statement got the exception that wasn't caught. Next question is which
>
> of the dlls is missing. Are you getting an exception because it's
>
> missing or because of something more fundamental, like nesting exception
>
> handlers?
>
>
>
> Using bare excepts is almost never a good idea. If it "works" you get
>
> no clues what went wrong. For example, a typo in source code can
>
> trigger a bare exception, as can a user typing Ctrl-C. So when you're
>
> using bare excepts, you have robbed the user of any way to terminate the
>
> program.
>
>
>
> If I were you, I'd be writing a loop so there's only one try block. Too
>
> much duplicated code in the way you're doing it.
>
>
>
>
>
>
>
> --
>
>
>
> DaveA


Sorry. It was a WindowsError, but the code I posted now works for me and I can't reproduce the problem. I'll be more diligent in the future.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      10-13-2012
On Fri, 12 Oct 2012 12:28:17 -0400, Dave Angel wrote:

> Using bare excepts is almost never a good idea. If it "works" you get no
> clues what went wrong. For example, a typo in source code can trigger a
> bare exception, as can a user typing Ctrl-C. So when you're using bare
> excepts, you have robbed the user of any way to terminate the program.


If you want to catch any error, use "except StandardError:". That covers
all errors but not things like KeyboardInterrupt (Ctrl-C) or SystemExit
(sys.exit()).

In situations such as this, where you try multiple candidates until one
succeeds, there's no reason to be any more specific than that. In any
case, Python's lack of formal interfaces makes it hard to reliably be more
specific.

However: you should bear in mind that loading the wrong DLL may just
result in an OS-level exception (e.g. segfault), which can't be caught.
It's preferable to allow the DLL to be explicitly selected e.g. in a
configuration file.

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      10-13-2012
Wanderer於 2012年10月12日星期五UTC+8下午11時36分27 寫道:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
>
>
>
> try:
>
> self.dll = windll.pvcam64
>
> except:
>
> print "No pvcam64"
>
> try:
>
> self.dll = windll.pvcam32
>
> except:
>
> print "No pvcam32"
>
> try:
>
> self.dll = windll.pvcam
>
> except:
>
> print "No pvcam"
>
> return
>
> else:
>
> print "installed pvcam"
>
> else:
>
> print "installed pvcam32"
>
> else:
>
> print "installed pvcam64"


In linux there are shared libraries that could be linked in the runtime.

But for the security concerns this requres the system administrator account
to install shared libraries.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ctypes module - looking for a way to dynamically call exportedfunction from a set of dlls dudeja.rajat@gmail.com Python 0 08-26-2008 10:42 AM
WindowsXP/ CTypes - How to convert ctypes array to a string? dudeja.rajat@gmail.com Python 0 08-19-2008 10:20 AM
RE: [ctypes-users] [Ann] ctypes 0.9.0 released Henk Punt Python 0 07-23-2004 10:34 PM
Calling dlls with Calldll/WinDll or Ctypes or... Todd Gardner Python 3 01-14-2004 12:57 AM
How to secure my DLLs Kelly G. ASP .Net 5 01-08-2004 12:44 AM



Advertisments