Background Intelligent Transfer Service (BITS) provides the documentation and samples necessary to write client applications that transfer files (downloads or uploads) between a client and server, and monitors jobs within the transfer queue.
To use BITS is quite simple, you can add few lines to obtain this feature.
To compile the program, you need latest Platform SDK and Visual C++ 7 is highly recommended.
The source code is simple and self-documented, it demonstrates the basic use of BITS.
#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <windows.h>
#include <objbase.h>
#include <BITS.H>
#pragma comment(lib,"ole32.lib")
IBackgroundCopyManager* g_BCManager = NULL;
HRESULT InitXferManager()
{
HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (SUCCEEDED(hr))
{
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0);
}
if (!SUCCEEDED(hr))
return hr;
hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,
CLSCTX_ALL,
__uuidof(IBackgroundCopyManager),
(void**) &g_BCManager);
return hr;
}
void UninitXferManager()
{
if (g_BCManager)
{
g_BCManager->Release();
g_BCManager = NULL;
}
CoUninitialize();
}
HRESULT CreateXferJob(IBackgroundCopyJob** job)
{
HRESULT hr;
GUID JobId;
hr = g_BCManager->CreateJob(L"My1stJob", BG_JOB_TYPE_DOWNLOAD, &JobId, job);
return hr;
}
HRESULT AddFile()
{
HRESULT hr;
IBackgroundCopyJob* job;
BG_JOB_STATE bjs;
hr = CreateXferJob(&job);
if (!SUCCEEDED(hr))
return hr;
hr = job->AddFile(L"http://news.sina.com.cn/c/2002-10-22/0929777144.html", L"c:\\2");
job->Resume();
while (TRUE)
{
job->GetState(&bjs);
if (bjs != BG_JOB_STATE_TRANSFERRING && bjs != BG_JOB_STATE_CONNECTING) {
printf("Transferring...\n");
break;
}
Sleep(100);
}
job->Complete();
printf("Done!\n");
return hr;
}
void main()
{
HRESULT hr;
hr = InitXferManager();
if (!SUCCEEDED(hr)) return;
AddFile();
UninitXferManager();
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.