Solved

Help : pointer to a callback function

Posted on 1997-09-02
2
397 Views
Last Modified: 2013-11-20
Hi everybody, I'm programming in Visual C++ 4.0 for Windows 95.

I'm trying to handle midi in inputs with a callback function namely :

void CALLBACK MidiInProc(HMIDIIN hmi,UINT wMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)

And in order to open the Midi In Port I need a pointer to the callback function to use in the following function :

MMRESULT midiInOpen(LPHMIDIIN lphMidiIn, UINT uDeviceID,
DWORD dwCallback, DWORD dwCallbackInstance, DWORD dwFlags);

dwCallback is the pointer and must be casted to double word.
Here is the declaration of the damned pointer :

void (CALLBACK *pMI)(HMIDIIN,UINT,DWORD,DWORD,DWORD) = MidiInProc;

and here it's passed as parameter in the function which opens Midi In port :

midiInOpen(&hmi,device,(DWORD)pMI,NULL,CALLBACK_FUNCTION);

But that works only when my callback function is a global function, and you can guess that I am not interested, I want it to be a class method.

So I put the callback function in a class, I declared my pointer so :

void (CALLBACK MyClass::*pMI)(HMIDIIN,UINT,DWORD,DWORD,
DWORD) = MidiInProc;

But it gives me the following error code :

error C2643: illegal cast from pointer to member

Please any suggestions about how to get it work fine ?
And I would like to use those callback functions, I know there are other ways, but I will change only if there 's no solution for my problem.

Thanks !!!

Steph
0
Comment
Question by:ranskalainen
2 Comments
 
LVL 1

Accepted Solution

by:
kargo earned 70 total points
Comment Utility
Win SDK functions know nothing about C++ and the magic "this" pointer.  This being the case you have to trick the 2 into working with each other.
The correct method is 2 fold.  The function itself MUST BE a static member function.  This means that the function exists outside the scope of data for the class, e.g., you only have access to static data from static functions.
The 2nd part is to pass a pointer to the class using the optional user data pointer of the callback.  Then once the static function gets called cast the LPVOID pointer to a class pointer and party on.

Ken

0
 

Author Comment

by:ranskalainen
Comment Utility
That works fine!
Answer short and clear.

Thanks

Steph.
0

Featured Post

What Is Threat Intelligence?

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

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

744 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

11 Experts available now in Live!

Get 1:1 Help Now