GetMessage Win API function throws exception (probably due to a wrong callback)












-1















My problem I think that is due to the fact that I try to bind a class member function (not static) as a callback to the SetWindowsHookEx function. I've tried debugging but I was unable to get any other useful info except the callback binding which I'm not aware of any other way of doing so.
Therefore, at the first call of the GetMessage function I receive the following exception: An unhandled exception was encountered during a user callback. .



My question is, if the cause of the exception is the way I'm binding the HookLowLevel::KeyboardProc function so I can pass it as a callback, what's the right way of doing so? If not, what do you think may be the cause of the exception being thrown?



The function inside which I'm doing the callback binding and where the GetMessage function that throws the exception is found:



void HookLowLevel::attachHook()
{
printf("Hooking the keyboardn");
auto callback = std::bind(&HookLowLevel::KeyboardProc, this);
hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)(&callback), 0, 0);
printf("%Xn", hook_);
MSG msg{ 0 };
try
{
while (GetMessage(&msg, NULL, 0, 0) != 0 && !windowChanged_)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
catch (const std::runtime_error& re)
{
std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what();
}
}


And this is how the definition of the KeyboardProc looks like:



LRESULT CALLBACK HookLowLevel::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)










share|improve this question























  • Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

    – David Heffernan
    Nov 15 '18 at 10:05
















-1















My problem I think that is due to the fact that I try to bind a class member function (not static) as a callback to the SetWindowsHookEx function. I've tried debugging but I was unable to get any other useful info except the callback binding which I'm not aware of any other way of doing so.
Therefore, at the first call of the GetMessage function I receive the following exception: An unhandled exception was encountered during a user callback. .



My question is, if the cause of the exception is the way I'm binding the HookLowLevel::KeyboardProc function so I can pass it as a callback, what's the right way of doing so? If not, what do you think may be the cause of the exception being thrown?



The function inside which I'm doing the callback binding and where the GetMessage function that throws the exception is found:



void HookLowLevel::attachHook()
{
printf("Hooking the keyboardn");
auto callback = std::bind(&HookLowLevel::KeyboardProc, this);
hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)(&callback), 0, 0);
printf("%Xn", hook_);
MSG msg{ 0 };
try
{
while (GetMessage(&msg, NULL, 0, 0) != 0 && !windowChanged_)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
catch (const std::runtime_error& re)
{
std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what();
}
}


And this is how the definition of the KeyboardProc looks like:



LRESULT CALLBACK HookLowLevel::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)










share|improve this question























  • Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

    – David Heffernan
    Nov 15 '18 at 10:05














-1












-1








-1








My problem I think that is due to the fact that I try to bind a class member function (not static) as a callback to the SetWindowsHookEx function. I've tried debugging but I was unable to get any other useful info except the callback binding which I'm not aware of any other way of doing so.
Therefore, at the first call of the GetMessage function I receive the following exception: An unhandled exception was encountered during a user callback. .



My question is, if the cause of the exception is the way I'm binding the HookLowLevel::KeyboardProc function so I can pass it as a callback, what's the right way of doing so? If not, what do you think may be the cause of the exception being thrown?



The function inside which I'm doing the callback binding and where the GetMessage function that throws the exception is found:



void HookLowLevel::attachHook()
{
printf("Hooking the keyboardn");
auto callback = std::bind(&HookLowLevel::KeyboardProc, this);
hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)(&callback), 0, 0);
printf("%Xn", hook_);
MSG msg{ 0 };
try
{
while (GetMessage(&msg, NULL, 0, 0) != 0 && !windowChanged_)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
catch (const std::runtime_error& re)
{
std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what();
}
}


And this is how the definition of the KeyboardProc looks like:



LRESULT CALLBACK HookLowLevel::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)










share|improve this question














My problem I think that is due to the fact that I try to bind a class member function (not static) as a callback to the SetWindowsHookEx function. I've tried debugging but I was unable to get any other useful info except the callback binding which I'm not aware of any other way of doing so.
Therefore, at the first call of the GetMessage function I receive the following exception: An unhandled exception was encountered during a user callback. .



My question is, if the cause of the exception is the way I'm binding the HookLowLevel::KeyboardProc function so I can pass it as a callback, what's the right way of doing so? If not, what do you think may be the cause of the exception being thrown?



The function inside which I'm doing the callback binding and where the GetMessage function that throws the exception is found:



void HookLowLevel::attachHook()
{
printf("Hooking the keyboardn");
auto callback = std::bind(&HookLowLevel::KeyboardProc, this);
hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)(&callback), 0, 0);
printf("%Xn", hook_);
MSG msg{ 0 };
try
{
while (GetMessage(&msg, NULL, 0, 0) != 0 && !windowChanged_)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
catch (const std::runtime_error& re)
{
std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what();
}
}


And this is how the definition of the KeyboardProc looks like:



LRESULT CALLBACK HookLowLevel::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)







c++ winapi boost






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 9:34









Gor BlurGor Blur

33




33













  • Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

    – David Heffernan
    Nov 15 '18 at 10:05



















  • Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

    – David Heffernan
    Nov 15 '18 at 10:05

















Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

– David Heffernan
Nov 15 '18 at 10:05





Remove the cast to HOOKPROC, and so your function call becomes SetWindowsHookEx(WH_KEYBOARD_LL, callback, 0, 0); Does this compile? If it does not, then callback is not of the correct signature. No amount of casting can change that. The cast to HOOKPROC in your code would merely be a means for you to tell a big fat lie to the compiler. Those lies seldom end well.

– David Heffernan
Nov 15 '18 at 10:05












1 Answer
1






active

oldest

votes


















2














The Windows API is strictly exposed as a C interface. You cannot pass pointers to non-static class members as callbacks to any Windows API (or function objects as returned by std::bind). The compiler told you about this already, which probably resulted in you silencing the compiler by adding an (invalid) cast.



Remove the cast and the compiler will let you know, whether you passed a function pointer with the correct signature. You can also safely remove your try/catch handlers. The Windows API will not ever throw a C++ exception (see first paragraph).






share|improve this answer

























    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',
    autoActivateHeartbeat: false,
    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%2f53316321%2fgetmessage-win-api-function-throws-exception-probably-due-to-a-wrong-callback%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The Windows API is strictly exposed as a C interface. You cannot pass pointers to non-static class members as callbacks to any Windows API (or function objects as returned by std::bind). The compiler told you about this already, which probably resulted in you silencing the compiler by adding an (invalid) cast.



    Remove the cast and the compiler will let you know, whether you passed a function pointer with the correct signature. You can also safely remove your try/catch handlers. The Windows API will not ever throw a C++ exception (see first paragraph).






    share|improve this answer






























      2














      The Windows API is strictly exposed as a C interface. You cannot pass pointers to non-static class members as callbacks to any Windows API (or function objects as returned by std::bind). The compiler told you about this already, which probably resulted in you silencing the compiler by adding an (invalid) cast.



      Remove the cast and the compiler will let you know, whether you passed a function pointer with the correct signature. You can also safely remove your try/catch handlers. The Windows API will not ever throw a C++ exception (see first paragraph).






      share|improve this answer




























        2












        2








        2







        The Windows API is strictly exposed as a C interface. You cannot pass pointers to non-static class members as callbacks to any Windows API (or function objects as returned by std::bind). The compiler told you about this already, which probably resulted in you silencing the compiler by adding an (invalid) cast.



        Remove the cast and the compiler will let you know, whether you passed a function pointer with the correct signature. You can also safely remove your try/catch handlers. The Windows API will not ever throw a C++ exception (see first paragraph).






        share|improve this answer















        The Windows API is strictly exposed as a C interface. You cannot pass pointers to non-static class members as callbacks to any Windows API (or function objects as returned by std::bind). The compiler told you about this already, which probably resulted in you silencing the compiler by adding an (invalid) cast.



        Remove the cast and the compiler will let you know, whether you passed a function pointer with the correct signature. You can also safely remove your try/catch handlers. The Windows API will not ever throw a C++ exception (see first paragraph).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 10:15

























        answered Nov 15 '18 at 10:05









        IInspectableIInspectable

        26.2k54396




        26.2k54396
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316321%2fgetmessage-win-api-function-throws-exception-probably-due-to-a-wrong-callback%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine