Geeks With Blogs
Blog Moved to http://podwysocki.codebetter.com/ Blog Moved to http://podwysocki.codebetter.com/
Update:  Put a comment in if you want the C# version of this code and I will get it to you
 
In my previous lesson, I gave a brief overview of the Background Intelligent Transfer Service.  Today we will dig a bit deeper into some of the API as well as give reasons why I chose Managed C++ over a C# implementation.
 
First off, there was plenty of reading to do from the last time.  If you want to read more about the uses of BITS as well as the key technologies behind it, please check out this link: http://msdn.microsoft.com/library/en-us/bits/bits/about_bits.asp.  Read up?  Good...  Let's continue!
 
Now I will discuss why I chose to do a Managed C++ version of this instead of a C# implementation.  As you will note with your installation of the Microsoft Windows Server 2003 SP1 SDK that I indicated was a required download, there are bits header files and libraries and all sorts of goodies.  Look in the Include directory, which in my instance is C:\Program Files\Microsoft Platform SDK\Include.  In this folder you will notice a bits2_0.h and a bits2_0.idl. 
 
There are several examples out there especially on the MSDN about using the MIDL tool to create the type library and then use the tlbimp tool in order to create the .NET wrapper library.  This approach was all well and good for BITS 1.0 which included download capability only and no ability to set credentials.  Of course the argument can be made to wrap 99% of the code in C# and only use C++ for the extra stuff such as setting credentials and getting upload reply data.  The C# only version will not work because C# does not support struct UNIONS which is essential to setting the username and password.  I've tried this approach in the past, but the better performer on mission critical applications such as this made it better to stick with just one language.  
 
On the MSDN, the article wraps BITS 1.0 functionality in a VB.NET solution  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwxp/html/WinXP_BITS.asp.   There is another example out there that uses the BITSAdmin tool to do uploads which is located here:  http://www.codeproject.com/vb/net/Bitsup.asp.  I wanted full functionality that neither could really provide, so I took lessons learned from each and went on my way.
 
 We will now dive right into the Reference material provided on this site.  Since there is so much material to cover, I will only cover the top level information at this moment.  Please keep in mind that we are creating a "Managed Wrapper" for BITS, so don't panic when we start working with native type conversion.  There will be no need to Google the results as I will include how to do things right here. 
 
Let us take a brief look at the BITS Interfaces.  Note that these interfaces all implement IUnknown and return HRESULTs with each method to indicate success or failure.  Today's lesson will cover briefly the heart of it all, the IBackgroundCopyManager.  This interface allows the implementer to add a job, get a job by an identifier, get all jobs for the current user or if an administrator, get all jobs for that machine. 
 
Now we will look at each method in some detail.
 
The CreateJob method gives the implementer the ability to create a job, given a null terminated string, and a job type and returns a Guid and an IBackgroundCopyJob.  The signature is as follows:
HRESULT CreateJob(
  LPCWSTR pDisplayName,
  BG_JOB_TYPE Type,
  GUID* pJobID,
  IBackgroundCopyJob** ppJob
);

The EnumJobs method gives the implementer the ability to get an enumerator to iterate through a collection of IBackgroundCopyJobs.  This has the parameter to be able to see all jobs for all users on the current machine if the user is an administrator.  The signature is as follows:
HRESULT EnumJobs(
  DWORD dwFlags,
  IEnumBackgroundCopyJobs** ppEnumJobs
);
 
The GetJob method provides the ability to get a particular IBackgroundCopyJob by the Guid.  We will worry about translating this GUID into a System.Guid later.  The signature is as follows:
HRESULT GetJob(
  REFGUID JobID,
  IBackgroundCopyJob** ppJob
);
 
Lastly, the GetErrorDescription method provides the ability to get the error message for a particular HRESULT based upon the LCID which is available from the System.Threading.Thread.  The signature of this method is:
HRESULT GetErrorDescription(
  HRESULT hResult,
  DWORD LanguageId,
  LPWSTR* ppErrorDescription
);
 
Until the next lesson, keep reading and I will then show you what I created in terms of the header file wrapper for the IBackgroundCopyManager as well as starting to get into the wrapping of the IBackgroundCopyJob.
 
 
Posted on Wednesday, May 10, 2006 7:33 AM Microsoft , Background Intelligent Transfer Service , .NET , Windows | Back to top


Comments on this post: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper

# re: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper
Requesting Gravatar...
hi there
thanks very much for these great lessons.

i have problem with implementing BITS as my cliend doesn't have Windows Server 2003 and don't want to install it at all. so do you think there is a way around that make me to use BITS with other server.

Thnaks
Left by John Abbas on May 24, 2006 6:22 AM

# re: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper
Requesting Gravatar...
hi there
thanks very much for these great lessons.

i have problem with implementing BITS as my cliend doesn't have Windows Server 2003 and don't want to install it at all. so do you think there is a way around that make me to use BITS with other server.

Thnaks
Left by John Abbas on May 24, 2006 7:52 AM

# re: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper
Requesting Gravatar...
This wrapper as written can be run on anything going back to Windows 2000 SP3 and XP. BITS will work just fine in that case.
Left by Matthew Podwysocki on May 24, 2006 2:11 PM

# re: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper
Requesting Gravatar...
Hi,

I want to download this source. Can u pls tell me from where should Idownload this sorce code?

Thanks
Left by Guest on Nov 13, 2006 3:00 AM

# re: Day 2 of the Background Intelligent Transfer Service (BITS) Managed Wrapper
Requesting Gravatar...
some one can help me to store the IBackgroundcopycallBack memebers information using WCF and SQL Server

Like Once Creating the job and add the jobs to BITS then BITS APIs can store the messages to SQL Server Database some table (Like JobsInfor Table: JobName,JobType,JobPriority,JobStatus,JobStartTime and JobEndTime)

please send me the solution to madhumuaug01@gmail.com

Thanks and awaiting for responsefrom you guys....
Left by madhu on Sep 09, 2009 4:26 PM

Your comment:
 (will show your gravatar)


Copyright © Matthew Podwysocki | Powered by: GeeksWithBlogs.net