"legard_new" <legar...@gmail.com> wrote in message news:70faf0b4-fead-49ac-bf18-e182fd63b...@j8g2000yqd.googlegroups.com...
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.

I don't see anything wrong with your declarations. It indicates you registered the callback successfully. Since the callback sounds like it monitors changes in "link state" maybe it isn't changing. Perhaps you need to call another function that causes this change.

Here's a DLL I wrote to test your code, but I added a ChangeLinkState function. When I call it after running your code the callback gets called, so your code is working properly.

(Compiled with Visual Studio 2008 "cl /MD /LD demo.c"

typedef unsigned char BYTE;

typedef struct tagLINKSTATE {
BYTE OldState, NewState;
} LINKSTATE;

typedef int (*CB)(LINKSTATE*);

CB g_cb;
BYTE g_linkState;

__declspec(dllexport) int __stdcall Callback(int a,CB cb)
{
g_cb = cb;
return 1;
}

__declspec(dllexport) void __stdcall ChangeLinkState(BYTE newState)
{
LINKSTATE state;
state.OldState = g_linkState;
state.NewState = newState;
g_linkState = newState;
g_cb(&state);
}

-Mark


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to