🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1943

Re: EnumWindows etc.

#1943
From: Ed Mulroy
Date: Sat, 09 Jun 2007 13:36
71 lines
3846 bytes
I think that EnumProc code is unnecessarily complex and is
formatted in a manner which does not help one to easily read
and understand it.

The compiler does not care about an extra space following a
comma or a blank line separating elements such as declarations,
code and if-blocks.  However people more easily read code which
has those things.

The code has several return statements.  While legal this additional
complexity is a complication when debugging.  Since several return
statments are not needed, they can be replaced with one return
statement.

The code does an allocation where one is not needed.  It declares
a 4-byte pointer and then allocates a 4-byte block of memory to
store the returned address.  If instead one merely declared the item
itself, only one 4-byte element is needed and there no longer is the
need to call GlobalAlloc, GlobalLock, GlobalUnlock and GlobalFree.

The EnumWindowsProc function is called internally by Windows and
will always receive a valid window handle.  The code does not need
to test hWnd for NULL.

The call to GetWindowThreadProcessIId with a valid window handle
will always return the process ID so need not be tested for NULL.

Because the intention is to assemble an AnsiString to use in a call
to the ListBox, an AnsiString is used.  However the code plays with
strcat to assemble a char* style string when AnsiString already has
functions to do such things.  Just assemble into an AnsiString from
the beginning using only one char* style string as a buffer.

I do not think your intention was to create a text block ending in a tab
for inclusion in the ListBox.  The tab is to separate the elements that
one can see but not to separate the end of the string from the end of
the displayed element.  This implies the tab should be added prior
to all but the first element.

Here is re-written code to illustrate these points.  I think it is a bit
easier to read and understand.  Try it.  See if it is easier for you.

  ------------------------
  bool __stdcall EnumProc(HWND hWnd, long)
  {
      unsigned long process_id;
      char buffer[128];
      const AnsiString tab_str = '\t';
      AnsiString total_str;

      GetClassName(hWnd, buffer, 95);
      total_str = buffer;

      GetWindowThreadProcessId(hWnd, &process_id);
      ultoa(process_id, buffer, 10);
      total_str = total_str + tab_str + buffer;

      if (GetWindowText(hWnd, buffer, 110)) {
          total_str = total_str + tab_str + buffer;
      }

      FormMain->ListBox1->Items->Add(total_str);
      return true;
  }
  ------------------------ 


.  Ed

> Matthew wrote in message
> news:4669fbaf$1@newsgroups.borland.com...
Technical Details
Message-ID: <466aacf2$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!feeder.news-service.com!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4668d902$1@newsgroups.borland.com> <l2ri63pmm23tsci1u4subhitr6cchmm147@4ax.com> <4669fbaf$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,859e738ad5fa0eae
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Ed Mulroy" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4668d902$1@newsgroups.borland.com> <l2ri63pmm23tsci1u4subhitr6cchmm147@4ax.com> <4669fbaf$1@newsgroups.borland.com>
Subject: Re: EnumWindows etc.
Date: Sat, 9 Jun 2007 09:36:51 -0400
Lines: 73
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 69.141.82.147
Message-ID: <466aacf2$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1181396210 69.141.82.147 (9 Jun 2007 06:36:50 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!feeder.news-service.com!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail