malloc() problem...

This is a discussion on malloc() problem... within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys, still working through my book and i have gotten into keyboard stuff. I have having some trouble compiling ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    malloc() problem...

    Hi guys, still working through my book and i have gotten into keyboard stuff. I have having some trouble compiling some source code. Heres the code, i highlighted the line im getting an error on. Any help would be greatly appreciated, thanks!

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[]=TEXT("KeyView1");
        HWND hwnd;
        MSG msg;
        WNDCLASS wndclass;
        
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc = WndProc;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName;
        
        if(!RegisterClass(&wndclass))
        {
            MessageBox(NULL, TEXT("This program requires Windows NT/XP!"), szAppName, MB_ICONERROR);
            return 0;
        }
        
        hwnd = CreateWindow(szAppName, TEXT("Keyboard Message Viewer #1"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            NULL, NULL, hInstance, NULL);
        ShowWindow(hwnd, iCmdShow);
        UpdateWindow(hwnd);
        
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar;
        static int cLinesMax, cLines;
        static PMSG pmsg;
        static RECT rectScroll;
        static TCHAR szTop[]=TEXT("Message        Key       Char    ")
                             TEXT("Repeat Scan Ext ALT Prev Tran");
        static TCHAR szUnd[]=TEXT("_______        ___       ____    ")
                             TEXT("______ ____ ___ ___ ____ ____");
        
        static TCHAR * szFormat[2]={TEXT("%-13s %3d %-15s%c%6u %4d %3s %3s %4s %4s"),
                                    TEXT("%13s             0x%04X%1s%c %6u %4d %3s %3s %4s %4s")};
        
        static TCHAR * szYes=TEXT("Yes");
        static TCHAR * szNo=TEXT("No");
        static TCHAR * szDown=TEXT("Down");
        static TCHAR * szUp=TEXT("Up");
        
        static TCHAR * szMessage[]={
            TEXT("WM_KEYDOWN"),    TEXT("WM_KEYUP"),
            TEXT("WM_CHAR"),       TEXT("WM_DEADCHAR"),
            TEXT("WM_SYSKEYDOWN"), TEXT("WM_SYSKEYUP"),
            TEXT("WM_SYSCHAR"),    TEXT("WM_SYSDEADCHAR")};
        
        HDC hdc;
        int i, iType;
        PAINTSTRUCT ps;
        TCHAR szBuffer[128], szKeyName[32];
        TEXTMETRIC tm;
        
        switch(message)
        {
            case WM_CREATE:
            case WM_DISPLAYCHANGE:
                //Get maximum size of the client area
                cxClientMax=GetSystemMetrics(SM_CXMAXIMIZED);
                cyClientMax=GetSystemMetrics(SM_CYMAXIMIZED);
                //get character size for fixed pitch font
                hdc=GetDC(hwnd);
                //allocate memory for display lines
                if(pmsg){
                    free(pmsg);}
                cLinesMax=cyClientMax/cyChar;
                pmsg = malloc(cLinesMax * sizeof(MSG));
                cLines=0;
                //fall through
            
            case WM_SIZE:
                if(message=WM_SIZE)
                {
                    cxClient=LOWORD(lParam);
                    cyClient=HIWORD(lParam);
                }
                //calculate scrolling rectangle
                rectScroll.left=0;
                rectScroll.right=cxClient;
                rectScroll.top=cyChar;
                rectScroll.bottom=cyChar * (cyClient / cyChar);
                
                InvalidateRect(hwnd, NULL, TRUE);
                return 0;
                
            case WM_KEYDOWN:
            case WM_KEYUP:
            case WM_CHAR:
            case WM_DEADCHAR:
            case WM_SYSKEYDOWN:
            case WM_SYSKEYUP:
            case WM_SYSCHAR:
            case WM_SYSDEADCHAR:
                //rearrange storage array
                for(i=cLinesMax-1; i>0; i--)
                {
                    pmsg[i]=pmsg[i-1];
                }
                //store new message
                pmsg[0].hwnd=hwnd;
                pmsg[0].message=message;
                pmsg[0].wParam=wParam;
                pmsg[0].lParam=lParam;
                
                cLines=min(cLines + 1, cLinesMax);
                //scroll up the display
                ScrollWindow(hwnd, 0, -cyChar, &rectScroll, &rectScroll);
                break; //i.e., call DefWindowProc so Sys Messages work
            
            case WM_PAINT:
                hdc=BeginPaint(hwnd, &ps);
                
                SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
                SetBkMode(hdc, TRANSPARENT);
                TextOut(hdc, 0, 0, szTop, lstrlen(szTop));
                TextOut(hdc, 0, 0, szUnd, lstrlen(szUnd));
                
                for(i=0; i<min(cLines, cyClient/cyChar-1); i++)
                {
                    iType = pmsg[i].message == WM_CHAR ||
                            pmsg[i].message == WM_SYSCHAR ||
                            pmsg[i].message == WM_DEADCHAR ||
                            pmsg[i].message == WM_SYSDEADCHAR;
                    
                    GetKeyNameText(pmsg[i].lParam, szKeyName, sizeof(szKeyName)/sizeof(TCHAR));
                    
                    TextOut(hdc, 0, (cyClient/cyChar - 1 - i) * cyChar, szBuffer,
                            wsprintf(szBuffer, szFormat[iType],
                                     szMessage[pmsg[i].message - WM_KEYFIRST],
                                     pmsg[i].wParam,
                                     (PTSTR)(iType ? TEXT(" ") : szKeyName),
                                     (TCHAR)(iType ? pmsg[i].wParam : ' '),
                                     LOWORD(pmsg[i].lParam),
                                     HIWORD(pmsg[i].lParam) & 0xFF,
                                     0x01000000 & pmsg[i].lParam ? szYes : szNo,
                                     0x20000000 & pmsg[i].lParam ? szYes : szNo,
                                     0x40000000 & pmsg[i].lParam ? szDown : szUp,
                                     0x80000000 & pmsg[i].lParam ? szUp : szDown));
                }
                EndPaint(hwnd, &ps);
                return 0;
                
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Thanks again!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It is a good idea to post the error message when you have a problem.
    Code:
               pmsg = malloc(cLinesMax * sizeof(MSG));
    In this case, it looks like you are compiling C code as C++. You can either compile it as C or you can cast the malloc call as required by C++:
    Code:
               pmsg = (PMSG) malloc(cLinesMax * sizeof(MSG));

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks! Now its compiling fine. I was going to try compiling it as C (the book i have has all the source code in C, but i thought that it would compile fine in C++) Thanks again!

    PS - The program wont run, it gives me an error message when i try to run it. I also noticed that when i use a program from the book for scroll bars they either dont work, or dont work correctly. I cant figure out why. Perhaps since they are in C they should be compiled in C? Thanks again!

    PPS - Im running Windows XP
    Last edited by Junior89; 01-14-2006 at 01:06 AM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Quelle sorte de "Erreur Message"? We would love to know.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Die Fehlermeldung sagt "KeyView1.exe has encountered a problem and needs to close. We are sorry for the inconvience."

    Then it says: Debug, Send Error Report, or Don't Send.

    PS-Why are we speaking in different languages? lol
    Last edited by Junior89; 01-14-2006 at 06:06 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 12:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21