Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Nov : Re: Problem Using ftp with winInet
| Subject: | Re: Problem Using ftp with winInet |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Thu, 8 Nov 2007 12:26:40 |
"Joe H" <joedotah@att.net> wrote in message
news:47335323@newsgroups.borland.com...
> if InternetReadFile(f,pchar(result[ndx]),BlockSize,AmtRead)
That is not the correct way to get a pointer to a specific character offset.
Try this instead:
if InternetReadFile(f, @result[ndx], BlockSize, AmtRead)
Alternatively, use a pointer variable instead:
ptr := PChar(Result);
repeat
InternetReadFile(f, ptr, BlockSize, AmtRead)
//...
Inc(ptr, AmtRead);
//...
You are using the first type when calling InternetWriteFile(), so you should
be using it for InternetReadFile() as well.
You are also not exiting your loops if InternetReadFile() and
InternetWriteFile() fail for any reason.
You are also passing your source data instead of the filename to
FtpOpenFile() in PutFile().
Also, your code is going to break when CodeGear changes 'String' to Unicode
next year, so you may as well adjust your code for that as well.
Try the following (untested):
function tFtpAccess.GetFile(RemoteName: String): AnsiString;
var
f: HInternet;
size: Integer;
ndx: Integer;
amtRead: DWORD;
abort: Boolean;
data: String;
begin
Result := '';
abort := False;
if fConnected then
begin
f := ftpOpenfile(fConnect, PChar(RemoteName), GENERIC_READ,
FTP_TRANSFER_TYPE_BINARY, 0);
if Assigned(f) then
try
size := ftpGetFileSize(f, nil);
if size > 0 then
begin
SetLength(data, size);
FillChar(data[1], size, 0);
ndx := 0;
repeat
amtRead := 0;
if not InternetReadFile(f, @data[ndx+1], BlockSize,
amtRead) then Exit;
if amtRead = 0 then Break;
Inc(ndx, amtRead);
if Assigned(fOnProgress) then
begin
fOnProgress(WebName, ndx, size, abort);
if abort then Exit;
end;
until ndx >= size;
SetLength(data, ndx);
Result := data;
finally
InternetCloseHandle(f);
end;
end;
end;
end;
function tFtpAccess.PutFile(Source: AnsiString; RemoteName: String):
Boolean;
var
f: HInternet;
size: Integer;
ndx: Integer;
toWrite: DWORD;
written: DWORD;
abort: Boolean;
begin
Result := False;
abort := False;
size := Length(Source);
if fConnected and (size > 0) then
begin
f := ftpOpenfile(fConnect, PChar(RemoteName), GENERIC_WRITE,
FTP_TRANSFER_TYPE_BINARY, 0);
if Assigned(f) then
try
ndx := 0;
repeat
toWrite := size - ndx;
if toWrite > BlockSize then
toWrite := BlockSize;
written := 0;
if not InternetWriteFile(f, @Source[ndx+1], toWrite,
Written) then Exit;
Inc(ndx, Written);
if Assigned(fOnProgress) then
begin
fOnProgress(RemoteName, ndx, size, abort);
if abort then Exit;
end;
until ndx >= size;
Result := True;
finally
InternetCloseHandle(f);
end;
end;
end;
Gambit