Solved

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

Posted on 2000-03-09
7
316 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
Independent Software Vendors: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

740 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