Solved

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

Posted on 2000-03-09
7
293 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now