Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2008, 3:56 PM   #1
core8583
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0 core8583 is on a distinguished road
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);
The error i get is

error C2276: '&' : illegal operation on bound member function expression

I have tried using
			SNMPAPI_CALLBACK callback = &snmp::SNcallback;
			SNsession = SnmpCreateSession(NULL, 0, callback ,NULL);
but it comes back with
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 possible

Any ideas anyone, is it even possible to use a class function as callback function?
core8583 is offline   Reply With Quote
Old Jun 14th, 2008, 5:23 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Re: Assigning a class function as a callback function

The member function would have to be declared static
Cache is offline   Reply With Quote
Old Jun 14th, 2008, 5:59 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
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);
Cache is offline   Reply With Quote
Old Jun 15th, 2008, 8:42 AM   #4
core8583
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0 core8583 is on a distinguished road
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 9:58 PM. Reason: corrected code tags
core8583 is offline   Reply With Quote
Old Jun 18th, 2008, 8:20 PM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 225
Rep Power: 3 Seif is on a distinguished road
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.
Seif is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling function from class yusufozkay C# 6 Jun 18th, 2007 11:03 AM
Call the class main function quantalfred Java 6 Jul 23rd, 2006 2:38 PM
How to call normal "write" function inside a class Edgar C++ 1 May 24th, 2006 7:35 PM
Problem assigning a string to a class object. omdown C++ 3 Oct 4th, 2005 1:48 AM
Php Postgresql Class Pizentios Show Off Your Open Source Projects 15 Jun 28th, 2005 10:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC