0

Hi all,
Can I know if there's any other way to load and call functions inside a dll, besides using ctypes module?
Thanks......

Best Regards
Vincent

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by bumsfeld
0

If you don't have any arguments to pass you can import the C function directly from msvcrt.dll:

# optional console wait for keypress, any key
# C:\Windows\System32\msvcrt.dll contains C functions
# works only with console (many IDEs have their own output)

from msvcrt import getch

print "Hello!"

getch()  # this is the C getch() function!

If you have to pass arguments you need the ctypes module (Python25 has the ctype module inclused), so you can convert Python argument types to C types:

# access the C function strchr()

from ctypes import *

libc = cdll.msvcrt  # accesses msvcrt.dll (the windows C function dll)
strchr = libc.strchr
strchr.restype = c_char_p # c_char_p is a pointer to a string
strchr.argtypes = [c_char_p, c_char]

print strchr("abcdef", "d")  # def

# optional console wait for keypress, any key
# C:\Windows\System32\msvcrt.dll contains C functions
# works only with console (many IDEs have their own output)
from msvcrt import getch
getch()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.