Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:52:00+00:00 2026-06-05T02:52:00+00:00

I want to create a window using winAPI: int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,

  • 0

I want to create a window using winAPI:

    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,
        int nShowCmd)
 {
WNDCLASSEX wClass;
HWND hWnd;


wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName=TEXT("Window Class");
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW|CS_DROPSHADOW ;

if(!RegisterClassEx(&wClass))
{
    int nResult=GetLastError();
    MessageBox(NULL,
        TEXT("Window class creation failed"),
        TEXT("Window Class Failed"),
        MB_ICONERROR);
}

hWnd=CreateWindowEx(NULL,
        TEXT("Window Class"),
        TEXT("My Process Explorer"),
        WS_OVERLAPPEDWINDOW,
        200,
        20,
        800,
        630,
        NULL,
        NULL,
        hInst,
        NULL);
  }

but I get Access Violation Error.
Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:52:01+00:00Added an answer on June 5, 2026 at 2:52 am

    Tenfour already points this out in the comments, but it bears repeating again about 6 or 8 more times: Never cast the pointer to your window procedure function to WNDPROC. In fact, don’t cast anything unless you know exactly why you need to cast it. Your answer to his comment asking why you’re casting is telling:

    For not getting Warnings!

    In fact, that’s precisely the problem! All a cast does is tell the compiler “Shut up, I know what I’m doing!” You aren’t getting warnings anymore because you pressed the “override” button. But those warnings were there for a reason—they were trying to tell you that your code was broken. The compiler is there to help you. You won’t get very far by ignoring it, or worse, flipping the override bit and telling it to shut up. As the prolific Win32 blogger Raymond Chen puts it, A function pointer cast is a bug waiting to happen. (This is such a common mistake that he also writes about it here and here.)

    The most common reason that people will feel compelled to cast function pointers is because the compiler is trying to warn them that their function signature is incorrect. The correct signature for a window procedure function is documented here on MSDN and looks like this:

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    

    You can, of course, name the function anything you choose. But the number of parameters, their types, and the return value all need to match that signature.

    If they don’t, the compiler will issue an error. If you flip the override bit and cast away the error, then the code will fail at runtime, which is exactly the symptoms you’re experiencing now. The CreateWindowEx function is saying “hey, whoa, I don’t recognize that window procedure you tried to pass me!”

    When I write a valid window procedure stub, remove the spurious cast, and then run your code, it works fine with no errors. For example:

    LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
                       LPSTR lpCmdLine, int nShowCmd)
    {
        WNDCLASSEX wClass;
        HWND hWnd;
    
        wClass.cbClsExtra=NULL;
        wClass.cbSize=sizeof(WNDCLASSEX);
        wClass.cbWndExtra=NULL;
        wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
        wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
        wClass.hIcon=NULL;
        wClass.hIconSm=NULL;
        wClass.hInstance=hInst;
        wClass.lpfnWndProc=WinProc;
        wClass.lpszClassName=TEXT("Window Class");
        wClass.lpszMenuName=NULL;
        wClass.style=CS_HREDRAW|CS_VREDRAW|CS_DROPSHADOW ;
    
        if(!RegisterClassEx(&wClass))
        {
            int nResult=GetLastError();
            MessageBox(NULL,
                TEXT("Window class creation failed"),
                TEXT("Window Class Failed"),
                MB_ICONERROR);
        }
    
        hWnd=CreateWindowEx(NULL,
                TEXT("Window Class"),
                TEXT("My Process Explorer"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                200,
                20,
                800,
                630,
                NULL,
                NULL,
                hInst,
                NULL);
    }
    

    However, a couple of other things to note:

    • You’re using the ANSI entry point, WinMain, which is incorrect since in the year 2012, you should definitely be compiling for Unicode. You’re already using the TEXT() macro to ensure that your string literals are wide strings when UNICODE is defined, but you need to do the same thing with your entry point function. Change the definition to:

       int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                            LPTSTR lpCmdLine, int nCmdShow);
      
    • You’re correctly checking the return value of the RegisterClassEx function, and if it fails, calling GetLastError as a debugging aid. You should be doing the same thing with the CreateWindowEx function. The documentation for that function does indicate that it sets the last error:

      If the function fails, the return value is NULL. To get extended error information, call GetLastError.

      So you can change your code to:

      hWnd=CreateWindowEx(NULL,
              TEXT("Window Class"),
              TEXT("My Process Explorer"),
              WS_OVERLAPPEDWINDOW,
              200,
              20,
              800,
              630,
              NULL,
              NULL,
              hInst,
              NULL);
      if (!hWnd)
      {
         int nResult = GetLastError();
         MessageBox(NULL,
              TEXT("Window creation failed"),
              TEXT("Window Failed"),
              MB_ICONERROR);
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use CGL or NSOpenGL to create an OpenGL window without using
I want to create a very simple window manager for Ubuntu using Ruby. Where
How to create the pop up window using LWUIT? I want to show an
I want to create a popup window using wxPython that acts like a bash
I want to create a window where the user can go out of the
I want to create an application which main window has canvas (or something where
I want to create a button that can show a window to show details
I want to create a separate thread that runs its own window. Frankly, the
I want to create a JToolBar without adding it into any JFrame window. If
Hi i want to create a generic style for pin button. <Window x:Class=TooglePinButtonStyle.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.