连接到 BITS 服务

若要连接到 BITS 系统服务,请创建 BackgroundCopyManager 对象的实例,如以下示例所示。 BITS 系统服务是在实现后台传输功能的客户端计算机上运行的 Windows 系统服务。

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <bits.h>

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

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

若要测试特定版本的 BITS,请根据要检查的版本为 BackgroundCopyManager 使用符号类标识符。 例如,若要测试 BITS 10.2,请使用 CLSID_BackgroundCopyManager10_2。

以下示例显示了如何使用其中一个符号类标识符。

  hr = CoCreateInstance(CLSID_BackgroundCopyManager5_0, NULL,
                        CLSCTX_LOCAL_SERVER,
                        IID_IBackgroundCopyManager,
                        (void**) &g_pbcm);
  if (SUCCEEDED(hr))
  {
    //BITS 5.0 is installed.
  }

使用 IBackgroundCopyManager 接口的方法创建传输作业枚举队列中的作业检索作业

BITS 要求客户端的接口代理使用 IDENTIFY 或 IMPERSONATE 模拟级别。 如果应用程序不调用 CoInitializeSecurity,则 COM 默认使用 IDENTIFY。 如果未设置正确的模拟级别,BITS 将以 E_ACCESSDENIED 失败。 如果提供了一个练习 BITS 接口的库,并且调用库的应用程序将模拟级别设置在 IDENTIFY 以下,那么将需要调用 CoSetProxyBlanket 来为调用的每个 BITS 接口设置正确的模拟级别。

在应用程序退出之前,请释放 IBackgroundCopyManager 接口指针的副本,如以下示例所示。

if (g_pbcm)
{
  g_pbcm->Release();
  g_pbcm = NULL;
}
CoUninitialize();

从 .NET 和 C# 调用 BITS BITS