C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-24-2008, 11:24 AM   #1
Registered User
 
Join Date: Aug 2008
Posts: 1
Delegate function implementation in C++ for class callback invocation

Hi -
I have created wrapper classes around Win32 API calls in GNU C++ (v 4.3.1) using Win32API library (v 3.11). I am having difficulty in the implementation of API functions that require callback functions as arguments (presumably since I have declared the callback functions as member functions of the class instead of in the global scope and have not implemented the c++ equivalent of a delegate function).

Here is a simple example using the EnumWindows Win32 API call:

Code:
class MyClass {

    public:
        void AccessorFunc() {
            EnumWindows(this->EnumWindowsProc, 0);
        }
    private:
        BOOL CALLBACK MyClass::EnumWindowsProc(HWND hWnd, LPARAM lParam) {
            // do whatever...
        }
};
I receive the following compile-time error using the MinGW compiler (version 3.14):

Code:
In member function 'void MyClass::AccessorFunc()' argument type 'BOOL (MyClass::)(HWND__*,LPARAM)' does not match 'BOOL (*)(HWND__*,LPARAM)'
MSDN put together an article on "How to: Implement Callback Functions" with this same scenario using .NET, C# and VC++ frameworks, which makes use of the delegate function.

I'm still working on getting up-to-speed on the c++ implementation of the delegate function, though I would like to avoid the use of additional functions outside the class scope in preference of an inline solution, if feasible. At this point, any suggestions are welcome to get the code to compile.

Thanks!
just_me is offline   Reply With Quote
Old 08-24-2008, 11:39 AM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
There are two things here:
First, this->EnumWindowProc is illegal syntax. The standard say to create a function pointer to a member function, you use the syntax &Class::MemberFunction.
Second, the problem is that you cannot use a class member function as a callback (how would the function know what instance the function belongs to?).
You will have to either create a static member function or a global function. The usual approach is also to pass "this" as the lparam, and to use it to call a non-static member function.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
callback, delegate

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
How do wait till a callback function returns poorkoder C Programming 1 06-16-2008 11:22 AM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
<Gulp> kryptkat Windows Programming 7 01-14-2006 01:03 PM
Pointer to member function as callback function ninebit C++ Programming 10 03-01-2002 05:52 AM


All times are GMT -6. The time now is 08:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22