Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2012, 11:02 AM
Motimon Motimon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 Motimon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 4 sec
Reputation Power: 0
Reading DLLs in Python

I'm an absolute beginner in Python with a basic understanding of C++ and Java. I'm looking to extract all the functions from a "propa.dll," but this is my first time doing more than working with very basic file reading/writing.

From what I've read, I should be looking into the ctypes library to accomplish this. I found the python.org ctypes-tutorial to be a little unintuitive (maybe I need a stronger base?), and have instead been reading from books and checking other forums for some guidance. So far I have:

from ctypes import *
propa = windll.LoadLibrary("D:\Windows\SysWOW64\propa.dll")

From this basic piece of code, I've read that I need to establish a prototype function and set function parameters, followed by mapping the function to a python name. Would anyone be able to point me to appropriate resources, or explain/provide me an example?

For reference:

propa.dll - google it if you like. I can't link the URL

How to load DLL using ctypes in Python on StackOverflow (the example I've been following)

Thanks

Reply With Quote
  #2  
Old June 27th, 2012, 01:44 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
The only part of your code that's useful is to import ctypes. Your win_load or whatever function isn't useful unless you already needed to do it for another reason. Read on---

I avoid MS-Windows systems, so I'm guessing with help from
http://docs.python.org/py3k/library/ctypes.html
opposed to testing.
I didn't find the propa archive as a shared object, and I thought I read they don't distribute the source. Again, I cannot test.
Somewhere in the following mess I've demonstrated hypot in libm which is quite similar to these functions.

Code:
$ # On linux 64 bit system, Build and run the sample c program:
$ ./a.out 

Gaseous attenuation = 0.11 dB

Gaseous attenuation exceeded for 0.01 % of the time = 0.14 dB

Clouds attenuation exceeded for 0.01 % of the time = 0.22 dB

Scintillation impairment exceeded for 0.01 % of the time = 0.43 dB

Rain attenuation exceeded for 0.01 % of the time = 6.00 dB

Convert the c program to python:
Code:
import math
import propa

TAU = 2*math.pi                         # http://www.youtube.com/watch?v=jG7vhMMXagQ

lat = 46.217                            # Latitude (°)
lon = 6.12                              # Logitude (°)

hs = 0.8140                             # Earth station altitude (km)
E = 33.0*TAU/360                        # Link elevation angle (°)

f = 12.0                                # Link frequency (GHz)
to = 45.0                               # Tilt polarization angle (°)

D = 1.0                           # Earth station antenna diameter (m)
eta = 0.5			# Earth station antenna efficiency
import propa

p = 0.01                                # Percentage of the time

# Intermediate parameters computation
hr = propa.rain_height(lat,lon)
R001 = propa.rain_intensity(lat,lon,0.01)
Temp = propa.temperature(lat,lon)
ro = propa.SWVD(lat,lon)
WVC = propa.IWVC(lat,lon,p)
LWC = propa.LWCC(lat,lon,p)
Nwet = propa.NWET(lat,lon)

# GASEOUS ATTENUATION
Agaseous = propa.gaseous_attenuation(f,E,Temp,ro)
print("\nGaseous attenuation = %.2f dB"%Agaseous)

# GASEOUS ATTENUATION EXCEEDED FOR p% OF THE TIME
Agaseous = propa.gaseous_attenuation_exc(f,E,Temp,WVC,ro)
print("\nGaseous attenuation exceeded for %.2f %% of the time = %.2f dB"%(p,Agaseous))

# CLOUD ATTENUATION EXCEEDED FOR p% OF THE TIME
Aclouds = propa.cloud_attenuation(f,E,LWC)
print("\nClouds attenuation exceeded for %.2f %% of the time = %.2f dB"%(p,Aclouds))

# IMPAIRMENT DUE TO SCINTILLATION EXCEEDED FOR p% OF THE TIME
Iscint = propa.scintillation(Nwet,f,E,p,hs,eta,D)
print("\nScintillation impairment exceeded for %.2f %% of the time = %.2f dB"%(p,Iscint))

# RAIN ATTENUATION EXCEEDED FOR p% OF THE TIME
Arain = propa.rain_attenuation(lat,f,E,p,hs,hr,R001,to)
print("\nRain attenuation exceeded for %.2f %% of the time = %.2f dB"%(p,Arain))

propa.py, the part you await:
Code:
# http://docs.python.org/py3k/library/ctypes.html

import ctypes
D = ctypes.c_double

# You probably want this instead of windll.LoadLibrary
propa = ctypes.WinDLL(r"D:\Windows\SysWOW64\propa.dll") # use a raw string, otherwise your backslashes may be lost.

def dress(library,attribute,return_type,argument_types):
    function = getattr(library,attribute)
    function.argtypes = argument_types
    function.restype = return_type
    return function

NWET = dress(propa,'NWET',D,(D,D,)) # extern double NWET(double lat, double lon);
rain_height = dress(propa,'rain_height',D,(D,D,)) # extern double rain_height(double lat, double lon);

# fill in the rest
#extern double temperature(double lat,double lon);
#extern double rain_intensity(double lat, double lon, double p);
#extern double rain_probability(double lat, double lon);
#extern double LWCC(double lat, double lon, double p);
#extern double IWVC(double lat, double lon, double p) ;
#extern double SWVD(double lat, double lon);
#extern double gaseous_attenuation(double f,double E,double Temp, double ro);
#extern double gaseous_attenuation_exc(double f,double E,double Temp, double WVC, double ro);
#extern double cloud_attenuation(double f,double E,double L);
#extern double rain_attenuation(double lat,double f,double E,double p,double hs,double hr,double R001, double to);
#extern double scintillation(double Nwet, double f,double E,double p,double hs,double eta, double D);

#
#>>> import ctypes
#>>> math = ctypes.CDLL(ctypes.util.find_library('m'))
#>>> math
#<CDLL 'libm.so.6', handle 7fc1831c94d8 at 165aa50>
#>>> math.hypot
#<_FuncPtr object at 0x15fd600>
#>>> D = ctypes.c_double
#>>> hypot = math.hypot
#>>> hypot.argtypes = (D,D,)
#>>> hypot.restype = D
#>>> hypot(3,4)
#5.0
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : June 27th, 2012 at 01:50 PM. Reason: highlight the error

Reply With Quote
  #3  
Old June 27th, 2012, 01:52 PM
Motimon Motimon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 Motimon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 4 sec
Reputation Power: 0
Thank you very much for your quick reply. I'll be spending the next couple of hours reading this over

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Reading DLLs in Python

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap