9 Replies - 1048 Views - Last Post: 21 July 2011 - 06:49 PM Rate Topic: -----

#1 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

C++ newbie question ...

Posted 21 July 2011 - 12:59 PM

Hi all,

I'm going through a simple win32 tutorial, and I'm getting an error.

All the application does is open a window with a textbox that takes input from the user, and when the button is pressed it displays a messagebox with whatever the user typed in the box.

The message box pops up properly but when I press "Ok" I get the following error:

Windows has triggered a breakpoint test.exe.

This may be due to a corruption of the heap, which indicates a bug in test.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while test.exe has focus.


Here is the code:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hMsgBoxBTN;
HWND hEdit;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR szCmdLine, int nShowCmd)
{
	static wchar_t name[] = L"My Application";
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	//Step 1: Registering the Window Class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = name;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, L"Window Registration Failed!", L"Registration Failure",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		name,
		L"My First Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return (int)Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch(msg)
	{
	case WM_CREATE:
		hMsgBoxBTN = CreateWindowEx(WS_EX_WINDOWEDGE, L"BUTTON", L"MSG Box", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
					0, 0, 0, 0, hwnd, NULL, GetModuleHandle(NULL), NULL);
		hEdit = CreateWindowEx(WS_EX_WINDOWEDGE, L"EDIT", L"Type Here", WS_CHILD | WS_VISIBLE|
				ES_MULTILINE | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
				0, 0, 0, 0, hwnd, NULL, GetModuleHandle(NULL), NULL);

		break;

	case WM_SIZE:
		GetClientRect(hwnd, &rect);
		SetWindowPos(hMsgBoxBTN, NULL, rect.right / 2 - 50, rect.bottom - 35, 100, 25, SWP_NOZORDER);
		SetWindowPos(hEdit, NULL, 0, 0, rect.right, rect.bottom - 50, SWP_NOZORDER);

		break;
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		
		EndPaint(hwnd, &ps);
		break;
	case WM_COMMAND:
		
		switch(LOWORD(wParam))
		{
		case BN_CLICKED:

			if(hMsgBoxBTN == (HWND)lParam)
			{
				int len = GetWindowTextLength(hEdit);
				wchar_t *buff = (wchar_t*)GlobalAlloc(GPTR, len+1);
				GetWindowText(hEdit, buff, len+1);
				MessageBox(hwnd, buff, L"Message", MB_OK);
				GlobalFree((HANDLE)buff);
			}
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}




Any suggestions?

Thanks in advance..

This post has been edited by Btu: 21 July 2011 - 01:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ newbie question ...

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: C++ newbie question ...

Posted 21 July 2011 - 01:18 PM

GlobalAlloc or GlobalFree is causing the problem, although I'm not completely sure why just now. I'll look into it more.

Try using new and delete instead, .e.g.

int len = GetWindowTextLength(hEdit);
wchar_t *buff = new wchar_t [len+1];
GetWindowText(hEdit, buff, len+1);
MessageBox(hwnd, buff, L"Message", MB_OK);
delete [] buff;


edit:
GlobalAlloc doesn't seem to be allocating enough memory for whatever reason.

This post has been edited by Aphex19: 21 July 2011 - 01:22 PM

Was This Post Helpful? 3
  • +
  • -

#3 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: C++ newbie question ...

Posted 21 July 2011 - 01:25 PM

Great! This fixed the problem.

As for your edit: How did you debug that?
(I'm using Visual Studio 2010)

This post has been edited by Btu: 21 July 2011 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ newbie question ...

Posted 21 July 2011 - 01:30 PM

I was able to compile and run you program as: <removed -- unhelpful>

I didn't run into any errors and I don't see anything that should cause a buffer overrun (so long as unicode is enabled).

In the IDE you can actually enable Unicode and add the user32.lib rather than need the #define and #pragma lines.


View PostAphex19, on 21 July 2011 - 04:18 PM, said:

GlobalAlloc or GlobalFree is causing the problem, although I'm not completely sure why just now. I'll look into it more.

Try using new and delete instead, .e.g.

int len = GetWindowTextLength(hEdit);
wchar_t *buff = new wchar_t [len+1];
GetWindowText(hEdit, buff, len+1);
MessageBox(hwnd, buff, L"Message", MB_OK);
delete [] buff;


edit:
GlobalAlloc doesn't seem to be allocating enough memory for whatever reason.



Ah! yes wchar_t type is larger than 1 char so you need
wchar_t *buff = (wchar_t*)GlobalAlloc(GPTR, (len+1)*sizeof(wchar_t));

good catch!
Was This Post Helpful? 1
  • +
  • -

#5 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: C++ newbie question ...

Posted 21 July 2011 - 01:32 PM

This is great info, thanks guys!

One last thing: This UNICODE issue threw me off a bit when following the tutorial. I had to change char to wchar_t and add L before all the strings.

What is the reason for this? If I'm asking a vague question, I'd gladly follow a link and do some reading.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ newbie question ...

Posted 21 July 2011 - 01:43 PM

Simply put -- C/C++ are generally ASCII based. However computers have really come a long way since the very US-centric days of ASCII. The problem is that a char can only hold 256 different values. Well there are many languages and even more characters than can be represented by 256 different values.

Unicode ups the size of a "character" from 1 byte to 2 (or 4 even). the wchar_t type is 2 bytes long and is capable of handling many more characters than char(65536 to be exact).

the prefixed L mean "long" and tells the compiler to use wchar_t characters rather than char to represent the string.

Note unfortunately C/C++ are still not Unicode friendly and so just because you use the L does not mean that the text is in Unicode (although for most western chars it should work but if you try to add other chars things get strange).

BTW to output wchar_t strings use wcout rather than regular old cout.
Was This Post Helpful? 1
  • +
  • -

#7 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: C++ newbie question ...

Posted 21 July 2011 - 01:46 PM

Wow, fantastic. You guys have been really helpful, thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#8 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: C++ newbie question ...

Posted 21 July 2011 - 03:08 PM

I have another question, I was hoping one of you could help me understand something:

In this block of code, I don't understand what is happening. I put a debugging dot on it and followed it through the loop many times and tried to see what was going on, but I just can't make sense of it:

// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return (int)Msg.wParam;


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: C++ newbie question ...

Posted 21 July 2011 - 06:45 PM

Since these are all Windows API calls it might be better if you checked the documentation for these functions: TranslateMesage(), DispatchMessage. And maybe this tutorial on the Windows MessageLoop.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: C++ newbie question ...

Posted 21 July 2011 - 06:49 PM

Thanks Jim.
Apologies to Aphex19 for overposting on this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1