Delphi - 需要XE2代码到Delphi7。使用wininet下载文件 [英] Delphi - XE2 code to Delphi7 needed. Using wininet to download a file
问题描述
注意:我只想使用wininet,而不是urlmon-urldownloadtofile。
Note: I only want to use wininet, not urlmon-urldownloadtofile.
嗯,我有以下代码在XE2中完美的下载文件: / p>
Well, I have the following code which works perfectly in XE2 to download a file:
procedure DownloadFile(URL: string; Path: string);
const
BLOCK_SIZE = 1024;
var
InetHandle: Pointer;
URLHandle: Pointer;
FileHandle: Cardinal;
BytesRead: Cardinal;
DownloadBuffer: Pointer;
Buffer: array [1 .. BLOCK_SIZE] of byte;
BytesWritten: Cardinal;
begin
InetHandle := InternetOpen(PWideChar(URL), 0, 0, 0, 0);
URLHandle := InternetOpenUrl(InetHandle, PWideChar(URL), 0, 0, 0, 0);
FileHandle := CreateFile(PWideChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, 0,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
DownloadBuffer := @Buffer;
repeat
InternetReadFile(URLHandle, DownloadBuffer, BLOCK_SIZE, BytesRead);
if not WriteFile(FileHandle, DownloadBuffer, BytesRead, BytesWritten, 0) or
(BytesWritten <> BytesRead) then
RaiseLastOSError;
until BytesRead < BLOCK_SIZE;
CloseHandle(FileHandle);
InternetCloseHandle(URLHandle);
InternetCloseHandle(InetHandle);
end;
上述代码的学分转到 jachguate 。他编辑我的代码来纠正它,我感谢他。
The above code's credits go to jachguate. He edited my code to correct it, and I thank him for that.
现在,这段代码只能在Delphi XE2下正常工作。我试图在Delphi 7下使用它,它不能正常工作。似乎在文件中存储相同的行或字节序列超过几次。
Now, this code only works correctly under Delphi XE2. I try to use it under Delphi 7 and it is not working correctly. It seems to store the same "line" or "byte sequence" to the file over and over a few times.
以下是上述代码的两个重新设置已经考虑在Delphi 7中使用 - 这两个都不正常。
The following are the two reformations of the above code I have attemted to use in Delphi 7 - neither of which worked properly.
procedure DownloadFile(URL: string; Path: string);
const
BLOCK_SIZE = 1024;
var
InetHandle: Pointer;
URLHandle: Pointer;
FileHandle: Cardinal;
BytesRead: Cardinal;
DownloadBuffer: Pointer;
Buffer: array [1 .. BLOCK_SIZE] of byte;
BytesWritten: Cardinal;
begin
InetHandle := InternetOpen(PChar(URL), 0, 0, 0, 0);
URLHandle := InternetOpenUrl(InetHandle, PChar(URL), 0, 0, 0, 0);
FileHandle := CreateFile(PChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, 0,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
DownloadBuffer := @Buffer;
repeat
InternetReadFile(URLHandle, DownloadBuffer, BLOCK_SIZE, BytesRead);
if not WriteFile(FileHandle, DownloadBuffer, BytesRead, BytesWritten, 0) or
(BytesWritten <> BytesRead) then
RaiseLastOSError;
until BytesRead < BLOCK_SIZE;
CloseHandle(FileHandle);
InternetCloseHandle(URLHandle);
InternetCloseHandle(InetHandle);
end;
procedure DownloadFile(URL: string; Path: string);
const
BLOCK_SIZE = 1024;
var
InetHandle: Pointer;
URLHandle: Pointer;
FileHandle: Cardinal;
BytesRead: Cardinal;
DownloadBuffer: Pointer;
Buffer: array [1 .. BLOCK_SIZE] of byte;
BytesWritten: Cardinal;
begin
InetHandle := InternetOpen(PAnsiChar(URL), 0, 0, 0, 0);
URLHandle := InternetOpenUrl(InetHandle, PAnsiChar(URL), 0, 0, 0, 0);
FileHandle := CreateFile(PAnsiChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, 0,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
DownloadBuffer := @Buffer;
repeat
InternetReadFile(URLHandle, DownloadBuffer, BLOCK_SIZE, BytesRead);
if not WriteFile(FileHandle, DownloadBuffer, BytesRead, BytesWritten, 0) or
(BytesWritten <> BytesRead) then
RaiseLastOSError;
until BytesRead < BLOCK_SIZE;
CloseHandle(FileHandle);
InternetCloseHandle(URLHandle);
InternetCloseHandle(InetHandle);
end;
只有编辑的内容才是数据类型转换。例如使用PChar,并使用了两个PAnsiChar。
Only thing edited was the data type conversions. Example one "PChar" was used, and example two "PAnsiChar" was used.
推荐答案
您需要使用 ^
char来取消引用指针 DownloadBuffer
,因此要修复您的方法,只需替换此代码
You are getting garbage in the file because you need dereference the pointer DownloadBuffer
using the ^
char, so to fix your method just replace this code
WriteFile(FileHandle, DownloadBuffer, BytesRead, BytesWritten, 0)
这个
WriteFile(FileHandle, DownloadBuffer^, BytesRead, BytesWritten, 0)
。
这篇关于Delphi - 需要XE2代码到Delphi7。使用wininet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!