Click here to Skip to main content
15,891,248 members
Articles / Desktop Programming / MFC
Article

Use BITS to improve online update of your product

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
22 Oct 2002 73.7K   16   16
Use BITS to improve online update of your product.
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();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVista problem Pin
martinassarsson20-Dec-06 0:56
martinassarsson20-Dec-06 0:56 
QuestionNeed vb.net version Pin
tomsmaily12310-May-06 4:03
tomsmaily12310-May-06 4:03 
Hai, is there any way to use BITS IN MY VB>NET app to download and upload files?D'Oh! | :doh:
GeneralWon't work from IE.... Pin
Sprog29-Nov-05 9:08
Sprog29-Nov-05 9:08 
GeneralProblem with BITS Pin
Paul Charles30-Dec-04 3:14
Paul Charles30-Dec-04 3:14 
GeneralCOM-Object Pin
Anonymous5-Oct-04 23:32
Anonymous5-Oct-04 23:32 
GeneralTips about BITS upload Pin
Member 14794507-Oct-03 23:16
Member 14794507-Oct-03 23:16 
GeneralAdv: Try the Web Update Wizard Pin
Anonymous30-Oct-02 5:55
Anonymous30-Oct-02 5:55 
GeneralRe: Adv: Try the Web Update Wizard Pin
Anonymous6-May-03 11:31
Anonymous6-May-03 11:31 
GeneralRe: Adv: Try the Web Update Wizard Pin
Anonymous6-May-03 11:47
Anonymous6-May-03 11:47 
GeneralRe: Adv: Try the Web Update Wizard Pin
davidlewinston68@hotmail.com16-Jul-04 15:30
sussdavidlewinston68@hotmail.com16-Jul-04 15:30 
Generalnot useful for online updates... Pin
Mario M.28-Oct-02 7:38
Mario M.28-Oct-02 7:38 
GeneralRe: not useful for online updates... Pin
Anonymous6-May-03 11:33
Anonymous6-May-03 11:33 
GeneralQuite an interesting topic Pin
Anthony_Yio23-Oct-02 17:09
Anthony_Yio23-Oct-02 17:09 
GeneralThis is the URL for online document Pin
phped23-Oct-02 3:21
phped23-Oct-02 3:21 
QuestionArticle missing? Pin
Andreas Saurwein22-Oct-02 23:36
Andreas Saurwein22-Oct-02 23:36 
AnswerYou're right Pin
Jonathan de Halleux23-Oct-02 0:33
Jonathan de Halleux23-Oct-02 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.