CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 33

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?

    in these case, i'm using a console aplication, so i don't have the windows class. then how can i call the LRESULT CALLBACK WindowProc() function?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    What for?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by VictorN View Post
    What for?
    is 1 thing that i need
    i have these code:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    LRESULT CALLBACK conProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_DESTROY:
                MessageBox(NULL, "The window was destroyed", "bye", MB_OK);
                break;
            case WM_CREATE:
                MessageBox(NULL, "The window was created", "hello", MB_OK);
        }
        return DefWindowProc(hWnd, msg, wParam, lParam );
    }
    
    int main()
    {
        SetWindowLong(GetConsoleWindow(), GWL_WNDPROC, (LONG)conProc);
        std::cout << GetLastError();
        getch();
        return 0;
    }
    but i get 1 error "Error code: 5 - Access is denied" on GetLastError()
    can you advice me?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    is 1 thing that i need
    i have these code:
    Why are you not checking the return value of GetConsoleWindow()?

    Why are you not checking the SetWindowLong() return code? You are not supposed to call GetLastError() if the function is successful.

    Please read the documentation about the return code:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 3rd, 2013 at 02:41 PM.

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Paul McKenzie View Post
    Why are you not checking the return value of GetConsoleWindow()?

    Why are you not checking the SetWindowLong() return code? You are not supposed to call GetLastError() if the function is successful.

    Please read the documentation about the return code:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Regards,

    Paul McKenzie
    the give me a value diferent than NULL: 0x1f082e
    and the SetWindowLong() give 0(zero). meaning that something isn't correct

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    the give me a value diferent than NULL: 0x1f082e
    and the SetWindowLong() give 0(zero). meaning that something isn't correct
    Did you read the documentation carefully?
    If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure.

    To deal with this, you should clear the last error information by calling SetLastError with 0 before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero
    You should post your current code, the code that checks for errors and return codes in the correct way. Your first code you posted is not correct.

    Regards,

    Paul McKenzie

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    What "messages" would you like to get for your console window?
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by VictorN View Post
    What "messages" would you like to get for your console window?
    mouse, keyboard, paint, move, what is possible
    whats why i wanted use that function for i get more easy the messages

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    mouse, keyboard, paint, move, what is possible
    Well, I never worked with console windows... Therefore I ask you: are you sure these messages come to console?
    Did you check it (with Spy++ or some other tool?)
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by VictorN View Post
    Well, I never worked with console windows... Therefore I ask you: are you sure these messages come to console?
    Did you check it (with Spy++ or some other tool?)
    i don't know use the Spy++ but i have it

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Cambalinho View Post
    mouse, keyboard, paint, move, what is possible
    whats why i wanted use that function for i get more easy the messages
    But as I stated, you must take those messages you intercepted and give them to the original console window procedure when you are done with them. You can't just throw the messages away as your code is doing now.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by Paul McKenzie View Post
    But as I stated, you must take those messages you intercepted and give them to the original console window procedure when you are done with them. You can't just throw the messages away as your code is doing now.

    Regards,

    Paul McKenzie
    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    #include <string>
    using namespace std;
    
    LRESULT CALLBACK ConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {    
        switch(msg)
        {
            case WM_DESTROY:
                MessageBox(NULL, "The window was destroyed", "bye", MB_OK);
                break;
            case WM_CREATE:
                MessageBox(NULL, "The window was created", "hello", MB_OK);
        }
        return CallWindowProc((WNDPROC ) ConsoleWndProc, hWnd, msg, wParam, lParam);
    }
    
    int main()
    {
        string a;
        cout << GetConsoleWindow() << endl;//the value is diferent from 0(zero)\NULL
        SetLastError(0);
        cout << SetWindowLong(GetConsoleWindow(), GWL_WNDPROC, (LONG)ConsoleWndProc);//here i recive 0(zero)
        cout << GetLastError();
        cin >> a;
        return 0;
    }
    but how can i do it, if the SetWindowLong() function is give problems?
    i have sure about the GetConsoleWindow(), because i get the HDC from it and works fine. i have used the HDC

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Then learn how to use it and do use!
    Victor Nijegorodov

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    but i get 1 error "Error code: 5 - Access is denied" on GetLastError()
    can you advice me?
    Yes - you can't do this with consoles! Windows won't let you subclass a console window.

    What exactly are you trying to achieve?
    Last edited by 2kaud; September 3rd, 2013 at 04:30 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i call LRESULT CALLBACK WindowProc() function?

    Quote Originally Posted by 2kaud View Post
    Yes - you can't do this with consoles! Windows won't let you subclass a console window.

    What exactly are you trying to achieve?
    just get the messages for add events in my class
    i'm trying another code but i don't know how works

    Code:
    MSG msg;
        BOOL bRet;
        //...
        while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
        { 
            if (bRet == -1)
            {
                // handle the error and possibly exit
            }
            else
            {
                TranslateMessage(&msg); 
                DispatchMessage(&msg); 
            }
        }
    i belive these is the other way, but isn't working
    can you advice?
    i only need get the messages from console

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured