Jump to content
  • Advertisement
Sign in to follow this  
hkBattousai

Which parameter of CreateWindowEx() is incorrect in my code?

This topic is 1417 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

I'm trying to create a simple window with the code below.

void MainWindow::Create()
{
	WNDCLASSEXW	wcx;
	wcx.cbSize		=	(UINT)		sizeof(wcx);
	wcx.style		=	(UINT)		CS_HREDRAW | CS_VREDRAW;
	wcx.lpfnWndProc		=	(WNDPROC)	MainWindowProc;
	wcx.cbClsExtra		=	(int)		0;
	wcx.cbWndExtra		=	(int)		0;
	wcx.hInstance		=	(HINSTANCE)	Status::hInstance;
	wcx.hIcon		=	(HICON)		NULL;
	wcx.hCursor		=	(HCURSOR)	NULL;
	wcx.hbrBackground	=	(HBRUSH)	COLOR_BACKGROUND;
	wcx.lpszMenuName	=	(LPCWSTR)	NULL;
	wcx.lpszClassName	=	(LPCWSTR)	m_ClassName.c_str();
	wcx.hIconSm		=	(HICON)		NULL;

	RegisterClassExW(&wcx);

	DWORD dw1 = GetLastError();    // Returns 0.

	m_Handle = CreateWindowExW(	(DWORD)		WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_TABSTOP | WS_GROUP | WS_VISIBLE,
					(LPCWSTR)	m_ClassName.c_str(),
					(LPCWSTR)	m_WindowTitle.c_str(),
					(DWORD)		WS_EX_ACCEPTFILES | WS_EX_CONTROLPARENT | WS_EX_LEFT | WS_EX_LTRREADING,
					(int)		50,
					(int)		50,
					(int)		800,
					(int)		600,
					(HWND)		HWND_DESKTOP,
					(HMENU)		NULL,
					(HINSTANCE)	Status::hInstance,
					(LPVOID)	NULL);

	DWORD dw2 = GetLastError();    // Returns 87.
}

The windows handle m_Handle is NULL, and the second GetLastError() returns 87, which means "The parameter is incorrect.". I tried sending different style codes, different class names, but it didn't change anything. Can you please help me fix this code?

 

Other relevant parts of the code are as shown below.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	Status::hInstance = hInstance;	// hInstance is stored in a static class member here. It is not modified anywhere else.

	Gui::GI()->MainWnd.Create();	// Calls the MainWindow::Create() method shown above.

	MSG Msg;
	HWND hWnd = Gui::GI()->MainWnd.GetHandle();
	while (GetMessageW(&Msg, NULL, 0, 0))
	{
		if (!IsDialogMessageW(hWnd, &Msg))
		{
			TranslateMessage(&Msg);
			DispatchMessageW(&Msg);
		}
	}
	return (int) Msg.wParam;
}
MainWindow::MainWindow()
{
	m_ClassName = L"MyMainWindowClass";    // std::wstring
	m_WindowTitle = L"My Game";            // std::wstring
}

Share this post


Link to post
Share on other sites
Advertisement

What do you initialize HWND_DESKTOP to?

 

edit: nevermind I looked up HWND_DESKTOP, I didn't realize it was a predefined constant.

Edited by nobodynews

Share this post


Link to post
Share on other sites

HWND_DESKTOP.

 

You are probably msitakenly making what is called an "owned window" but passing garbage as the parent window's  HWND. Set that parameter to NULL to make an unowned toplevel window otherwise set it to a valid window handle. 

 

Oh, yeah, SmkViper is right...

Edited by jwezorek

Share this post


Link to post
Share on other sites

Why are you casting everything? You don't need to cast a NULL pointer to NULL. The std::wstring::c_str() method returns a LPCWSTR which is a Long Pointer to a Const Char Wide STRing. If you're returning a const char*, you'll get incorrect values because LPCWSTR != LPCSTR.

 

Also, you definetely should use the this pointer in the CreateWindowEx(...) function. Your loop is not OK also. Consider using this example for a flexible Win32 window creation using OOP. 

Edited by Irlan

Share this post


Link to post
Share on other sites
I think the primary problem here is that you're using the raw Win32 API instead of a simpler, saner wrapper library. tongue.png

Even many big AAA game engines have switched to things like SDL for creating their window and polling input even on Windows. Works equally well with Direct3D as with OpenGL.

Share this post


Link to post
Share on other sites

I think the primary problem here is that you're using the raw Win32 API instead of a simpler, saner wrapper library. tongue.png

Even many big AAA game engines have switched to things like SDL for creating their window and polling input even on Windows. Works equally well with Direct3D as with OpenGL.

If you're starting it is fine, but creating your own library all you're doing is what you have to do in the future that is to build your own things not using third party or open source libraries. Having full control of your application is essencial in my opinion.

 

You can reuse the code you made before by simple linking the library within a project. It is simple as it sounds and doesn't takes more than 10 hours to code. For instance, a simple thread library - you'll face with paralelism later - can be nothing more than a Thread and a Critical Section class, which is.

#ifndef CRITICALSECTION_H_
#define CRITICALSECTION_H_

#include <Windows.h>

class CCriticalSection {
public:
	CCriticalSection();
	~CCriticalSection();
	void Lock();
	void Unlock();
private:
	CRITICAL_SECTION m_csSection;
};

class CLocker {
public :
	inline CLocker(CCriticalSection& _csToLock) {
		m_pcsCritic = &_csToLock;
		m_pcsCritic->Lock();
	}
	inline ~CLocker() {
		m_pcsCritic->Unlock();
	}
protected :
	CCriticalSection* m_pcsCritic;
};

#endif

It is basic. Note that I said "can be nothing more than", because if you need to use more complex classes such thread pools to load resources, obviously you'll need to increase the complexity of your project and you'll probably need more time as well.

 

That was just an example but applies to the things you need on the moment, such the Win32 Window class you mentioned.

 

Nothing is stopping you to do what you'll need to do anyway in the future. 

Edited by Irlan

Share this post


Link to post
Share on other sites

If you're starting it is fine, but creating your own library all you're doing is what you have to do in the future that is to build your own things not using third party or open source libraries. Having full control of your application is essencial in my opinion


Mind explaining why you think it is essential to have "full control" over such a well defined and solved problem as windowing, which SDL (amongst other frameworks) provides a good (platform independant) solution to? I can understand wanting to do it, but I cannot see how it would be essential to have low level control over such a thing..?

Share this post


Link to post
Share on other sites

 

If you're starting it is fine, but creating your own library all you're doing is what you have to do in the future that is to build your own things not using third party or open source libraries. Having full control of your application is essencial in my opinion


Mind explaining why you think it is essential to have "full control" over such a well defined and solved problem as windowing, which SDL (amongst other frameworks) provides a good (platform independant) solution to? I can understand wanting to do it, but I cannot see how it would be essential to have low level control over such a thing..?

 

In my project I don't want to pool events, I want to have an separate thread to do the job and buffer the keyboard data, which drives me to writing my own keyboard routines, which drives me to not choose an existing library to do this job, and since the library has keyboard related routines it start to enter in conflict with my own routines. Such thing may be not possible to do on these types of frameworks.
Having full control is the same thing of writing code of something that you have a solid knowledge; using third party libraries you don't learn anything but memorize functions for creating everything.
The funny fact is that the time it takes to learn the low-level stuff, it is the same to learn such high-level aspect of the library you're using, and you realize it when you did used these libraries and started coding by your own.
 
So it is not just a question of inserting Your Name™ in the header file, is for academic purposes also.
Edited by Irlan

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!