Is it logical to register non static method as callback function?
up vote
-1
down vote
favorite
I created 2 class.class KeyLogger
and class cHookProc
first i create cHookProc
in KeyLogger
class.
then i get address of LRESULT CALLBACK cHookProc::HookProc
by unsigned int cHookProc::getCallBackAddr()
is it logical and works fine always?
this is KeyLogger.h
#pragma once
#include <time.h>
#include <windows.h>
class cHookProc
public:
LRESULT CALLBACK HookProc(int code, WPARAM wp, LPARAM lp);
unsigned int getCallBackAddr();
private:
;
class KeyLogger
public:
KeyLogger();
~KeyLogger();
private:
cHookProc *cHkProc;
;
and this is KeyLogger.cpp
#include "KeyLogger.h"
KeyLogger::KeyLogger()
cHkProc = new cHookProc;
int i = cHkProc->getCallBackAddr();
HOOKPROC hprc = (HOOKPROC)i;
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, hprc, NULL, NULL);
KeyLogger::~KeyLogger()
delete cHkProc;
LRESULT CALLBACK cHookProc::HookProc(int code, WPARAM wp, LPARAM lp)
if (code == 0)
.
.
.
return CallNextHookEx(NULL, code, wp, lp);
unsigned int cHookProc::getCallBackAddr()
LRESULT(__stdcall cHookProc::*ptrtofn)(int, WPARAM, LPARAM) = &cHookProc::HookProc;
unsigned int *i;
i = (unsigned int*)&ptrtofn;
return *i;
c++ callback static
add a comment |
up vote
-1
down vote
favorite
I created 2 class.class KeyLogger
and class cHookProc
first i create cHookProc
in KeyLogger
class.
then i get address of LRESULT CALLBACK cHookProc::HookProc
by unsigned int cHookProc::getCallBackAddr()
is it logical and works fine always?
this is KeyLogger.h
#pragma once
#include <time.h>
#include <windows.h>
class cHookProc
public:
LRESULT CALLBACK HookProc(int code, WPARAM wp, LPARAM lp);
unsigned int getCallBackAddr();
private:
;
class KeyLogger
public:
KeyLogger();
~KeyLogger();
private:
cHookProc *cHkProc;
;
and this is KeyLogger.cpp
#include "KeyLogger.h"
KeyLogger::KeyLogger()
cHkProc = new cHookProc;
int i = cHkProc->getCallBackAddr();
HOOKPROC hprc = (HOOKPROC)i;
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, hprc, NULL, NULL);
KeyLogger::~KeyLogger()
delete cHkProc;
LRESULT CALLBACK cHookProc::HookProc(int code, WPARAM wp, LPARAM lp)
if (code == 0)
.
.
.
return CallNextHookEx(NULL, code, wp, lp);
unsigned int cHookProc::getCallBackAddr()
LRESULT(__stdcall cHookProc::*ptrtofn)(int, WPARAM, LPARAM) = &cHookProc::HookProc;
unsigned int *i;
i = (unsigned int*)&ptrtofn;
return *i;
c++ callback static
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
4
Putting address intoint
is not going to work. Non-static member can not be registered as callback because signature mismatch.
– VTT
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
1
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I created 2 class.class KeyLogger
and class cHookProc
first i create cHookProc
in KeyLogger
class.
then i get address of LRESULT CALLBACK cHookProc::HookProc
by unsigned int cHookProc::getCallBackAddr()
is it logical and works fine always?
this is KeyLogger.h
#pragma once
#include <time.h>
#include <windows.h>
class cHookProc
public:
LRESULT CALLBACK HookProc(int code, WPARAM wp, LPARAM lp);
unsigned int getCallBackAddr();
private:
;
class KeyLogger
public:
KeyLogger();
~KeyLogger();
private:
cHookProc *cHkProc;
;
and this is KeyLogger.cpp
#include "KeyLogger.h"
KeyLogger::KeyLogger()
cHkProc = new cHookProc;
int i = cHkProc->getCallBackAddr();
HOOKPROC hprc = (HOOKPROC)i;
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, hprc, NULL, NULL);
KeyLogger::~KeyLogger()
delete cHkProc;
LRESULT CALLBACK cHookProc::HookProc(int code, WPARAM wp, LPARAM lp)
if (code == 0)
.
.
.
return CallNextHookEx(NULL, code, wp, lp);
unsigned int cHookProc::getCallBackAddr()
LRESULT(__stdcall cHookProc::*ptrtofn)(int, WPARAM, LPARAM) = &cHookProc::HookProc;
unsigned int *i;
i = (unsigned int*)&ptrtofn;
return *i;
c++ callback static
I created 2 class.class KeyLogger
and class cHookProc
first i create cHookProc
in KeyLogger
class.
then i get address of LRESULT CALLBACK cHookProc::HookProc
by unsigned int cHookProc::getCallBackAddr()
is it logical and works fine always?
this is KeyLogger.h
#pragma once
#include <time.h>
#include <windows.h>
class cHookProc
public:
LRESULT CALLBACK HookProc(int code, WPARAM wp, LPARAM lp);
unsigned int getCallBackAddr();
private:
;
class KeyLogger
public:
KeyLogger();
~KeyLogger();
private:
cHookProc *cHkProc;
;
and this is KeyLogger.cpp
#include "KeyLogger.h"
KeyLogger::KeyLogger()
cHkProc = new cHookProc;
int i = cHkProc->getCallBackAddr();
HOOKPROC hprc = (HOOKPROC)i;
HHOOK keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, hprc, NULL, NULL);
KeyLogger::~KeyLogger()
delete cHkProc;
LRESULT CALLBACK cHookProc::HookProc(int code, WPARAM wp, LPARAM lp)
if (code == 0)
.
.
.
return CallNextHookEx(NULL, code, wp, lp);
unsigned int cHookProc::getCallBackAddr()
LRESULT(__stdcall cHookProc::*ptrtofn)(int, WPARAM, LPARAM) = &cHookProc::HookProc;
unsigned int *i;
i = (unsigned int*)&ptrtofn;
return *i;
c++ callback static
c++ callback static
edited yesterday
asked yesterday
Amir
802817
802817
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
4
Putting address intoint
is not going to work. Non-static member can not be registered as callback because signature mismatch.
– VTT
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
1
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday
add a comment |
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
4
Putting address intoint
is not going to work. Non-static member can not be registered as callback because signature mismatch.
– VTT
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
1
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
4
4
Putting address into
int
is not going to work. Non-static member can not be registered as callback because signature mismatch.– VTT
yesterday
Putting address into
int
is not going to work. Non-static member can not be registered as callback because signature mismatch.– VTT
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
1
1
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
If your function doesn't use any method from the class, then it might work, but it's just random chance, and it will break if you change the function and add references to the object. Make it a static method in that case.
A callback on a method should be wrapped with a reference to the object on which the method is called.
Also use a unique pointer for cHkProc
.
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If your function doesn't use any method from the class, then it might work, but it's just random chance, and it will break if you change the function and add references to the object. Make it a static method in that case.
A callback on a method should be wrapped with a reference to the object on which the method is called.
Also use a unique pointer for cHkProc
.
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
add a comment |
up vote
0
down vote
If your function doesn't use any method from the class, then it might work, but it's just random chance, and it will break if you change the function and add references to the object. Make it a static method in that case.
A callback on a method should be wrapped with a reference to the object on which the method is called.
Also use a unique pointer for cHkProc
.
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
If your function doesn't use any method from the class, then it might work, but it's just random chance, and it will break if you change the function and add references to the object. Make it a static method in that case.
A callback on a method should be wrapped with a reference to the object on which the method is called.
Also use a unique pointer for cHkProc
.
If your function doesn't use any method from the class, then it might work, but it's just random chance, and it will break if you change the function and add references to the object. Make it a static method in that case.
A callback on a method should be wrapped with a reference to the object on which the method is called.
Also use a unique pointer for cHkProc
.
answered yesterday
Matthieu Brucher
4,6021127
4,6021127
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
add a comment |
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
I register non static method after creating class. doesn't it make my code safe?
– Amir
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
If your callback is not called on a method, then it's wrong, plain wrong.
– Matthieu Brucher
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205221%2fis-it-logical-to-register-non-static-method-as-callback-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You can't, really, since non-static member functions needs an object to be called on. If you don't have that then you could make a static member function wrapper, if that function could also access an instance (object) of the class.
– Some programmer dude
yesterday
4
Putting address into
int
is not going to work. Non-static member can not be registered as callback because signature mismatch.– VTT
yesterday
@VTT after searching a lot i found out your answer is exactly correct and my code is not correct. thanks.
– Amir
yesterday
1
If the code that is using the callback has access to an instance of the class (or an instance of a derived class) it can make sense to register a member function of that class as a callback. But that is only true if the code using the callback will associate the member function with the class instance and call it correctly. That is a very narrow set of conditions where it is applicable. Most callback schemes aim to be more general than that (e.g. useful in more contexts than a single class or its set of derived classes).
– Peter
yesterday