Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

Calling DLLs from Python [Windows]

Georgy Pruss
P: n/a
Georgy Pruss
Hi all,

How can I call a function which is in a Windows DLL? For example,

import win32api
lib_hnd = win32api.LoadLibrary( "user32.dll" )
if lib_hnd:
fn_addr = win32api.GetProcAddress( lib_hnd, "MessageBeep" ) # returns int(2010532466)
if fn_addr:
# Here I'd like to call fn_addr. In C it would be plain fn_addr()
win32api.FreeLibrary( lib_hnd )

Google didn't help on this.

Thank you,
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base6 4')


Jul 18 '05 #1

2 Replies



Brian Elmegaard
P: n/a
Brian Elmegaard

re: Calling DLLs from Python [Windows]

"Georgy Pruss" <SEE_AT_THE_END@hotmail.com> writes:
[color=blue]
> How can I call a function which is in a Windows DLL? For example,[/color]

ctypes is your friend
from ctypes import *

If you have t.dll exporting INCREMENT

# load dll
inc = windll.LoadLibrary("t.dll")

# Initiate c-variable for the dll
n=c_int(1)

# Call the dll one
inc.INCREMENT(byref(n))
print "The integer is now: %d" % n.value

# And a few times more:
for i in range(5):
inc.INCREMENT(byref(n))
print "The square of the integer is now: %d" % n.value**2
[color=blue]
> Google didn't help on this.[/color]

???????

http://groups.google.com/groups?hl=e...=Google+Search



--
Brian (remove the sport for mail)
http://www.et.dtu.dk/staff/be
Jul 18 '05 #2

Georgy Pruss
P: n/a
Georgy Pruss

re: Calling DLLs from Python [Windows]


"Brian Elmegaard" <brian@rk-speed-rugby.dk> wrote in message news:un0cchxv5.fsf@mail.afm.dtu.dk...[color=blue]
> "Georgy Pruss" <SEE_AT_THE_END@hotmail.com> writes:
>[color=green]
> > How can I call a function which is in a Windows DLL? For example,[/color]
>
> ctypes is your friend[/color]

Thank you!
[color=blue]
>[color=green]
> > Google didn't help on this.[/color]
>
> ???????
>
> http://groups.google.com/groups?hl=e...=Google+Search
>[/color]

Sure. The right question is half of the answer :)
Thanks!

Georgy.
[color=blue]
>
>
> --
> Brian (remove the sport for mail)
> http://www.et.dtu.dk/staff/be[/color]


Jul 18 '05 #3

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your Python question on Bytes

You can also browse similar questions: Python

Get Python Help

Get Python help from a network of professionals.

Post your Question » Over 331,212 Members | 3356 Online