-
Windows newbie question
I was wondering if anyone can tell me what is wrong with this code:
Code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMessage,
WPARAM wParam, LPARAM lParam);
void Display_Windows_Error(void);
HINSTANCE hInstGlobal;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE PrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASS WndClass;
WndClass.style = 0;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "WinProgram4";
if(RegisterClass(&WndClass) == 0)
return(1);
HWND hWindow;
hWindow = CreateWindow(WinProgram4","Window",
WS_OVERLAPPEDWINDOW,0,0
400,400,NULL,NULL,hInstance,NULL);
if(hWindow == NULL)
{
Display_Windows_Error();
return(1);
}
ShowWindow(hWindow,nShowCmd);
UpdateWindow(hWindow);
MSG Message;
while(GetMessage(&Message,NULL,0,0))
{ DispatchMessage(&Message); }
return(Message.wParam);
}
Basically the problem is this..... after the CreateWindow function I am checking to see if the hWindow is NULL and it is.Display_Windows_Error() is just a function to show me the what the error was. It calls GetLastError() function and FormatMessage() functions to tell me what went wrong. The error I am getting is "invalid Window Handle" and I can't figure out why. If anyone can tell me what is going wrong I would greatly appreciate it.
-
hWindow = CreateWindow("WinProgram4","Window",
WS_OVERLAPPEDWINDOW,0,0,
400,400,NULL,NULL,hInstance,NULL);
Syntax errors in the code you posted.
-
That is was not the problem. That is just my bad typing. If it was a syntax that was the problem I would be a happy camper :)
My problem is hWindow being NULL.
-
Just so you know, I'm Unregistered above.
I know that was'nt THE problem, I was just stating that the code you posted had syntax errors. :)
Regarding the code, I'm no expert but it seems pretty messed up lol. I tried to fix it.
-
One thing I noticed is that you never populated your hInstGlobal
variable.
Code:
hInstGlobal=hInstance;
WndClass.hInstance=hInstGlobal;
CreateWindow(blah...blah..blah,,,hInstGlobal,...);
You also left out WS_VISIBLE in the dwStyle param of the
CreateWindow call.
And your passing 0 as your WndClass.style. I could be wrong but
I've never seen 0 passed there.
-
Hmm ...... thanks for the advice. This code is from a book, not my own. hInstGlobal is used in the WndProc function. The code I posted was by no means complete. I just want to know why hWindow is NULL.
Just so I know, jizzer, What is wrong with the code I posted. You said it seemed pretty messed up. In what way? I am a total newbie to windows programming so I don't know if the book I am using is the right way or wrong way.
-
Not sure what book you're learning from, but I notice you're using
WNDCLASS, CreateWindow and RegisterClass. These 3 have been
superceded by WNDCLASSEX, CreateWindowEx and
RegisterClassEX. Of course I'm not saying that using the older
versions are the problem, just something to think about.
-
Ahhh I see. Yea I knew about that from reading microcraps help documents. Thanks for the info though