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

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()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.