Program not showing up
When I compile this program, a window doesn't open, instead, a message box that says, "Click box.exe has stopped working", I don't know why this is happening.
Here is the WinMain function
Thanks in advanced
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
LPSTR szClassName = "BoxApp";
//Register the window class
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = 0;
WinClass.lpfnWndProc = WndProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = g_hInstance;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WinClass.lpszClassName = szClassName;
WinClass.lpszMenuName = NULL;
g_hWindow = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, "The Box App",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL, NULL, g_hInstance, NULL);
while(1 == 1)
InvalidateRect(g_hWindow, NULL, FALSE);
if(GetMessage(&msg, NULL, 0, 0) > 0)
Add WS_VISIBLE to your window styles or use ShowWindow() with SW_SHOWNORMAL to make it visible.
The show parameter of the command line only works if you have it set in a shortcut, otherwise it's 0.
It's there, you just can't see it.
if (icmdshow > 0)
Nope, the problem still occurs
Ok I'm assuming you have a window message procedure defined somewhere else in the code?
I don't see a call to RegisterClass() which is essential before anything is going to happen. You can't even successfully create a window without it.
Plus I rather suspect you're going to run into problems with your dispatcher loop... Invalidating the window at that point is going to make it constantly redraw itself, causing quite the light show... plus there's no exit provision for when WM_CLOSE or WM_QUIT are processed, leaving you with no exit from the program.
At minimum you need a window class, one window, a message dispatcher and a message tosser before a windows program is going to run. The example below comes from theForger's Win32 API Tutorial Less than this and you get nothing.
const char g_szClassName = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hwnd, msg, wParam, lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
// Step 2: Creating the Window
hwnd = CreateWindowEx(
"The title of my window",
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
Ok thanks, that was what was causing the error
Edit: Ops, I'm sorry for this, the window show up, but it still says "Click box.exe has stopped working". I didn't put that in the response earlier because it was a few seconds before the error showed up, and by then I needed to catch the bus.