Solved

Help : pointer to a callback function

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
wordcount challenge 11 123
Problem to event 3 98
Problem to open Excel file 15 140
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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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