Hi all,

I've create a window using CreateWindowEx windows API and do some work on it. Thats fine. But for the safer operation at the end of all the processing I want to close/destroy the window.

I've try this,

BOOL clsWin = DestroyWindow(m_hwnd_RTFBox);
	if(clsWin != 0)
	{
		printf("SS");
	}

Here m_hwnd_RTFBox is the handle to the window. On the above code there is no compile error, but gives a runtime error and saying there is a unhandled exception on the first line. Where I'm going wrong. I found this from the MSDN, but it wont work.

// Create window
	try
	{
		m_hwnd_RTFBox = CreateWindowEx(
							WS_EX_APPWINDOW,
							RICHEDIT_CLASS,
							"RichTextWindow",
							WS_BORDER|ES_MULTILINE,
							0,
							0,
							100,
							100,
							::GetConsoleWindow(),
							NULL,
							0,
							NULL);
	}
	catch(...)
	{
		std::cerr << "Can't create the window handler." << std::endl;
	}
// Processing done here
	::SetWindowText(m_hwnd_RTFBox, rtf.c_str());
	int recCount = GetWindowText(m_hwnd_RTFBox, pBuffer, 1024);
	
	if(recCount > 0)
	{
		string strPlr(pBuffer, recCount);
		cout << strPlr;
	}

// Want to destroy the window here

I think The window must have WindowProcedure ,and then a message system control to DestroyWindow.

CreateWindowEx does not throw an exception on error so there is no point to the try/catch block. If the function fails then you have to call GetLastError() to find out the error number, and you can pass that to FormatMessage() to get a human-readable error message.

I don't think you want to use the console's window handle as the parent of a Windows GUI program. Instead, pass NULL as that parameter.

>>But I can't use WindowProc, because I use windows API here in my code
That's what the WindowProc function is for. Establish the WindowProc when you call RegisterClass before CreateWindowEx()

If you want to write win32 api programs then don't start out with a console program. Set this tutorial for the basics.

CreateWindowEx does not throw an exception on error so there is no point to the try/catch block. If the function fails then you have to call GetLastError() to find out the error number, and you can pass that to FormatMessage() to get a human-readable error message.

Yes there is no exception thrown. I've just use it for my clear work out, and also I've never use the GetLastError() because few time I'm confused that at which place should I use it.

I don't think you want to use the console's window handle as the parent of a Windows GUI program. Instead, pass NULL as that parameter.

Used the console window as the main window because my application also console based application.

Now ,i do not uderstand anytohing what you wrote.


eranga262154 wrote
>But I can't use WindowProc, because I use windows API here in my code.

Ancient
>>I don't think you want to use the console's window handle as the parent of a Windows GUI program.


He use API i tought that is for windows application.And must have WinProc.

He use API i tought that is for windows application.

Yes I use windows API

And must have WinProc.

In API I can't use it, isn't it?

>>Used the console window as the main window because my application also console based application.

CreateWindowsEx REQUIRES a win32 api program because it is a GUI. You can't create one with a console program. Period.

>>He use API i tought that is for windows application.And must have WinProc.
I agree. Which is why his program crashes. And he doesn't have a message pump so the window will never get any messages even if it were created.

And he doesn't have a message pump so the window will never get any messages even if it were created.

I can send messages to the window. Send a message to close the window, (WM_CLOSE) and try to destroy the window. But not work. I can send messages to it.

Obivous is that you can't send the real message,because it is not working.Or i do not know.

I can send messages to the window. Send a message to close the window, (WM_CLOSE) and try to destroy the window. But not work. I can send messages to it.

You can send all the messages you want, but without a message pump to pass them along to the correct window they will just be tossed into the bit bucket by the Windows operating system. You need to read and code a tutorial about how to write a windows program.

I'm not clear what Ancient Dragon says. You says that, according to my code I can't send message to the window. I've try it with PostMessage() with the window handler. So....

>>You says that, according to my code I can't send message to the window
Read Very Carefully and Very sloooooooooooowly. I did not say that. Shall I repeat -- I did not say that. What I said was that the window will never receive any of the messages your console program sends it. Calling PostMessage() does not guarentee the receipient will receive the message. When you mail a letter at the post office the person to whom you sent the letter may or may not receive it. If the person lives some place that has no post office then your letter will be sent to the Post Office's dead letter office. Same with Windows messages.

If you will just read this win32 api tutorial you will probably understand more.

That is really helpful to me. Thanks.

As there example explain, I try to send a message and depend on it destroy the window. But in my case the window is the console. If I get correct in my application I can't destroy the window. Right? So that mean the window destroyed automatically....

This article has been dead for over six months. Start a new discussion instead.