This article was written by Benjamin aka chaosben. He kindly agreed to publish it here. You can also publish articles here. Mail us @ mail(-at+)delphi-jedi.net

Getting started with BITS

BITS, the Background Intelligent Transfer Service, provides an easy way to
download/upload files from/to the network/internet. You may have heard of this
service in the context of the automatic Windows Update and you are right -
Microsoft uses it to get the files on the computer.

Ok, I know, you want to start coding. Thats why our first step is to add the
units ActiveX, ComObj and JwaBits to the uses clause of our project.
In the next step, we have ensure, that the COM library is initialized for the
current thread. It sounds hard, but it is easy (thanks to Sebastian):

  1. initialization
  2.   CoInitFlags := COINIT_APARTMENTTHREADED;

Just write this part at the end of your unit before the final “end.”

Fine, now we have a nice environment for messing around with BITS. Let’s
grab an interface to a BackgoundCopyMananger. After declaring a variable
(I call it “FManager”) of type IBackgroundCopyManager, it can be
initialized as follow:

  1. Res := CoCreateInstance(CLSID_BackgroundCopyManager,
  2.                         nil,
  3.                         CLSCTX_LOCAL_SERVER,
  4.                         IID_IBackgroundCopyManager,
  5.                         FManager);
  6. if not Succeeded(Res) then
  7.   raise Exception.Create(‘Can not create BackgroundCopyManager.
  8.                          Is the service running?’);

Don’t forget to free the interface at the end of the program. Just write:

  1. procedure TForm1.FormDestroy(Sender: TObject);
  2. begin
  3.   if Assigned(FMananger) then
  4.     FMananger := nil;
  5. end;

As our first test with BITS, we want to see, which jobs are currently running.
I dropped a listbox and timer on a form and filled the OnTimer-Event with this code:

  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. var
  3.   Job : IBackgroundCopyJob;
  4.   Jobs : IEnumBackgroundCopyJobs;
  5.   Res : HRESULT;
  6.   DisplayName : PWideChar;
  7.   Fetched : ULong;
  8. begin
  9.   ListBox1.Clear;
  10.  
  11.   //Try to get all jobs (hopefully we have the rights)
  12.   Res := FManager.EnumJobs(BG_JOB_ENUM_ALL_USERS, Jobs);
  13.  
  14.   //maybe the right is missing … try to get just our jobs
  15. if not Succeeded(Res) then
  16.     Res := FManager.EnumJobs(0, Jobs);
  17.  
  18.   //nothing is ok … better go sleeping  :)
  19.   if not Succeeded(Res) then
  20.     raise Exception.Create(‘Can not enum BackgroundCopyJobs’);
  21.  
  22.   //walk through the enum of jobs …
  23.   while Succeeded(Jobs.Next(1, Job, @Fetched)) and (Fetched = 1) do
  24.   begin
  25.     //… and try to get the name of each job
  26.     if Succeeded(Job.GetDisplayName(DisplayName)) then
  27.     begin
  28.       ListBox1.Items.Add(DisplayName);
  29.  
  30.       //its important to free the memory, BITS allocated for us
  31.       CoTaskMemFree(DisplayName);
  32.     end;
  33.  
  34.     //release the job
  35.     Job := nil;
  36.  
  37.   end;
  38. end;

Ok, that’s it. Play around with the interface to get in touch with them.
Maybe another article will follow.

Benjamin aka chaosben
www.TheUnknownOnes.net

Part 2

You can get to the second part of BITS here.

Download

The new BITS header conversion units will be available through the JEDI API Release. They actually are not available. Thus we provide them as download here.

Notice:
The code examples above do not use these new units, however. Thus you can just include JwaBits.pas (or JwaWindows).

JwaBITS Download

Send post as PDF to www.pdf24.org
convert this post to pdf.