locked
CreateWindow returns NULL for non-Admin Users but works fine for Admin Users RRS feed

  • Question

  • CreateWindow returns NULL for non-Admin Users but works fine for Admin Users, any help is appreciated.

    CreateWindow with WS_VISIBLE | WS_CHILD | WS_TABSTOP  window styles is returning NULL.

    Wednesday, May 8, 2019 2:01 PM

All replies

  • CreateWindow returns NULL for non-Admin Users but works fine for Admin Users, any help is appreciated.

    CreateWindow with WS_VISIBLE | WS_CHILD | WS_TABSTOP  window styles is returning NULL.

    Immediately after CreateWindow fails call GetLastError().  What error code is returned?

    Provide a complete code snippet that shows the call to CreateWindow.  Also show the code to register the window class if not using a system provided one.

    Wednesday, May 8, 2019 2:07 PM
  • Also, if you can, show what is in the window's WM_CREATE and WM_NCCREATE handlers if possible. CreateWindow can return NULL if either of these indicate failure.

    Because of how selective it is, it is possible that these can contain an attempt to access some admin resource, and fails when you don't have admin rights.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Wednesday, May 8, 2019 3:03 PM
  • WNDCLASS wc;

    wc.style = CS_BYTEALIGNCLIENT | CS_GLOBALCLASS | CS_DBLCLKS;
            wc.lpfnWndProc = (WNDPROC) WindowProc;
            wc.cbClsExtra = 0;
            wc.cbWndExtra = 4; 
            wc.hInstance = hInstance;
            wc.hIcon = NULL;
            wc.hCursor = NULL;
            wc.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
            wc.lpszMenuName = NULL;
            wc.lpszClassName = (LPCSTR)SWPRCLST_CLASSNAME;


    return RegisterClass(&wc);


    .
    .
    .

    HWND hWnd = CreateWindow((LPCSTR)"SWProcListCtlClass", (LPCSTR)"C Nodename", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x0002,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            pWnd,       // Parent window    
            NULL,       // Menu
            hInstance,  // Instance handle
            NULL        // Additional application data
            );

       if (!hWnd)
       {
          return FALSE;
       }

    The above is not full code but snippets that can give an idea of how the code looks like.

    GetLastError returned 183 ie., ERROR_ALREADY_EXISTS

    Wednesday, May 8, 2019 6:18 PM
  • Thank you for your time, added code snippets above.  Please review and advise.
    Wednesday, May 8, 2019 6:19 PM
  • Thank you for your time, added code snippets above.  Please review and advise.
    Wednesday, May 8, 2019 6:19 PM
  • WNDCLASS wc;

    wc.style = CS_BYTEALIGNCLIENT | CS_GLOBALCLASS | CS_DBLCLKS;
            wc.lpfnWndProc = (WNDPROC) WindowProc;
            wc.cbClsExtra = 0;
            wc.cbWndExtra = 4; 
            wc.hInstance = hInstance;
            wc.hIcon = NULL;
            wc.hCursor = NULL;
            wc.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
            wc.lpszMenuName = NULL;
            wc.lpszClassName = (LPCSTR)SWPRCLST_CLASSNAME;


    return RegisterClass(&wc);


    .
    .
    .

    HWND hWnd = CreateWindow((LPCSTR)"SWProcListCtlClass", (LPCSTR)"C Nodename", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x0002,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            pWnd,       // Parent window    
            NULL,       // Menu
            hInstance,  // Instance handle
            NULL        // Additional application data
            );

       if (!hWnd)
       {
          return FALSE;
       }

    The above is not full code but snippets that can give an idea of how the code looks like.

    GetLastError returned 183 ie., ERROR_ALREADY_EXISTS

    This code does not actually show when GetLastError is called.  For all we know, it is returning the error code set by some function other than CreateWindow.

    There is no window style in the SDK headers that corresponds to 0x0002.

    Are you building for UNICODE or MBCS?

    Does your application create the window that is being set as the parent window?

    What about the window procedure handling for WM_CREATE and WM_NCCREATE that Darran Rowe requested?

    Does your application have a manifest?

    Finally, you should be able to produce a minimal demo that reproduces the problem that you can make available.



    • Edited by RLWA32 Wednesday, May 8, 2019 7:29 PM
    Wednesday, May 8, 2019 7:24 PM
  • Hi,

    According to the information you have provided, there are instructions in the MSDN document.

    If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183).

    If the specified file exists, the function succeeds and the last-error code is set toERROR_ALREADY_EXISTS (183).

    So you need to make sure that there are any function calls like CreateFile in your code.

    In addition, you need to make sure that the window you created successfully returns the handle.

    Best regards,

    Strive


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 9, 2019 2:39 AM
  • You should never have to cast string constants.  If you get an error, then you may have a Unicode/ANSI problem, as RLWA32 implied.

    Are you calling RegisterClass multiple times for the same class?  That would certainly return ERROR_ALREADY_EXISTS.  You only have to register the class once, no matter how many windows of that kind you create.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 10, 2019 1:22 AM
  • Are you calling RegisterClass multiple times for the same class?  That would certainly return ERROR_ALREADY_EXISTS.  You only have to register the class once, no matter how many windows of that kind you create

    Registering the same class twice returns ERROR_CLASS_ALREADY_EXISTS (1410).

    I suspect that the error code mentioned by the OP is probably unrelated to creation of a window.

    Friday, May 10, 2019 1:38 AM