Sign in to follow this  

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

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

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

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..?

It's not like it isn't a useful thing to learn, I've had people ask me why I've used winapi in some of my programs, there are plenty of reasons.
  • SDL/SFML/Some windowing library, isn't always going to be good enough, it also introduces a dependency you may not want to have.
  • It's good to do it a few times, IMO, too many people here comment like you should avoid things they already learned even though understanding.
  • An example of learning might be: how the OS painting and GDI works is useful, or how raw OS messages are actually given and translated by libraries into a more friendly form. Things make a lot more sense after you learn them a bit.
My rule of thumb besides tinkering around with winapi to learn more about it, is only use it if I'm writing everything else from scratch anyway. I definitely would suggest SDL or SFML if your focus is more on just getting a game together though.

Share this post


Link to post
Share on other sites

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

Not sure what you're getting at here.. the OS automatically pools messages for you, least every OS I know of. Using something like SFML you have to ask for each OS event just like you would with raw winapi, they just do background translation for you, theres also nothing stopping you from starting another thread that does that constantly. Although to be frank I'm not sure you'll get much of a performance increase here unless you're doing a pretty fancy game. Realistically processing and translating OS events into game friendly form doesn't take very long at all.
 

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.

Yes, but that doesn't mean you should never use tools if they're available and simple for you to use.
 

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.

Personally I've written code to register and spawn windows with.. well, windows quite a few times in the past, as well as all the direct3d code to interface with them, that doesn't mean I automatically always want to do that. Frankly a lot of the time its just redundant and a waste of time when I can just link to a library I already am very familiar with(you can learn libraries just like low level api's.)

EDIT: Woops, thought I hit edit message. My bad! Pretend these two are glued together. Edited by Satharis

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..?

 

It's not like it isn't a useful thing to learn, I've had people ask me why I've used winapi in some of my programs, there are plenty of reasons.
  • SDL/SFML/Some windowing library, isn't always going to be good enough, it also introduces a dependency you may not want to have.
  • It's good to do it a few times, IMO, too many people here comment like you should avoid things they already learned even though understanding.
  • An example of learning might be: how the OS painting and GDI works is useful, or how raw OS messages are actually given and translated by libraries into a more friendly form. Things make a lot more sense after you learn them a bit.
My rule of thumb besides tinkering around with winapi to learn more about it, is only use it if I'm writing everything else from scratch anyway. I definitely would suggest SDL or SFML if your focus is more on just getting a game together though.

 

If is just a game you should use an existing game engine to the job since is 10x higher level than libraries such SDL, SFML, etc.

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..?

 

It's not like it isn't a useful thing to learn, I've had people ask me why I've used winapi in some of my programs, there are plenty of reasons.
  • SDL/SFML/Some windowing library, isn't always going to be good enough, it also introduces a dependency you may not want to have.
  • It's good to do it a few times, IMO, too many people here comment like you should avoid things they already learned even though understanding.
  • An example of learning might be: how the OS painting and GDI works is useful, or how raw OS messages are actually given and translated by libraries into a more friendly form. Things make a lot more sense after you learn them a bit.
My rule of thumb besides tinkering around with winapi to learn more about it, is only use it if I'm writing everything else from scratch anyway. I definitely would suggest SDL or SFML if your focus is more on just getting a game together though.

 

If is just a game you should use an existing game engine to the job since is 10x higher level than libraries such SDL, SFML, etc.

 

Agreed, unless you got some other motive, like you want to do it for fun or as a learning excercise. I also understand the reasons listed by Satharis and I agree that it is indeed useful to learn. What I dont get is why it would be essential to have full control over windowing..

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..?

 

It's not like it isn't a useful thing to learn, I've had people ask me why I've used winapi in some of my programs, there are plenty of reasons.
  • SDL/SFML/Some windowing library, isn't always going to be good enough, it also introduces a dependency you may not want to have.
  • It's good to do it a few times, IMO, too many people here comment like you should avoid things they already learned even though understanding.
  • An example of learning might be: how the OS painting and GDI works is useful, or how raw OS messages are actually given and translated by libraries into a more friendly form. Things make a lot more sense after you learn them a bit.
My rule of thumb besides tinkering around with winapi to learn more about it, is only use it if I'm writing everything else from scratch anyway. I definitely would suggest SDL or SFML if your focus is more on just getting a game together though.

 

If is just a game you should use an existing game engine to the job since is 10x higher level than libraries such SDL, SFML, etc.

 

Agreed, unless you got some other motive, like you want to do it for fun or as a learning excercise. I also understand the reasons listed by Satharis and I agree that it is indeed useful to learn. What I dont get is why it would be essential to have full control over windowing..

 

In the example I gave said that if I want to have an separate thread to do the event buffering job. For those who know the Win32 API, they know that this is not possible to do in a library such SDL, SFML, etc, because these libraries are already doing polling I suppose, and since my main target is performance and separation of concerns, why I will be doing such thing when in order to archive maximum performance and coherence with the rest of my project I would have to write my own code?! Having full control over "windowing" in this case is the same thing of having control over input messages and the way you manage them.

 

Note that I'm not talking about frameworks such Qt, etc, because altough I did never used, I know that Qt is essential when programming tools and other related things. I'm talking about the final real-time application you're running or the tech-demo you're doing, etc.

Edited by Irlan

Share this post


Link to post
Share on other sites

This topic is 1109 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this