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;










share|improve this question























  • 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














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;










share|improve this question























  • 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












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;










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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












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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)