Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Jeff
Wed, 07 Jun 2006 16:27
I Can't call this function more than once?
Author: Jeff
Date: Wed, 07 Jun 2006 16:27
Date: Wed, 07 Jun 2006 16:27
26 lines
1816 bytes
1816 bytes
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?
HWND TfrmMain::FindWindowLike(String strWinText)
{
HWND hWindow; //Window handle
char *ptr_chrTitle = ""; //Window title
hWindow = FindWindow(NULL, NULL); //Start with the first window
do
{
SendMessage(hWindow, WM_GETTEXT, GetWindowTextLength(hWindow) + 1,
(LPARAM)ptr_chrTitle); //Get the window's caption
if (static_cast<String>(ptr_chrTitle).Pos(strWinText) == 1) //Matches!
"Visual SourceSafe Explorer --..."
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
}
Re: I Can't call this function more than once?
Author: Jeff
Date: Wed, 07 Jun 2006 16:52
Date: Wed, 07 Jun 2006 16:52
46 lines
2360 bytes
2360 bytes
Awesome, thanks, I didn't realize that.
"Boba" <Boba@somewhere.world> wrote in message
news:448701e9@newsgroups.borland.com...
> "Jeff" <Jeff@nospam.com> wrote in message
> news:4486fe87$1@newsgroups.borland.com...
>> 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?
>>
>> HWND TfrmMain::FindWindowLike(String strWinText)
>> {
>> HWND hWindow; //Window handle
>> char *ptr_chrTitle = ""; //Window title
>>
>
> how much memory you think you have pointed by ptr_chrTitle?
>
>> hWindow = FindWindow(NULL, NULL); //Start with the first window
>>
>> do
>> {
>> SendMessage(hWindow, WM_GETTEXT, GetWindowTextLength(hWindow) + 1,
>> (LPARAM)ptr_chrTitle); //Get the window's caption
>
> make sure you don't exceed the memory size pointed by ptr_chrTitle.
>
>> if (static_cast<String>(ptr_chrTitle).Pos(strWinText) == 1)
> //Matches!
>> "Visual SourceSafe Explorer --..."
>> 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
>> }
>>
>>
>
>
Re: I Can't call this function more than once?
Author: Thomas Maeder [TeamB]
Date: Wed, 07 Jun 2006 17:10
Date: Wed, 07 Jun 2006 17:10
76 lines
3530 bytes
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.
Re: I Can't call this function more than once?
Author: Boba
Date: Wed, 07 Jun 2006 18:48
Date: Wed, 07 Jun 2006 18:48
40 lines
2099 bytes
2099 bytes
"Jeff" <Jeff@nospam.com> wrote in message
news:4486fe87$1@newsgroups.borland.com...
> 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?
>
> HWND TfrmMain::FindWindowLike(String strWinText)
> {
> HWND hWindow; //Window handle
> char *ptr_chrTitle = ""; //Window title
>
how much memory you think you have pointed by ptr_chrTitle?
> hWindow = FindWindow(NULL, NULL); //Start with the first window
>
> do
> {
> SendMessage(hWindow, WM_GETTEXT, GetWindowTextLength(hWindow) + 1,
> (LPARAM)ptr_chrTitle); //Get the window's caption
make sure you don't exceed the memory size pointed by ptr_chrTitle.
> if (static_cast<String>(ptr_chrTitle).Pos(strWinText) == 1)
//Matches!
> "Visual SourceSafe Explorer --..."
> 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
> }
>
>
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads