Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetLastError problem

GetLastError problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasedebuggingtutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rmnowick
    wrote on last edited by
    #1

    I have the following code that I am executing:

    DWORD last_err;
    WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    //wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = "MainMenu";
    wc.lpszClassName = "MainWindowClass";

    if (!RegisterClass(&wc))
    {
    	last\_err = GetLastError();
    }
    

    The RegisterClass call fails and when I run the code in debug and last_err gets set it has a value of 0. I'm expecting it to have a value that designates just what the error was. I got this code fom the following example:

    int APIENTRY WinMain(
    HINSTANCE hinstance, // handle to current instance
    HINSTANCE hinstPrev, // handle to previous instance
    LPSTR lpCmdLine, // address of command-line string
    int nCmdShow) // show-window type
    {
    WNDCLASS wc;

    // Register the main window class. 
    wc.style = CS\_HREDRAW | CS\_VREDRAW; 
    wc.lpfnWndProc = (WNDPROC) MainWndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hinstance; 
    wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); 
    wc.hCursor = LoadCursor(NULL, IDC\_ARROW); 
    wc.hbrBackground = GetStockObject(WHITE\_BRUSH); 
    wc.lpszMenuName =  "MainMenu"; 
    wc.lpszClassName = "MainWindowClass"; 
    
    if (!RegisterClass(&wc)) 
       return FALSE; 
    
    // 
    // Process other messages. 
    // 
    

    }

    This comes from the help index under "Using Window Procedures". Robert

    D N M 3 Replies Last reply
    0
    • R rmnowick

      I have the following code that I am executing:

      DWORD last_err;
      WNDCLASS wc;

      wc.style = CS_HREDRAW | CS_VREDRAW;
      wc.lpfnWndProc = (WNDPROC) WindowProc;
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      //wc.hbrBackground = GetStockObject(WHITE_BRUSH);
      wc.lpszMenuName = "MainMenu";
      wc.lpszClassName = "MainWindowClass";

      if (!RegisterClass(&wc))
      {
      	last\_err = GetLastError();
      }
      

      The RegisterClass call fails and when I run the code in debug and last_err gets set it has a value of 0. I'm expecting it to have a value that designates just what the error was. I got this code fom the following example:

      int APIENTRY WinMain(
      HINSTANCE hinstance, // handle to current instance
      HINSTANCE hinstPrev, // handle to previous instance
      LPSTR lpCmdLine, // address of command-line string
      int nCmdShow) // show-window type
      {
      WNDCLASS wc;

      // Register the main window class. 
      wc.style = CS\_HREDRAW | CS\_VREDRAW; 
      wc.lpfnWndProc = (WNDPROC) MainWndProc; 
      wc.cbClsExtra = 0; 
      wc.cbWndExtra = 0; 
      wc.hInstance = hinstance; 
      wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); 
      wc.hCursor = LoadCursor(NULL, IDC\_ARROW); 
      wc.hbrBackground = GetStockObject(WHITE\_BRUSH); 
      wc.lpszMenuName =  "MainMenu"; 
      wc.lpszClassName = "MainWindowClass"; 
      
      if (!RegisterClass(&wc)) 
         return FALSE; 
      
      // 
      // Process other messages. 
      // 
      

      }

      This comes from the help index under "Using Window Procedures". Robert

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Do you get the same results when using RegisterClassEx()?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      R 1 Reply Last reply
      0
      • R rmnowick

        I have the following code that I am executing:

        DWORD last_err;
        WNDCLASS wc;

        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = (WNDPROC) WindowProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        //wc.hbrBackground = GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName = "MainMenu";
        wc.lpszClassName = "MainWindowClass";

        if (!RegisterClass(&wc))
        {
        	last\_err = GetLastError();
        }
        

        The RegisterClass call fails and when I run the code in debug and last_err gets set it has a value of 0. I'm expecting it to have a value that designates just what the error was. I got this code fom the following example:

        int APIENTRY WinMain(
        HINSTANCE hinstance, // handle to current instance
        HINSTANCE hinstPrev, // handle to previous instance
        LPSTR lpCmdLine, // address of command-line string
        int nCmdShow) // show-window type
        {
        WNDCLASS wc;

        // Register the main window class. 
        wc.style = CS\_HREDRAW | CS\_VREDRAW; 
        wc.lpfnWndProc = (WNDPROC) MainWndProc; 
        wc.cbClsExtra = 0; 
        wc.cbWndExtra = 0; 
        wc.hInstance = hinstance; 
        wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); 
        wc.hCursor = LoadCursor(NULL, IDC\_ARROW); 
        wc.hbrBackground = GetStockObject(WHITE\_BRUSH); 
        wc.lpszMenuName =  "MainMenu"; 
        wc.lpszClassName = "MainWindowClass"; 
        
        if (!RegisterClass(&wc)) 
           return FALSE; 
        
        // 
        // Process other messages. 
        // 
        

        }

        This comes from the help index under "Using Window Procedures". Robert

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #3

        Looks ok to me. Check that your hInstance param is correct and also your WindowProc and lpszMenuName. ---- lpszMenuName Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu. ---- I don't know why GetLastError() returns 0. If you are using MFC you may want to look at AfxRegisterWndClass() or AfxRegisterClass(). Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

        R 1 Reply Last reply
        0
        • D David Crow

          Do you get the same results when using RegisterClassEx()?


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          R Offline
          R Offline
          rmnowick
          wrote on last edited by
          #4

          Yes

          1 Reply Last reply
          0
          • N Neville Franks

            Looks ok to me. Check that your hInstance param is correct and also your WindowProc and lpszMenuName. ---- lpszMenuName Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu. ---- I don't know why GetLastError() returns 0. If you are using MFC you may want to look at AfxRegisterWndClass() or AfxRegisterClass(). Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

            R Offline
            R Offline
            rmnowick
            wrote on last edited by
            #5

            Neville, My hInstance param comes right from my main window creation, just like the example:

            int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR szCmdLine, int nShow)
            {

            hInstance = hInst;
            

            I changed the name of my WindowProc routine to MyWindowProc just so that it wasn't the default anymore. That routine is at the top of the module that contains the code in question:

            LRESULT CALLBACK MyWindowProc(HWND hwnd,
            UINT uMsg,
            WPARAM wParam,
            LPARAM lParam
            )
            {
            int dummy_var;

            dummy\_var = 1;
            return 1;
            

            }

            My lpszMenuName is NULL. My intent is to create a window that is invisible later on by calling CreateWindow for the purpose of processing messages only. The reason that I am calling RegisterClass is because I assumed that I needed to do this prior to getting the CreateWindow to work. BTW, that is also returning a 0 when it fails and I check GetLastError. Very strange. The help message that describes the kind of window that I want to create is here: From CreateWindow...

            hWndParent
            [in] Handle to the parent or owner window of the window being created. To create a child window or an owned window, supply a valid window handle. This parameter is optional for pop-up windows.
            Windows 2000/XP: To create a message-only window, supply HWND_MESSAGE or a handle to an existing message-only window.

            Message-Only Windows
            A message-only window enables you to send and receive messages. It is not visible, has no z-order, cannot be enumerated, and does not receive broadcast messages. The window simply dispatches messages.

            To create a message-only window, specify the HWND_MESSAGE constant or a handle to an existing message-only window in the hWndParent parameter of the CreateWindowEx function. You can also change an existing window to a message-only window by specifying HWND_MESSAGE in the hWndNewParent parameter of the SetParent function.

            To find message-only windows, specify HWND_MESSAGE in the hwndParent parameter of the FindWindowEx function. In addition, FindWindowEx searches message-only windows as well as top-level windows if both the hwndParent and hwndChildAfter parameters are NULL.

            As to the last reference to MFC, I am not using it. Robert

            1 Reply Last reply
            0
            • R rmnowick

              I have the following code that I am executing:

              DWORD last_err;
              WNDCLASS wc;

              wc.style = CS_HREDRAW | CS_VREDRAW;
              wc.lpfnWndProc = (WNDPROC) WindowProc;
              wc.cbClsExtra = 0;
              wc.cbWndExtra = 0;
              wc.hInstance = hInstance;
              wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
              wc.hCursor = LoadCursor(NULL, IDC_ARROW);
              //wc.hbrBackground = GetStockObject(WHITE_BRUSH);
              wc.lpszMenuName = "MainMenu";
              wc.lpszClassName = "MainWindowClass";

              if (!RegisterClass(&wc))
              {
              	last\_err = GetLastError();
              }
              

              The RegisterClass call fails and when I run the code in debug and last_err gets set it has a value of 0. I'm expecting it to have a value that designates just what the error was. I got this code fom the following example:

              int APIENTRY WinMain(
              HINSTANCE hinstance, // handle to current instance
              HINSTANCE hinstPrev, // handle to previous instance
              LPSTR lpCmdLine, // address of command-line string
              int nCmdShow) // show-window type
              {
              WNDCLASS wc;

              // Register the main window class. 
              wc.style = CS\_HREDRAW | CS\_VREDRAW; 
              wc.lpfnWndProc = (WNDPROC) MainWndProc; 
              wc.cbClsExtra = 0; 
              wc.cbWndExtra = 0; 
              wc.hInstance = hinstance; 
              wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); 
              wc.hCursor = LoadCursor(NULL, IDC\_ARROW); 
              wc.hbrBackground = GetStockObject(WHITE\_BRUSH); 
              wc.lpszMenuName =  "MainMenu"; 
              wc.lpszClassName = "MainWindowClass"; 
              
              if (!RegisterClass(&wc)) 
                 return FALSE; 
              
              // 
              // Process other messages. 
              // 
              

              }

              This comes from the help index under "Using Window Procedures". Robert

              M Offline
              M Offline
              Maksim Lepikhin
              wrote on last edited by
              #6

              In your code hbrBackground member is not initialized. It should be either NULL or valid HBRUSH. Maksim Lepikhin www.softforpros.com

              R 1 Reply Last reply
              0
              • M Maksim Lepikhin

                In your code hbrBackground member is not initialized. It should be either NULL or valid HBRUSH. Maksim Lepikhin www.softforpros.com

                R Offline
                R Offline
                rmnowick
                wrote on last edited by
                #7

                This was the problem. I had the following code commented out:

                //wc.hbrBackground = GetStockObject(WHITE_BRUSH);

                I believe I did this because it didn't compile. I had to add a cast to it:

                wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

                The code was originally from some sample code. Figures it wouldn't compile...

                // Register the main window class. 
                wc.style = CS\_HREDRAW | CS\_VREDRAW; 
                wc.lpfnWndProc = (WNDPROC) MainWndProc; 
                wc.cbClsExtra = 0; 
                wc.cbWndExtra = 0; 
                wc.hInstance = hinstance; 
                wc.hIcon = LoadIcon(NULL, IDI\_APPLICATION); 
                wc.hCursor = LoadCursor(NULL, IDC\_ARROW); 
                wc.hbrBackground = GetStockObject(WHITE\_BRUSH); 
                wc.lpszMenuName =  "MainMenu"; 
                wc.lpszClassName = "MainWindowClass"; 
                

                Thanks Maksim, Robert

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups