[RESOLVED] how can i call LRESULT CALLBACK WindowProc() function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

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

  1. #1
    Join Date
    Apr 2009
    Posts
    734

    [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
    Wallisellen (ZH), Switzerland
    Posts
    17,611

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

    What for?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    734

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

    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 03:41 PM.

  5. #5
    Join Date
    Apr 2009
    Posts
    734

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

    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
    Join Date
    Apr 2009
    Posts
    734

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

    Quote Originally Posted by Paul McKenzie View Post
    Did you read the documentation carefully?

    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
    Code:
    #include <iostream>#include <windows.h>
    #include <conio.h>
    
    
    using namespace std;
    
    
    LRESULT CALLBACK ConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        cout<< "hi";
        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()
    {
        cout << GetConsoleWindow() << endl;//the value is diferent from 0(zero)\NULL
        cout << SetWindowLong(GetConsoleWindow(), GWL_WNDPROC, (LONG)ConsoleWndProc);//here i recive 0(zero)
        getch();
        return 0;
    }
    i read, but i don't understand what they speak about the GWL_WNDPROC const

  8. #8
    Join Date
    Apr 1999
    Posts
    27,446

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

    Quote Originally Posted by Cambalinho View Post
    i read, but i don't understand
    You didn't read.

    Once again:
    you should clear the last error information by calling SetLastError with 0 before calling SetWindowLong.
    Your code is not doing this.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 2009
    Posts
    734

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

    Quote Originally Posted by Paul McKenzie View Post
    You didn't read.

    Once again:
    Your code is not doing this.

    Regards,

    Paul McKenzie
    Code:
    int main()
    {
        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();
        getch();
        return 0;
    }
    i get the same results

  10. #10
    Join Date
    Apr 1999
    Posts
    27,446

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

    Quote Originally Posted by Cambalinho View Post
    i read, but i don't understand what they speak about the GWL_WNDPROC const
    Why are you trying to override the console window's window procedure?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 2009
    Posts
    734

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

    Quote Originally Posted by Paul McKenzie View Post
    Why are you trying to override the console window's window procedure?

    Regards,

    Paul McKenzie
    what i need is getting the messages nothing more

  12. #12
    Join Date
    Apr 1999
    Posts
    27,446

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

    Quote Originally Posted by Cambalinho View Post
    what i need is getting the messages nothing more
    Even if the OS allowed you to overide the window procedure for capturing messages, you are not doing it correctly. Your code just takes over all control of the console window, thus the console window never has a chance to behave properly because it is not receiving its messages.

    You're supposed to override the window, and then call the Console window's window procedure after you receive the message, not DefWindowProc. Otherwise, the console window will not operate correctly. And all of this is if you're allowed to override the console window's procedure.

    You need to take a step back and learn about Windows' subclassing. You don't just replace the Window Proc, you must keep the original one so that the original window's behaviour is done.

    Picture it this way -- you're the console window, and you receive messages and act OK. Then I am the custom window procedure and steps in front of you to receive messages. I get the message, and instead of handing the message to you (the console window) so that you can operate correctly, I throw it away. You never get the messages, and you're just sitting there doing nothing. That is exactly what your code snippet will do.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 3rd, 2013 at 04:49 PM.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

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

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

  14. #14
    Join Date
    Apr 2009
    Posts
    734

    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

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,611

    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

Page 1 of 3 123 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center