?
Solved

Help : pointer to a callback function

Posted on 1997-09-02
2
Medium Priority
?
424 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 140 total points
ID: 1305368
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
ID: 1305369
That works fine!
Answer short and clear.

Thanks

Steph.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

809 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