Results 1 to 6 of 6

Thread: Getting Memory Access Violation error into the RegisterClassEx function

  1. #1
    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;
    }

  2. #2
    Join Date
    May 2009
    Posts
    527

    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.

  3. #3
    Join Date
    May 2009
    Posts
    539

    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.

  4. #4
    Join Date
    Apr 2009
    Posts
    488

    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.

  5. #5
    Join Date
    Apr 2009
    Posts
    569

    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.

  6. #6
    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.

Similar Threads

  1. Replies: 3
    Last Post: 27-02-2012, 10:10 PM
  2. Replies: 9
    Last Post: 06-10-2011, 06:15 PM
  3. Replies: 3
    Last Post: 21-12-2010, 03:45 PM
  4. 0xC0000005 Access Violation Error
    By Farley in forum Operating Systems
    Replies: 5
    Last Post: 14-03-2009, 09:21 AM
  5. WDS/PXE/TFTP Access Violation Error Message on Windows Server 2008
    By Ryan Newington in forum Windows Server Help
    Replies: 6
    Last Post: 16-05-2008, 07:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,012,515.64205 seconds with 16 queries