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
Solved

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

Posted on 2000-03-09
7
312 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

829 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