Connecting to the BITS Service

To connect to the BITS service, create an instance of the BackgroundCopyManager object as shown in the following example.

#define UNICODE
#define _WIN32_WINNT  0x0500
#include <windows.h>
#include "bits.h"

//Global variable that several of the code examples in this document reference.
IBackgroundCopyManager* g_pbcm = NULL;  

//Specify the appropriate COM threading model for your application.
if (SUCCEEDED(hr))
  hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,
                        (void**) &g_pbcm);
  if (SUCCEEDED(hr))
    //Use g_pbcm to create, enumerate, or retrieve jobs from the queue.

To test for a specific version of BITS, use one of the following symbolic class identifiers:

  • For BITS 3.0, use CLSID_BackgroundCopyManager3_0.
  • For BITS 2.5, use CLSID_BackgroundCopyManager2_5.
  • For BITS 2.0, use CLSID_BackgroundCopyManager2_0.
  • For BITS 1.5, use CLSID_BackgroundCopyManager1_5.
  • For BITS 1.0, use CLSID_BackgroundCopyManager.

The following example shows how to use one of the symbolic class identifiers.

  hr = CoCreateInstance(CLSID_BackgroundCopyManager2_0, NULL,
                        (void**) &g_pbcm);
  if (SUCCEEDED(hr))
    //BITS 2.0 is installed.

Use the methods of the IBackgroundCopyManager interface to create transfer jobs, enumerate jobs in the queue, and retrieve jobs.

To retrieve a pointer to the IBitsPeerCacheAdministration interface, call the IBackgroundCopyManager::QueryInterface method. The following example shows how to get the IBitsPeerCacheAdministration interface.

  HRESULT hr = S_OK;
  IBackgroundCopyManager* pbcm = NULL;
  IBitsPeerCacheAdministration* pCacheAdmin = NULL;

  hr = pbcm>QueryInterface(__uuidof(IBitsPeerCacheAdministration), (void**)&pCacheAdmin);
  if (FAILED(hr))
    wprintf(L"pbcm->QueryInterface failed with 0x%x.\n", hr);
    goto cleanup;

BITS requires that the client's interface proxies use either the IDENTIFY or IMPERSONATE level of impersonation. If the application does not call CoInitializeSecurity, COM uses IDENTIFY by default. BITS fails with E_ACCESSDENIED if the correct impersonation level is not set. If you provide a library that exercises the BITS interfaces, and an application that calls your library sets the impersonation level below IDENTIFY, then you will need to call CoSetProxyBlanket to set the correct impersonation level for each BITS interface that you call.

Before your application exits, release your copy of the IBackgroundCopyManager interface pointer, as shown in the following example.

if (g_pbcm)
  g_pbcm = NULL;