Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-06-2011
Member
 
Join Date: Apr 2011
Posts: 61
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;
}
Reply With Quote
  #2  
Old 25-06-2011
Member
 
Join Date: May 2009
Posts: 523
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.
Reply With Quote
  #3  
Old 25-06-2011
Member
 
Join Date: May 2009
Posts: 532
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.
Reply With Quote
  #4  
Old 25-06-2011
Member
 
Join Date: Apr 2009
Posts: 484
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.
Reply With Quote
  #5  
Old 25-06-2011
Member
 
Join Date: Apr 2009
Posts: 567
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.
Reply With Quote
  #6  
Old 28-06-2011
Member
 
Join Date: Apr 2011
Posts: 61
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Getting Memory Access Violation error into the RegisterClassEx function"
Thread Thread Starter Forum Replies Last Post
ACCESS VIOLATION error with Wargame: European Escalation Pirateous Video Games 3 27-02-2012 10:10 PM
Getting "Access Violation Error at address 00423B70 in Module Comcast" when shut down the PC Skyla Operating Systems 9 06-10-2011 06:15 PM
Error access violation at 0x02CEEFAE while playing Cradle of Rome 2 in windows 7 Banned-BlackBadal Video Games 3 21-12-2010 03:45 PM
0xC0000005 Access Violation Error Farley Operating Systems 5 14-03-2009 09:21 AM
WDS/PXE/TFTP Access Violation Error Message on Windows Server 2008 Ryan Newington Windows Server Help 6 16-05-2008 07:17 PM


All times are GMT +5.5. The time now is 01:29 PM.