🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: I Can't call this function more than once?

#3518
From: Thomas Maeder [TeamB]
Date: Wed, 07 Jun 2006 17:10
76 lines
3530 bytes
"Jeff" <Jeff@nospam.com> writes:

> I've build a function that returns the handle to a window who's caption 
> starts with the String passed to it (see code below).  The problem I'm 
> having is that when I call it a second time, say in a loop, it throws an 
> EAccessViolation.  Any thoughts?

Bad luck. With a little more luck, the function would crash on the
first call already.


> HWND TfrmMain::FindWindowLike(String strWinText)
> {
>  HWND hWindow; //Window handle
>  char *ptr_chrTitle = ""; //Window title

1. This initializes a pointer to char non-const with an array of 2
char const; it only compiles because of a deprecated conversion from a
string literal to pointer to char non-const.

2. ptr_chrTitle now points to an array of two char objects that must
not be modified. Not exactly what you need, I think. Cf. below.


>
>  hWindow = FindWindow(NULL, NULL);   //Start with the first window

It's always a good idea to initialize variables in their definition,
if possible.

HWND hWindow(FindWindow(NULL,NULL));



>  do
>  {
>   SendMessage(hWindow, WM_GETTEXT, GetWindowTextLength(hWindow) + 1, 
> (LPARAM)ptr_chrTitle); //Get the window's caption

1. What happens if FindWindow() returns NULL in the initialization of
hWindow?

2. Even if hWindow happens to refer to an existing window,
ptr_chrTitle points to an array that in general will be too short for
copying a window title into it; and the array elements of this array
must under no condition be modified.


>   if (static_cast<String>(ptr_chrTitle).Pos(strWinText) == 1) //Matches! 
> "Visual SourceSafe Explorer --..."

Huh?


>    return hWindow; //Return the handle
>   else
>    hWindow = GetNextWindow(hWindow, GW_HWNDNEXT);  //Get the handle to the 
> next window
>  } while (hWindow != NULL);  //Search until we run out of windows
>
>  return hWindow; //Return the handle

If program flow reaches this line, hWindows will always be equal to
NULL, so the correct code is

return NULL;

without a comment.


This illustrates why I don't like comments that merely rehash *what*
the code does (I'm all in favor of comments that tell the human reader
*why* the code does what it does, or *why* it does it the way it does
etc.). They tend to get mindlessly copied&pasted or get out of sync in
other ways. Better write code in a way that doesn't require such a
comment.
Technical Details
Message-ID: <m2wtbsevsh.fsf@glue.ch>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4486fe87$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5a83417f22065f8d
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: I Can't call this function more than once?
References: <4486fe87$1@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Wed, 07 Jun 2006 19:10:38 +0200
Message-ID: <m2wtbsevsh.fsf@glue.ch>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:wdiT/eV9no182qmoElH9zuA1uaw=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 84.73.158.25
X-Trace: newsgroups.borland.com 1149700237 84.73.158.25 (7 Jun 2006 10:10:37 -0700)
Lines: 76
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail