Call CFUNCTYPE, class Structure, ctype, dll and Callback functionproblem

L

legard_new

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
 
M

Mark Tolonen

legard_new said:
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
 
L

legard_new

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

Thanks Mark for good news.
Exactly, i check again documentation and i must call another
functions.

Thanks for help.

Best regards,
Gregory
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top