Solved

Wrapper C++ classes for API's with callbacks.

Posted on 2000-03-09
7
303 Views
Last Modified: 2010-04-02
Hi.
Question: What's the best way to wrap an API call that receives a callback function pointer in a C++ class.

I'm asking because:
1. Callback functions have to be static.
2. They need to access the members of the class in which they reside. In order for them to do that, these have to be static as well...

I tried taking the callbacks outside the class definition, but that's plain ugly. It worked though - But I had to pass them a pointer to my class, which meant they could only access the public members...

Is there a smarter way of solving this?
Thanks,
Assaf.
0
Comment
Question by:AssafLavie
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2602096
set it as friend function
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2602098
Most windows callback function are passed a VOID * pointer that is specified when the callback is "registered".  You can use this VOID * to specify a pointer to an object whose member function is called.  The static callback function can convert this VOID * pointer back to a  pointer to an object and then call the member function.

example follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2602119
class EnmWnd
{
   static BOOL CALLBACK EnmPrc(HWND WndHnd,LPARAM LngPrm)
   {
      EnmWnd *ObjPtr = (EnmWnd *)LngPrm; // Convert back to a object pointer.
      return ObjPtr->WndFnd(WndHnd); // Call member procedure.
   }
public:
   void Enumerate()
   {
      EnumWindows((WNDENUMPROC) EnmPrc,(LPARAM) this);
   }
   BOOL WndFnd(HWND WndHnd)
   {
//        Do something with the window handle and this object.
       return TRUE;
   }
};
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 4

Author Comment

by:AssafLavie
ID: 2602367
Thanks.
0
 
LVL 4

Author Comment

by:AssafLavie
ID: 2602373
But what should I do when I cannot pass an application defined value to the callback?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2602971
Redesign.

You might be able to use a global variable to store a pointer to the object.  (Assuiming you don't have to worry about multi-threadding.)

But honestly, I dont't think that will ever be a problem.  I can't think of any that doesn't take a parameter like this.
0
 
LVL 4

Author Comment

by:AssafLavie
ID: 2603937
Thanks again.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question