Solved

Help : pointer to a callback function

Posted on 1997-09-02
2
400 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 32
how to monitor remote shell execution on linux 9 97
centeredAverage challenge 8 131
mapBully challenge 6 115
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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