Solved

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

Posted on 2000-03-09
7
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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.

738 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