-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuc_DownloadBits.pas
158 lines (133 loc) · 4.08 KB
/
uc_DownloadBits.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
unit uc_DownloadBits;
interface
uses
ExtActns;
type
TDownloadBits = class
public
class procedure DownloadForground(ziel, downloadurl: WideString; DownloadFeedback:TDownloadProgressEvent);
class procedure DownloadBackground(ziel, downloadurl, ExeName, Params: WideString);
class procedure CompleteJob(JobId: WideString);
end;
implementation
uses
ComObj, ActiveX, SysUtils,
JwaBits, JwaBits1_5, Windows;
{ TDownloadBits }
class procedure TDownloadBits.CompleteJob(JobId: WideString);
var
bi: IBackgroundCopyManager;
job: IBackgroundCopyJob;
g: TGuid;
begin
bi:=CreateComObject(CLSID_BackgroundCopyManager) as IBackgroundCopyManager;
g:=StringToGUID(jobid);
bi.GetJob(g,job);
job.Complete();
end;
class procedure TDownloadBits.DownloadBackground(ziel, downloadurl,
ExeName, Params: WideString);
var
bi: IBackgroundCopyManager;
job: IBackgroundCopyJob;
job2: IBackgroundCopyJob2;
jobId: TGUID;
r: HRESULT;
begin
bi:=CreateComObject(CLSID_BackgroundCopyManager) as IBackgroundCopyManager;
r:=bi.CreateJob('Updatedownload', BG_JOB_TYPE_DOWNLOAD, JobId, job);
if not Succeeded(r) then
raise Exception.Create('Create Job Failed');
r:=Job.AddFile(PWideChar(downloadurl), PWideChar(ziel));
if not Succeeded(r) then
raise Exception.Create('Add File Failed');
// Download starten
Job.Resume();
Params:=Params+' '+GUIDToString(jobId);
Job2 := Job as IBackgroundCopyJob2;
Job2.SetNotifyCmdLine(pWideChar(ExeName), PWideChar(Params));
Job.SetNotifyFlags(BG_NOTIFY_JOB_TRANSFERRED);
end;
class procedure TDownloadBits.DownloadForground(ziel, downloadurl: widestring; DownloadFeedback:TDownloadProgressEvent);
var
bi: IBackgroundCopyManager;
job: IBackgroundCopyJob;
er: IBackgroundCopyError;
jobId: TGUID;
r: HRESULT;
// Status Zeug
p: BG_JOB_PROGRESS;
statusCode: BG_JOB_STATE;
// Timer Zeug
hTimer: THandle;
DueTime: TLargeInteger;
c: boolean;
errorContextDescription,
errorDescription: LPWSTR;
begin
bi:=CreateComObject(CLSID_BackgroundCopyManager) as IBackgroundCopyManager;
r:=bi.CreateJob('Updatedownload', BG_JOB_TYPE_DOWNLOAD, JobId, job);
if not Succeeded(r) then
raise Exception.Create('Create Job Failed');
r:=job.AddFile(PWideChar(downloadurl), PWideChar(ziel));
if not Succeeded(r) then
raise Exception.Create('Add File Failed');
job.SetMinimumRetryDelay(1);
job.SetPriority(BG_JOB_PRIORITY_FOREGROUND);
job.SetNoProgressTimeout(1);
// Download starten
job.Resume();
DueTime:=-10000000;
hTimer:=CreateWaitableTimer(nil, false, 'EinTimer');
try
SetWaitableTimer(hTimer, DueTime, 1000, nil, nil, false);
while True do
begin
job.GetState(statusCode);
if statusCode in [BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_TRANSFERRED] then
begin
job.GetProgress(p);
if assigned(DownloadFeedback) then
begin
DownloadFeedback(nil, p.BytesTransferred, p.BytesTotal, dsDownloadingData, '', c);
if c then
break;
end;
end;
if statusCode in [BG_JOB_STATE_TRANSFERRED,
BG_JOB_STATE_ERROR,
BG_JOB_STATE_TRANSIENT_ERROR] then
break;
WaitForSingleObject(hTimer, INFINITE);
end;
finally
CancelWaitableTimer(hTimer);
CloseHandle(hTimer);
end;
case statusCode of
BG_JOB_STATE_QUEUED: ;
BG_JOB_STATE_CONNECTING: ;
BG_JOB_STATE_TRANSFERRING: ;
BG_JOB_STATE_SUSPENDED: ;
BG_JOB_STATE_ERROR,
BG_JOB_STATE_TRANSIENT_ERROR:
if Succeeded(Job.GetError(er)) then
begin
try
er.GetErrorContextDescription(LANGIDFROMLCID(GetThreadLocale()), errorContextDescription);
er.GetErrorDescription(LANGIDFROMLCID(GetThreadLocale()), errorDescription);
job.Cancel;
raise Exception.Create(errorDescription+#13#10+errorContextDescription);
finally
CoTaskMemFree(errorContextDescription);
CoTaskMemFree(errorDescription);
end;
end;
BG_JOB_STATE_TRANSFERRED: job.Complete();
BG_JOB_STATE_ACKNOWLEDGED: ;
BG_JOB_STATE_CANCELLED: ;
end;
job:=nil;
bi:=nil;
end;
end.