Click here to Skip to main content
Licence 
First Posted 22 Oct 2002
Views 57,738
Bookmarked 15 times

Use BITS to improve online update of your product

By | 22 Oct 2002 | Article
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

About the Author

Leo Wang



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVista problem Pinmembermartinassarsson0:56 20 Dec '06  
QuestionNeed vb.net version Pinmembertomsmaily1234:03 10 May '06  
GeneralWon't work from IE.... PinmemberSprog9:08 29 Nov '05  
GeneralProblem with BITS PinmemberPaul Charles3:14 30 Dec '04  
GeneralCOM-Object PinsussAnonymous23:32 5 Oct '04  
GeneralTips about BITS upload Pinsussbatata17323:16 7 Oct '03  
GeneralAdv: Try the Web Update Wizard PinsussAnonymous5:55 30 Oct '02  
GeneralRe: Adv: Try the Web Update Wizard PinsussAnonymous11:31 6 May '03  
GeneralRe: Adv: Try the Web Update Wizard PinsussAnonymous11:47 6 May '03  
GeneralRe: Adv: Try the Web Update Wizard Pinsussdavidlewinston68@hotmail.com15:30 16 Jul '04  
Generalnot useful for online updates... PinmemberMario M.7:38 28 Oct '02  
GeneralRe: not useful for online updates... PinsussAnonymous11:33 6 May '03  
GeneralQuite an interesting topic PinmemberAnthony_Yio17:09 23 Oct '02  
GeneralThis is the URL for online document Pinmemberphped3:21 23 Oct '02  
QuestionArticle missing? PinmemberAndreas Saurwein23:36 22 Oct '02  
AnswerYou're right PinmemberJonathan de Halleux0:33 23 Oct '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 23 Oct 2002
Article Copyright 2002 by Leo Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid