|
-
Jan 29th, 2003, 01:30 AM
#1
callback in a class
I need to make a callback for a function in a class, when i just declare the function itself, the compiler complains when i want to pass the pointer to the function.
I know the anser has to do with a typedef (did something like this a long time ago), but I forgot how to declare the function with it....
here's something similar to what I have
Code:
class myclass
{
private:
void CALLBACK myCallbackFunction(long num);
void some_other_function(void);
}
void CALLBACK myclass::myCallbackFunction(long num)
{
}
void myclass::some_other_function(void)
{
// compiler gives error on next line
// cannot convert (function prototype here) to DWORD_PTR
ret = API_function((DWORD_PTR)myCallbackFunction);
}
Anyone knows how to make it work ?
Last edited by CVMichael; Jan 29th, 2003 at 03:11 AM.
-
Jan 29th, 2003, 02:47 AM
#2
Hyperactive Member
You have mispelled CALLBACK.
-
Jan 29th, 2003, 02:51 AM
#3
Your right, in this code I did (I edited to CALLBACK not COLLBACK as I had before)... but it is not mispelled in my real code...
If I did, the compiler would give me a sintax error...
And the code I wrote there is just a sample of what i'm doing... If I paste my real class, it will be too much code to figure out the problem...
Last edited by CVMichael; Jan 29th, 2003 at 02:54 AM.
-
Jan 29th, 2003, 03:14 AM
#4
Hyperactive Member
Re: callback in a class
How about trying this
Code:
class myclass
{
private:
void CALLBACK myCallbackFunction(long num);
void some_other_function(void);
}
void CALLBACK myclass::myCallbackFunction(long num)
{
}
void myclass::some_other_function(void)
{
// compiler gives error on next line
// cannot convert (function prototype here) to DWORD_PTR
ret = API_function((DWORD_PTR)myclass::myCallbackFunction);
}
-
Jan 29th, 2003, 03:21 AM
#5
Now I get 2 errors at the same line...
error C2440: 'type cast' : cannot convert from 'void (__stdcall myclass::* )(long)' to 'DWORD_PTR'
-
Jan 29th, 2003, 03:26 AM
#6
Hyperactive Member
What's that DWORD_PTR doing there?
Code:
ret = API_function((DWORD_PTR)myCallbackFunction);
All pointers whether it is pointing to char or short or integer are all 32 bit. Pointer is a special type.
What you are doing, is casting the function to a dword pointer?
-
Jan 29th, 2003, 03:31 AM
#7
This is the API function i'm trying to use:
MMRESULT acmStreamOpen(
LPHACMSTREAM phas,
HACMDRIVER had,
LPWAVEFORMATEX pwfxSrc,
LPWAVEFORMATEX pwfxDst,
LPWAVEFILTER pwfltr,
DWORD_PTR dwCallback,
DWORD_PTR dwInstance,
DWORD fdwOpen
);
If you look at dwCallback parameter, you'll see it's a DWORD_PTR
-
Jan 29th, 2003, 03:36 AM
#8
Hyperactive Member
Originally posted by CVMichael
Now I get 2 errors at the same line...
error C2440: 'type cast' : cannot convert from 'void (__stdcall myclass::* )(long)' to 'DWORD_PTR'
What's the other error?
-
Jan 29th, 2003, 03:37 AM
#9
I made a mistake there, I actually got one error... (the one I pasted)
-
Jan 29th, 2003, 03:47 AM
#10
But how to you do a callback in a class in general ?
-
Jan 29th, 2003, 03:48 AM
#11
Hyperactive Member
Then I do not know what the error may be, though everything seems to be alright.
-
Jan 29th, 2003, 03:53 AM
#12
Hyperactive Member
Originally posted by CVMichael
But how to you do a callback in a class in general ?
The class method will have to be static.
-
Jan 29th, 2003, 03:58 AM
#13
I made my callback static, now I get this error...
error C2352: 'CACMClass::StreamEvent' : illegal call of non-static member function
I'm trying to call a method in the class from the callback function, how do I do it ?
-
Jan 29th, 2003, 04:46 AM
#14
Hyperactive Member
From MSDN
Compiler Error C2352
'class::function' : illegal call of non-static member function
The specified nonstatic member function was called in a static member function.
The following is an example of this error:
class X
{
public:
static void func1();
void func2();
static void func3()
{
func1(); // OK, calls static func1
func2(); // error, calls nonstatic func2
}
};
==========================================
Your myCallbackFunction() uses non-static members, so we have to try the this pointer approach.
typedef (myclass::*PFN)();
PFN pfn=&myclass::myCallbackFunction;
........
API_function((DWORD_PTR)this->*pfn);
-
Jan 29th, 2003, 04:52 AM
#15
hehe, I was doing just tht right now... but I get a warning:
warning C4312: 'type cast' : conversion from 'DWORD' to 'CACMClass *' of greater size
the api has a parameter to pass a value to the callback function, it's DWORD dwInstance
As I know DWORD is unsigned long, and it should be enough to pass a class pointer right ?
Then why is the warning " ... of greater size" ?
-
Jan 29th, 2003, 05:02 AM
#16
Hyperactive Member
Originally posted by CVMichael
hehe, I was doing just tht right now... but I get a warning:
warning C4312: 'type cast' : conversion from 'DWORD' to 'CACMClass *' of greater size
the api has a parameter to pass a value to the callback function, it's DWORD dwInstance
1)As I know DWORD is unsigned long, and it should be enough to pass a class pointer right ?
Then why is the warning " ... of greater size" ?
1) Yes
2) Dunno, but try this APIFunction((DWORD)this);
I admit your case is special that your callback is going to be used in the class itself and I had never used acm multimedia functions myself.
Excuse me, all this while I am busy chatting online with a girl, I don't think I can help you, maybe you would want to search the C++ forum.
-
Jan 29th, 2003, 05:04 AM
#17
Thanks for you help...
I think it should work now, although I cannot test it yet, cuz the rest of the code is not done...
-
Jan 29th, 2003, 05:16 AM
#18
Hyperactive Member
Remember to cast it back before using it
Code:
myclass* ptr = (myclass*) fdwOpen;//I assume fdwOpen contains this
ptr->mymemberfunction();
Really have to go now.
-
Jan 29th, 2003, 06:41 AM
#19
Hyperactive Member
It just came to me that you need not pass the class pointer to the API Function because the Callback function which gets it, is a member function of the class itself. So just pass a NULL.
So the casting of the previous post is not needed as well.
Only if your Callback function is 'external' to the class, then you need to pass the class pointer.
-
Jan 30th, 2003, 08:43 AM
#20
Search the forum for callback and class, this has already been answered a few times.
The reason for the warning "of greater size" is due to a compiler setting. DWORD is unsigned long, a 32-bit value. A pointer is 32-bit too, but only for now. Soon there will be 64-bit systems where pointers are 64 bits long. But DWORD will still be 32-bit. Therefore VC++ warns you about this 64-bit portability problem.
DWORD_PTR however is an unsigned value which is the same size as a pointer.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|