Solved

Help : pointer to a callback function

Posted on 1997-09-02
2
409 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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