Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is FindClose call necessary for failed FindFirst?

118 views
Skip to first unread message

A

unread,
Oct 13, 2011, 9:04:12 AM10/13/11
to
I found the code all around the place that calls FindFirst, compares to 0
and if successful then calls FindClose.

OK, I understand FindClose is required for succeeded FindFirst/FindNext.

But what about if FindFirst <> 0 ?

Some code calls it, some other doesn't.

I've read somewhere that FindClose might hang the system but that was for
Win95/NT and I don't really need compatibility for anything older than Win
XP.

So should I call FindClose or not if FindFirst returns 0?

Just for the record - I am talking about Delphi version of FindClose that
takes TSearchRec as input, not the WinAPI version that wants handle as
input.


A

unread,
Oct 13, 2011, 2:32:49 PM10/13/11
to
Found the answer if anyone is looking for it - no need. Both FindFirst and
FindClose do some checks for invalid file handler and call to FindClose is
optional if FindFirst fails.

From Findfirst source:

if F.FindHandle <> INVALID_HANDLE_VALUE then
begin
Result := FindMatchingFile(F);
if Result <> 0 then FindClose(F);
end
else
Result := GetLastError;
...

From FindClose source:

if F.FindHandle <> INVALID_HANDLE_VALUE then
...


Jamie

unread,
Oct 13, 2011, 7:59:43 PM10/13/11
to
You only use FindClose if the FindFirst succeeds and use that handle
returned in all following functions related, including the findclose.

Even if you were to use FindCLose on an invalidhandle, it would
ignore it anyway.

The Delphi implementation of it stores the system handle in the search
record, so in this case, you call FindCLose in all cases to be sure when
doing the local layer function, over the API call.

In either case, calling the FindCLose will just insure proper closing
of the handle, if it happens to be valid.

Jamie


0 new messages