// JP opened flex table

Click to See Complete Forum and Search --> : Even Basic Win32API won't work?


cosmeo3000
July 10th, 2008, 07:08 AM
Well, I have started to learn WinAPI and for some reason this won't work. It compiles perfectly, but I always get my "Window Creation Failed" error.

Here's the Code:

#include <windows.h>

const char g_szClassName[] = "myClassName";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

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);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window registration failed", "Error", MB_OK | MB_ICONERROR);
return 0;
}

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE, g_szClassName, "Title of my window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed", "Error", MB_OK | MB_ICONERROR);
return 0;
}

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

TheCPUWizard
July 10th, 2008, 07:19 AM
What does GetLastError() return???

cosmeo3000
July 10th, 2008, 07:32 AM
How would I use GetLastError? Sorry, i'm not so experienced at programming =P

TheCPUWizard
July 10th, 2008, 08:10 AM
How would I use GetLastError? Sorry, i'm not so experienced at programming =P


int errCode = GetLastError();


Then look up the returned value in the documentation.

This is clearly stated in the documentation for CreateWindowEx. :rolleyes:

cosmeo3000
July 10th, 2008, 09:49 AM
int errCode = GetLastError();


Then look up the returned value in the documentation.

This is clearly stated in the documentation for CreateWindowEx. :rolleyes:
Returns with 0 for me... but isn't GetLastError for coding errors? The message box that comes up was one that I coded in, but it should only come up when the window wasn't created properly.

TheCPUWizard
July 10th, 2008, 09:55 AM
... but isn't GetLastError for coding errors? .

NO it is the mechanism used whenever a function can not successfully complete.

messycan
July 10th, 2008, 11:16 AM
What does GetLastError() return???


Well by inspecting the original code, you can see that there is a missing return for the Defualt Window Proc.


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


;)

cosmeo3000
July 10th, 2008, 11:32 AM
Well by inspecting the original code, you can see that there is a missing return for the Defualt Window Proc.


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


;)
Thank you very much, that seemed to be the problem :D

Igor Vartanov
July 12th, 2008, 05:58 AM
In fact, the more appropriate way would be the following:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
You were lucky, those messages imply zero to be returned from handler. Some other messages may require for returning values other than zero, so it seems like a good habit to take care about returning the excact value the documentation requires.

//JP added flex table