dirkil2
asked on
Delphi Win Inet: How to make this code Delphi 7 code work in Delphi 2010
Hi there!
I have Delphi code that uses the WinInet library (see attachment) to download a file from a url. This used to work in Delphi 7. When I now compile it with Delphi 2010 it no longer works even when I exchange String with AnsiString and Char with AnsiChar.
There must something else be wrong. Can someone modify this code so that it works under Delphi 2010?
Any help is very much appreciated.
Regards,
Dirk
I have Delphi code that uses the WinInet library (see attachment) to download a file from a url. This used to work in Delphi 7. When I now compile it with Delphi 2010 it no longer works even when I exchange String with AnsiString and Char with AnsiChar.
There must something else be wrong. Can someone modify this code so that it works under Delphi 2010?
Any help is very much appreciated.
Regards,
Dirk
function THttpManager.GetHTML(AUrl: string): string;
var
databuffer : array[0..4095] of char;
ResStr : string;
hSession, hfile: hInternet;
dwindex,dwcodelen,dwread,dwNumber: cardinal;
dwcode : array[1..20] of char;
res : pchar;
Str : pchar;
begin
ResStr:='';
if pos('http://',lowercase(AUrl))=0 then
AUrl:='http://'+AUrl;
hSession:=InternetOpen('InetURL:/1.0',
INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil,
0);
if assigned(hsession) then
begin
hfile:=InternetOpenUrl(
hsession,
pchar(AUrl),
nil,
0,
INTERNET_FLAG_RELOAD, // damit nicht aus dem Cache gelesen wird
0);
dwIndex := 0;
dwCodeLen := 10;
HttpQueryInfo(hfile,
HTTP_QUERY_STATUS_CODE,
@dwcode,
dwcodeLen,
dwIndex);
res := pchar(@dwcode);
dwNumber := sizeof(databuffer)-1;
if (res ='200') or (res ='302') then
begin
while (InternetReadfile(hfile,
@databuffer,
dwNumber,
DwRead)) do
begin
if dwRead =0 then
break;
databuffer[dwread]:=#0;
Str := pchar(@databuffer);
resStr := resStr + Str;
end;
end
else
ResStr := 'Status:'+res;
if assigned(hfile) then
InternetCloseHandle(hfile);
end;
InternetCloseHandle(hsession);
Result := resStr;
end;
change type string to AnsiString
and PChar to PAnsiChar
What is the problem now? It doesn't work? You get corrupted files? You get empty files? You get an access violation? Please give some more details, thanks.
You could try to change the data buffer from Char (which is now 2 bytes) to Byte.
You could try to change the data buffer from Char (which is now 2 bytes) to Byte.
ASKER
I wrote that I tried that already without success.
ASKER
@ebob
The call to InternetReadfile fails.
The call to InternetReadfile fails.
ASKER
Maybe someone could try to compile this code under Delphi 2010 and try to load a file. It's an easy test. I would really like to hear about the results.
ow, sorry, i missed the conversion bits
i tried it it and it fails here for me :
databuffer[dwread]:=#0;
still looking why
i tried it it and it fails here for me :
databuffer[dwread]:=#0;
still looking why
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I can call the resulting GetURL.exe with http://www.drbob42.com/delphi/home.htm as argument for example.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
nice going bob,
i got 8192 as amount of bytes read from the internetReadfile
and was puzzled at first why the array was only half the size ...
but you pinned it
i got 8192 as amount of bytes read from the internetReadfile
and was puzzled at first why the array was only half the size ...
but you pinned it
I still use WinINet today, as well as Unicode strings and data buffers, so I knew where to look for ;-)
ASKER
You guys are great! I plugged ebob's code into my program and it worked straight away.
ebob, I hope you don't mind if I award a few points to Geert as well.
ebob, I hope you don't mind if I award a few points to Geert as well.
No, that's OK. And you can call me Bob or Dr.Bob (eBob42 is my company name) ;-)