Call CFUNCTYPE, class Structure, ctype, dll and Callback function problem

legard_new legard4d at gmail.com
Sat Jul 31 15:09:15 EDT 2010


Hello,

I have a problem with calling Callback function with Python. I have a
DLL file.

Below is a description in documentation.
	FUNCTION Callback
		Arguments: Callback ID integer read-only, immediate value
		CBackProc address read-only, immediate value
		Returns: status
			The Callback routine registers the callback routine for
notification of a link state change.
			The callback procedure provided by the application must accept one
parameter:
				the address of the structure containing the data.

		CallbackID An integer identifying the callback routine being
provided to the API. Can be one of 1 or 2.
		CBackProc The address of the callback procedure that the API will
execute.

		This procedure must accept one parameter, of type LinkStateStruct,
passed by reference.

		LinkStateStruct read-only, by reference
		OldState: byte
		NewState byte

		The routine will return one of the following error codes:
		1 - The callback address was successfully registered by the API.
		2 - The API has not been initialised.
		3 - The CallbackID value was not recognised as a valid link state
callback.

Here is my little code in Python:

from ctypes import *
class LinkStateStruct(Structure):
    _fields_ = [("OldState", c_byte),
                ("NewState", c_byte)]
demoapi=windll.LoadLibrary("D:\\DEMO.dll")
#DECLARE CALLBACK FUNCTION
g_HLSC = CFUNCTYPE(c_int, POINTER(LinkStateStruct))
def py_g_HLSC(x):
    print "CALLBACK__py_g_HLSC_FIRED->", x
    return 0
g__HLSC = g_HLSC(py_g_HLSC)
status=demoapi.Callback(1,g__HLSC)
print "Status=",status

My result is:
Status= 1

But Callback function is not executed. If think that i call g__HLSC
witohout paramter that in document "The callback procedure

provided by the application must accept one parameter: the address of
the structure containing the data.". I don't now how to do.

Please help.

Best regards,
Gregory



More information about the Python-list mailing list