連線 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 要求用戶端的介面 Proxy 使用 IDENTIFY 或 IMPERSONATE 模擬層級。 如果應用程式未呼叫 CoInitializeSecurity,COM 預設會使用 IDENTIFY。 如果未設定正確的模擬層級,BITS 會失敗並E_ACCESSDENIED。 如果您提供一個練習 BITS 介面的連結庫,而呼叫連結庫的應用程式會設定識別下方的模擬層級,則您必須呼叫 CoSetProxyBlanket ,以針對您所呼叫的每個 BITS 介面設定正確的模擬層級。

在應用程式結束之前,請釋放IBackgroundCopyManager 介面指標的複本,如下列範例所示。

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

從 .NET 和 C# 呼叫 BITS 的 BITS