Sign in to follow this  
michaudj

How to make a CALLBACK a friend function [resolved]

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this