[python-win32] About ctypes and getprocaddress
Alejandro Bermudez Aragurz
abermudez at hispasec.com
Mon Aug 11 13:01:17 CEST 2008
Hello list,
I'm working with ctypes doing a wrapper for a DLL. I use
LoadLibrary, FreeLibrary and GetProcAddress of windows system API
(kernel32.dll).
The problem is the following: Inside a DLL there are 4 function. I
can to request its address of 3 functions without problem
(GetProcAddress return the correct value), but the 4th function work
sometimes. Sometime, GetProcAddress return NULL value and GetLastError
return error code 127. But others time, return the correct address and
GetLastError return 0 (no error ocurred). I don't change the code
between each try. I print too the pefile exports and this function
appers in the pe file. The DLL is always the same. The python code is
always the same. Why don't work well GetProcAddress sometime, and other
time work well? Here post my code test:
import ctypes
import time
import tool_pefile
#DLLs manager
funcion_open = ctypes.windll.kernel32.LoadLibraryA
funcion_close = ctypes.windll.kernel32.FreeLibrary
funcion_direccion = ctypes.windll.kernel32.GetProcAddress
#Manejo de errores
funcion_error = ctypes.windll.kernel32.GetLastError
if __name__ == '__main__':
count = 5
while count:
#pe file
hPE = tool_pefile.Tool()
txt = hPE.info_txt("c:\\windows\\system32\\TrIDLib.dll")
print txt
#Se abre la DLL
dllname =
ctypes.create_string_buffer(len("c:\\windows\\system32\\TrIDLib.dll"))
dllname.value = "TrIDLib.dll"
hDLL = funcion_open( dllname )
error = funcion_error()
print str(error)
#name = "TrID_Analyze"
name = "TrID_LoadDefsPack"
print hDLL
#time.sleep(4)
funcion = ctypes.create_string_buffer(len(name))
funcion.value = name
print funcion.value
direccion = funcion_direccion( hDLL, funcion )
print hex(direccion)
error = funcion_error()
print str(error)
res = funcion_close( hDLL )
print res
error = funcion_error()
print str(error)
print "To sleep"
count -= 1
time.sleep(2)
More information about the python-win32
mailing list