Exception thrown at 0x00007FF93FEABAF0 (user32.dll) in Win32 API GUI.exe: 0xC0000005: Access violation writing location 0x0000000000000064.

Calin 20 Reputation points
2023-09-08T01:34:34.0433333+00:00

Code compiles fine, exception occurs when typing something in the textbox then clicking File>open submenu>Change Title.

I know it's a bug, I'm just very new to C++ and don't understand too much about it yet.

Code is from this specific tutorial: https://www.youtube.com/watch?v=9JMQkUOhW1s&list=PLWzp0Bbyy_3i750dsUj7yq4JrPOIUR_NK&index=4&ab_channel=ThePentamollisProject

Full code:

#include<windows.h>
#define FILE_MENU_NEW 1
#define FILE_MENU_OPEN 2
#define FILE_MENU_EXIT 3
#define CHANGE_TITLE 4
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);
void AddControls(HWND);
HMENU hMenu;
HWND hEdit;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow)
{
	WNDCLASS wc = {0};
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hInstance = hInst;
	wc.lpszClassName = L"myWindowClass";
	wc.lpfnWndProc = WindowProcedure;
	if (!RegisterClassW(&wc))
		return 1;
	CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500,
		NULL, NULL, NULL, NULL);
	MSG msg = {0};
	while (GetMessage(&msg,NULL,NULL,NULL)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	switch (msg) 
	{
	case WM_COMMAND:
		switch(wp)
		{
		case FILE_MENU_EXIT:
			DestroyWindow(hWnd);
			break;
		case FILE_MENU_NEW:
			MessageBeep(MB_ICONINFORMATION);
			break;
		case CHANGE_TITLE:
			wchar_t text(100);
			GetWindowTextW(hEdit, (LPWSTR)text,100);
			SetWindowTextW(hWnd, (LPWSTR)text);
			break;
		}
		break;
	case WM_CREATE:
		AddMenus(hWnd);
		AddControls(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProcW(hWnd, msg, wp, lp);
	}
}
void AddMenus(HWND hWnd) 
{
	hMenu = CreateMenu();
	HMENU hFileMenu = CreateMenu();
	HMENU hSubMenu = CreateMenu();
	AppendMenu(hSubMenu, MF_STRING, CHANGE_TITLE, L"Change Title");
	AppendMenu(hFileMenu, MF_STRING, FILE_MENU_NEW, L"New");
	AppendMenu(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"Open Submenu");
	AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
	AppendMenu(hFileMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");
	AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFileMenu, L"File");
	AppendMenu(hMenu, MF_STRING, NULL, L"Help");
	SetMenu(hWnd,hMenu);
}
void AddControls(HWND hWnd) 
{
	CreateWindowW(L"static", L"Enter text here:", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER,200,100,100,50,hWnd,NULL,NULL,NULL);
	hEdit = CreateWindowW(L"edit", L"...", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 200, 152, 100, 50, hWnd, NULL, NULL, NULL);
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,732 questions
{count} votes

Accepted answer
  1. Viorel 117.4K Reputation points
    2023-09-08T06:10:23.99+00:00

    Try to replace wchar_t text(100) with wchar_t text[100].

    Use text instead of (LPWSTR)text in next lines.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.