#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep 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
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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
    Last edited by b49P23TIvg; June 27th, 2012 at 01:50 PM. Reason: highlight the error
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0
    Thank you very much for your quick reply. I'll be spending the next couple of hours reading this over

IMN logo majestic logo threadwatch logo seochat tools logo