用C++Builder怎样实现MS后台智能传送服务(BITS,Background Intelligent Transfer Service)?

imagex 2004-10-21 03:46:32
http://blog.joycode.com/uestc95/archive/2003/10/26/3907.aspx
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
HUANG_JH 2004-11-03
  • 打赏
  • 举报
回复


程序代码:

CPP文件

//-----------------------------------------------------------------------

#define _WIN32_WINNT 0x0500

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

#include <stdio.h>

#include <windows.h>

#pragma comment(lib,"ole32.lib")

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

g_BCManager = NULL;

}

//----------------------------------------------------------------------

HRESULT TForm1::InitManager()

{

HRESULT hMyResult;

hMyResult = NULL ;

hMyResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

if (SUCCEEDED(hMyResult))

{

hMyResult = CoInitializeSecurity(NULL, -1, NULL, NULL,

RPC_C_AUTHN_LEVEL_CONNECT,

RPC_C_IMP_LEVEL_IMPERSONATE,

NULL, EOAC_NONE, 0);

}

if ( SUCCEEDED(hMyResult) )

return( CoCreateInstance( __uuidof(BackgroundCopyManager),

NULL,

CLSCTX_ALL,

__uuidof(IBackgroundCopyManager),

(void**) &g_BCManager));

else

return hMyResult;

}

//----------------------------------------------------------------------

void TForm1::ReleaseManager()

{

if (g_BCManager)

{

g_BCManager->Release();

g_BCManager = NULL;

}

CoUninitialize();

}

//----------------------------------------------------------------------

HRESULT TForm1:: TransFile()

{

HRESULT hMyResult;

IBackgroundCopyJob* pjob = NULL;

BG_JOB_STATE bjs;

GUID JobId;

hMyResult = g_BCManager->CreateJob(L"Kevin1stJob", BG_JOB_TYPE_DOWNLOAD, &JobId, &pjob);

if (SUCCEEDED(hMyResult))

{

hMyResult = pjob->AddFile(L"http://bcroad.51.net/mysoft/Cdapp.zip", L"c:\\test1\\a.zip");

if ( hMyResult == S_OK )

{

pjob->Resume();

while (true)

{

hMyResult = E_INVALIDARG;

if ( pjob == NULL )

g_BCManager->GetJobA(JobId,&pjob);

hMyResult = pjob->GetState(&bjs);

if ( hMyResult == S_OK )

{

Form1->Caption = "传输中...";

if ( bjs == BG_JOB_STATE_ERROR || bjs == BG_JOB_STATE_TRANSIENT_ERROR )

{

Form1->Caption = "传输错误,恢复中...";

if (nErrCount>5) //错误次数超过5次就退出

{

Form1->Caption = "文件传输到达最大错误数,退出!";

break;

}

else

nErrCount ++;

if ( pjob == NULL )

g_BCManager->GetJobA(JobId,&pjob);

hMyResult = pjob->Resume ();

if ( hMyResult!= S_OK )

break;

}

else

if (bjs == BG_JOB_STATE_TRANSFERRED)

{

Form1->Caption = "完成!";

if ( pjob == NULL )

g_BCManager->GetJobA(JobId,&pjob);

pjob->Complete();

break;

}

}

else

{

Form1->Caption = " GetState失败,退出!";

break;

}

Sleep(200);

Application->ProcessMessages();

}

}

}

return hMyResult;

}

//----------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

//使用

nErrCount = 0 ; //错误次数统计

HRESULT hMyResult = NULL ;

hMyResult = InitManager();

if (!SUCCEEDED(hMyResult))

{

if (hMyResult == REGDB_E_CLASSNOTREG )

ShowMessage("REGDB_E_CLASSNOTREG");

else

if (hMyResult == E_OUTOFMEMORY )

ShowMessage("E_OUTOFMEMORY");

else

if (hMyResult == E_INVALIDARG )

ShowMessage("E_INVALIDARG");

else

if (hMyResult == E_UNEXPECTED )

ShowMessage("E_UNEXPECTED");

else

if (hMyResult == CLASS_E_NOAGGREGATION )

ShowMessage("CLASS_E_NOAGGREGATION");

else

ShowMessage("InitManager 失败!");

}

else

TransFile ();

ReleaseManager();

}

//-------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

Close();

}

//---------------------------------------------------------------------

h文件

//---------------------------------------------------------------------------

#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

//以下3个文件可在sdk的Include目录中找到

#include "objbase.h"

#include "strsafe.h"

#include "bits.h"

//---------------------------------------------------------------------------

class TForm1 : public TForm

{

__published: // IDE-managed Components

TButton *Button1;

TButton *Button2;

void __fastcall Button1Click(TObject *Sender);

void __fastcall Button2Click(TObject *Sender);

private: // User declarations

public: // User declarations

__fastcall TForm1(TComponent* Owner);

//自定义文件

IBackgroundCopyManager* g_BCManager ;

HRESULT TransFile ();

void ReleaseManager();

HRESULT InitManager();

int nErrCount ;

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif

bcb 5.0 + win2000 pro sp3 + SDK 编译通过。

imagex 2004-11-02
  • 打赏
  • 举报
回复
国中无人啊。

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧