大网数据广告活动
网络与域名解析

Connecting to the BITS Service

· 52CE 我爱测

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

To connect to the BITS system service, create an instance of the BackgroundCopyManager object as shown in the following example. The BITS system service is the Windows system service running on the client computer that implements the background transfer capability.

#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.
  }
}

To test for a specific version of BITS, use a symbolic class identifier for the BackgroundCopyManager based on the version you want to check. For example, to test for BITS 10.2, use CLSID_BackgroundCopyManager10_2.

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

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

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

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->Release();
  g_pbcm = NULL;
}
CoUninitialize();

Related topics

Calling into BITS from .NET and C# for BITS

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 11/29/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。