How to make a CALLBACK a friend function [resolved]

Started by
3 comments, last by michaudj 15 years, 11 months ago
Hi My problem was : Make a CALLBACK function a friend function from a class in a namespace. Platform : Windows XP sp2 Language : C++ Using Microsoft Visual Studio 2008 The solution i found is :

namespace device {
	namespace input {
class Keyboard
{
	friend LRESULT LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
	//friend LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); //Same Error as above
public:
	Keyboard();
	~Keyboard();
private:
	int KeyboardProc( int nCode, WPARAM wParam, LPARAM lParam );
	
};
}
}










#include "stdafx.h"
#include "KeyboardDevice.hpp" // Class Keyboard
#include <iostream>

static device::input::Keyboard* pKBDev;
HHOOK hkb;
HINSTANCE hins;

//... some code ....

LRESULT CALLBACK device::input::LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{ 
	if(nCode < 0)
		return CallNextHookEx(hkb, nCode, wParam, lParam);

	pKBDev->KeyboardProc( nCode, wParam, lParam );

	return CallNextHookEx(hkb, nCode, wParam, lParam); 

}

extern "C" __declspec(dllexport) BOOL installhook()
{
    hkb=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)device::input::LowLevelKeyboardProc,hins,0);
    return TRUE;

}








[Edited by - michaudj on May 7, 2008 1:03:53 PM]
JoelBeen Programming for 4 month
Advertisement
Could you post the entire error message? Just posting error codes is not particularly useful.
yes, certainly
here it is
1>.\KeyboardManager.cpp(50) : error C2440: 'cast de type' : impossible de convertir de 'overloaded-function' en 'HOOKPROC'


Maybe i don't understand the friend correctly


i simply want to be able to call the function KeyboardProc from the LowLevelKeyboardProc

And i though this would be simple :(

[Edited by - michaudj on May 7, 2008 12:57:09 PM]
JoelBeen Programming for 4 month
You are missing the CALLBACK #define in front of the function prototype when you friend LowLevelKeyboardProc.

IE:

friend LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);


CALLBACK is #defined to be __stdcall. __stdcall LowLevelKeyboardProc is a different function from LowLevelKeyboardProc from the compiler's persective.
sorry, i accidently erased the comment from the code, but i tried both and they gave the same error.

I just added it in comment to the code, to show i tried this solution.

Thanks you very much for the help though.


UPDATE :

Somewhere within a file i used this : using namespace device;

which made the thing fail... so i have updated the code... a new error arise !!! :P


UPDATE ; PROBLEM SOLVE!

Thanks CPR007, though the error was somewhere else, you made me realize my misstake !

[Edited by - michaudj on May 7, 2008 12:27:24 PM]
JoelBeen Programming for 4 month

This topic is closed to new replies.

Advertisement