Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Sep : Re: Delete FTP File
| Subject: | Re: Delete FTP File |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Mon, 24 Sep 2007 16:46:23 |
"Anthoni" <nonono@nono.com> wrote in message
news:46f82b8a@newsgroups.borland.com...
> Could you have a quick look at my code and make sure I
> am doing everything correctly please? Like I said, totally new
> to WinInet and want to make sure dont have memory leaks, etc
You are not closing your WinInet handle if InternetConnect() fails, and you
are not closing the handle that FtpFindFirstFile() returns.
I suggest you make sure of try..finally and try..except blocks to help
stablize the code, ie:
function TfrmTest.zp_xFTPDelete(const aFile: string; var Err: String):
Boolean;
var
hNet, hFTP, hSearch: HINTERNET;
sRec: TWin32FindData;
begin
Result := False;
Err := '';
try
// Prepare WinInet DLL
hNet := InternetOpen('myprogram', INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
if hNet = Nil then
raise Exception.Create('Unable to get access to
WinInet.Dll');
try
// Connect to FTP
hFTP := InternetConnect(hNet, PChar('mydomain.org'), 21,
PChar('myuser'), PChar('mypass'), INTERNET_SERVICE_FTP, 0, 0);
if hFTP = nil then
raise Exception.Create('Failed to connect to host');
try
// Change to correct directory
if not FtpSetCurrentDirectory(hFTP, '/mydir/') then
raise Exception.Create('Failed to set correct
directory');
hSearch := FtpFindFirstFile(hFTP, PChar(aFile), sRec, 0,
0);
if hSearch = nil then
raise Exception.Create('Failed to locate correct
file');
InternetCloseHandle(hSearch);
if not FtpDeleteFile(hFTP, PChar(aFile)) then
raise Exception.Create('Failed to delete file');
Result := True;
finally
InternetCloseHandle(hFTP);
end;
finally
InternetCloseHandle(hNet);
end;
except
on E: Exception do
Err := E.Message;
end;
end;
Gambit
none