Getting Memory Access Violation error into the RegisterClassEx function
I have recently started to do programming in to C++. I having an issue while doing programming with windows programming on C++. I am getting Memory Access Violation with the RegiterClassEx function. I am posting the code below and let me know where exactly I am making the mistake. Let me know if you are having any specific solution to get the requirement of mine. Any help would be highly appreciated. Thanks a lot in advance.
Code:
#include<Windows.h>
#include<iostream>
using namespace std;
const string ProgramTitle = "Hello Windows";
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
string text = "Hello Windows!";
switch(message)
{
case WM_PAINT:
{
RECT rt;
GetClientRect(hWnd, &rt);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
DrawText(hdc, text.c_str(), text.length(), &rt, DT_CENTER);
for(int n = 0; n < 3000; n++)
{
int x = rand() % (rt.right - rt.left);
int y = rand () % (rt.bottom - rt.top);
COLORREF c = RGB(rand() % 265, rand() % 265, rand() % 265);
SetPixel(hdc, x, y, c);
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName - ProgramTitle.c_str();
wc.hIconSm = NULL;
return RegisterClassEx(&wc);
}
bool InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd = CreateWindow(
ProgramTitle.c_str(),
ProgramTitle.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640, 480,
NULL,
NULL,
hInstance,
NULL);
if(hWnd == 0)
return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MyRegisterClass(hInstance);
if(!InitInstance(hInstance, nCmdShow))
return 0;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
re: Getting Memory Access Violation error into the RegisterClassEx function
Well in order to get further help I am suggesting that you have to run the program into the debug mode. It will help you out which line is causing an issue in the above mentioned code. It will give you a better idea to find out the where exactly is the issue is happening in the situation of yours. so you should try the same and let me know whether it is working or not.
re: Getting Memory Access Violation error into the RegisterClassEx function
Well I was having the same issue which you have mentioned over here and I have done lots of research work and I have figure out the root cause of the problem. I have found that the wc.lpszClassName unitized in the code of mine. So I have simply initialized the same and I have resolved the error message from the game code of mine. So I recommend that you should initialized the wc.lpszClassName and see whether the error message is persisting or not.
re: Getting Memory Access Violation error into the RegisterClassEx function
I think I have figure out the root cause of the problem in the situation of yours. Well you have to replace the
Code:
wc.lpszClassName - ProgramTitle.c_str();
with the following code.
Code:
wc.lpszClassName = ProgramTitle.c_str();
once you have done the same you have the same you have to see that it is working or not. I am hoping that it would be useful to you.
re: Getting Memory Access Violation error into the RegisterClassEx function
As far as I know the above mentioned issue has been occurring just because of the referring the memory address which have been already released by the program itself. So you have to see that you should refer the object which you have mentioned over here and see whether it is working or not. So try the same and let me know whether it is working or not.
Re: Getting Memory Access Violation error into the RegisterClassEx function
Well I wanted to say thanks to all the guys to reply with possible solution over here to help me out to fix the matter of mine. I have done changes in the code which you have regarding the replacement of lines into the code. I wanted to appreciate your efforts which you have put in to help me out. Anyway thanks a lot for replying with possible solution over here.