Thread: list of HWND's not working

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    list of HWND's not working

    Well, I'm using Borland C++ Builder, and I don't think that would have much to do with my problem. At the moment, my program works fine on my computer, but not on a friend's. He get's Abnormal Program Termination whenever he hits a button that runs this function:

    Code:
    //These are global variables:
    HWND hWnd[256];
    HWND hCur;
    AnsiString asHcur;
    int iHwnd = 0;
    
    void RefreshHWND()
    {
            iHwnd = 0;
            frmMain->lstProcs->Clear();
            HWND hFirst, hNext, hTemp;
    
            hFirst = GetForegroundWindow();
    
            AnsiString asWinfo;
            char szWintit[256];
            GetWindowText(hFirst, szWintit, GetWindowTextLength(hFirst) + 1);
            asWinfo.sprintf("%08X - %s", hFirst, szWintit);
    
            frmMain->lstProcs->AddItem(asWinfo, NULL);
            hTemp = hFirst;
            hWnd[iHwnd] = hFirst;
    
            while ((hNext = GetNextWindow(hTemp, GW_HWNDNEXT)) != NULL)
            {
                    iHwnd++;
                    GetWindowText(hNext, szWintit, GetWindowTextLength(hNext) + 1);
                    asWinfo.sprintf("%08X - %s", hNext, szWintit);
                    frmMain->lstProcs->AddItem(asWinfo, NULL);
                    hTemp = hNext;
                    hWnd[iHwnd] = hNext;
            }
    }
    Is there something wrong with my code that uses too much memory?
    (the edit I did was to add in spacing for easier reading)
    Last edited by neandrake; 03-22-2003 at 11:28 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Re: list of HWND's not working

    Originally posted by neandrake
    Code:
    void RefreshHWND()
    {
            iHwnd = 0;
            frmMain->lstProcs->Clear();
            HWND hFirst, hNext, hTemp;
            ...
    }
    For improved readability make sure you've put all your declarations before your code. You wouldn't make global declarations between functions, would you?

    As for your problem, you might wanna get a debugger to hand and check the values for szWintit, etc.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    The third parameter for GetWindowText is the size, in characters, of the buffer (in this case, for szWintit, 256), not the actual length of the window text, because it may well be that the window's text length is greater than the size of the buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM