![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 5
Rep Power: 0
![]() |
Assigning a class function as a callback function
I am trying to workout how to assign a class function call SNcallback as a windows API callback function.
My function is defined as SNMPAPI_STATUS CALLBACK SNcallback (HSNMP_SESSION SNsession, HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam, LPVOID lpClientData) and i need to assign it to the second parameter of the SnmpCreateSession function for WinSNMP SNMPAPI_CALLBACK callback = &SNcallback; SNsession = SnmpCreateSession(NULL, 0, callback ,NULL); error C2276: '&' : illegal operation on bound member function expression I have tried using SNMPAPI_CALLBACK callback = &snmp::SNcallback; SNsession = SnmpCreateSession(NULL, 0, callback ,NULL); error C2440: 'initializing' : cannot convert from 'SNMPAPI_STATUS (__stdcall snmp::* )(HSNMP_SESSION,HWND,UINT,WPARAM,LPARAM,LPVOID)' to 'SNMPAPI_CALLBACK'
There is no context in which this conversion is possibleAny ideas anyone, is it even possible to use a class function as callback function? |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Re: Assigning a class function as a callback function
The member function would have to be declared
static |
|
|
|
|
|
#3 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Re: Assigning a class function as a callback function
Here is an untested hint to calling a non-static member function in the callback. 'SNcallback' could of course be made a static member function of 'SNMP_worker' if you'd like.
struct SNMP_worker
{
void run(/* params */) {}
};
SNMPAPI_STATUS CALLBACK SNcallback(
HSNMP_SESSION SNsession
, HWND hWnd
, UINT wMsg
, WPARAM wParam
, LPARAM lParam
, LPVOID lpClientData
)
{
if (SNMP_worker* p_worker =
static_cast<SNMP_worker*>(lpClientData))
{
p_worker->run(/* params */);
}
}
// ...
SNMP_worker worker;
HSNMP_SESSION session = SnmpCreateSession(NULL, 0, &SNcallback, (void*)&worker); |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2008
Posts: 5
Rep Power: 0
![]() |
Re: Assigning a class function as a callback function
Thanks alot, I couldn't get it work without defining it as a static, and so the function losed access to all the class functions and data sets. I got around this by passing a struct into the function with the
this pointer of the class so i can't get to all the data i need to get to.Last edited by Ancient Dragon; Jun 15th, 2008 at 8:58 PM. Reason: corrected code tags |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
Re: Assigning a class function as a callback function
Just an extra note. Although static members are type compatible with pointer to functions with most compilers, there are some ABI's where they may differ between C and C++.
In these cases you'd probably want a wrapper function that uses a global object to remember the function. http://www.parashift.com/c++-faq-lit...o-members.html 33.2 for an example of this. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Calling function from class | yusufozkay | C# | 6 | Jun 18th, 2007 10:03 AM |
| Call the class main function | quantalfred | Java | 6 | Jul 23rd, 2006 1:38 PM |
| How to call normal "write" function inside a class | Edgar | C++ | 1 | May 24th, 2006 6:35 PM |
| Problem assigning a string to a class object. | omdown | C++ | 3 | Oct 4th, 2005 12:48 AM |
| Php Postgresql Class | Pizentios | Show Off Your Open Source Projects | 15 | Jun 28th, 2005 9:55 AM |