Jump to content
  • Advertisement
Sign in to follow this  
TheUnbeliever

Problems with CreateWindowEx

This topic is 4442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Actually the problem persists with CreateWindow, obviously. I'm just starting out, trying to get a hang of using the WINAPI. First simple thing I'm trying to do is simply display a window - I realise there are more complex things that you would normally need to do, register window class, etc. However, this should work according the the MSDN library which I checked at Using Windows and CreateWindow - at least, assuming I understood it correctly (but also the demo code is identical to mine, obviously, for such a simple task). My code is thus:
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HINSTANCE hInstMainWindow;

	HWND hMainWindow =
		CreateWindowEx(	0,
				"MainWindowClass",
				"My First WINAPI Window!",
				WS_OVERLAPPEDWINDOW,
				0,
				0,
				800,
				600,
				NULL,
				NULL,
				hInstMainWindow,
				NULL);

	if(!hMainWindow)
		MessageBox(NULL, "Window creation failed", "Win32Learning", MB_ICONEXCLAMATION);

	ShowWindow(hMainWindow, SW_SHOWDEFAULT);
	UpdateWindow(hMainWindow);
	return 0;
}


Using Visual C++ 2005 Express edition, having setup the Platform SDK alongside it correctly (I can display MessageBoxes, for example, without a problem simply to test). My problem is that I get the following runtime error when I run it in the debugger by hitting F5: "Run-Time Check Failure #3 - The variable 'hInstMainWindow' is being used without being defined.". The window creation obviously fails, throwing up my error messagebox. I reckoned that that made sense, and also tried
HINSTANCE *hInstMainWindow = new HINSTANCE
(with the appropriate dereferencing operator added in in the function call). I also tried replacing hInstMainWindow in the function call with hInstance from the WinMain declaration. Under both these circumstances, I skip the runtime error and go straight to the error messagebox.

Share this post


Link to post
Share on other sites
Advertisement
You don't need hInstMainWindow; the HINSTANCE parameter of CreateWindow and CreateWindowEx is the instance of the module, which is the hInstance parameter of your WinMain function. You're getting the error message box because the Window Class MainWindowClass has not been registered. You must register a Window Class by filling a WNDCLASSEX structure and registering it with RegisterClassEx before you can use it in CreateWindowEx (unless you're using a system class).

Share this post


Link to post
Share on other sites
Ah, I see. That makes sense. Thanks, I guess I was doing myself more harm than good by trying to learn as little as possible at a given time! I'll give that a try just now.

Share this post


Link to post
Share on other sites
Thanks guys. I've currently got

[source lang="cpp#include <windows.h>

LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcMainWindowClass;
MSG msgMessage;

wcMainWindowClass.cbSize = sizeof(WNDCLASSEX);
wcMainWindowClass.style = 0;
wcMainWindowClass.lpfnWndProc = MainWndProc;
wcMainWindowClass.cbClsExtra = 0;
wcMainWindowClass.cbWndExtra = 0;
wcMainWindowClass.hInstance = hInstance;
wcMainWindowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wcMainWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wcMainWindowClass.hbrBackground = (HBRUSH) COLOR_WINDOW+1;
wcMainWindowClass.lpszMenuName = NULL;
wcMainWindowClass.lpszClassName = "MainWindowClass";
wcMainWindowClass.hIconSm = NULL;

if(!RegisterClassEx(&wcMainWindowClass))
MessageBox(NULL, "Registering window class failed", "Win32Learning", MB_ICONEXCLAMATION);

HWND hMainWindow =
CreateWindowEx( 0,
"MainWindowClass",
"My First WINAPI Window!",
WS_OVERLAPPEDWINDOW,
0,
0,
800,
600,
NULL,
NULL,
hInstance,
NULL);

if(!hMainWindow)
MessageBox(NULL, "Window creation failed", "Win32Learning", MB_ICONEXCLAMATION);

ShowWindow(hMainWindow, SW_SHOWDEFAULT);
UpdateWindow(hMainWindow);

while(GetMessage(&msgMessage, NULL, 0, 0))
{
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}

return msgMessage.wParam;
}

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


Which shows the window briefly before closing again. I'll read up on those message pumps, but I'm happy that I've managed my target for tonight, and so shall head off. :) Thanks again, both.

EDIT: Actually the message pump was quite easy so I've implemented that, thanks, now I've got a bog standard window. Interesting how, with a bit of reading, this all looks a lot less like gibberish than it did about 12 hours ago. I've updated the source above with it. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!