C#調用BITS(Background Intellgent Transfer Service)

來源:互聯網
上載者:User
BITS(Background Intellgent Transfer Service)是微軟推出的用來實現Client和Server之間進行檔案傳輸的技術。微軟只提供了基於COM的介面,而沒有提供基於Managed 程式碼的封裝。如果想用.NET上語言(如C#)需要我們手工對COM介面進行封裝或使用第三方的封裝,BITS有多個版本,找到合適的比較麻煩。MS沒有提供TLB檔案,也沒把TLB檔案嵌入到實現BITS的dll中,也就不能用tlbimp.exe工具產生COM的封裝。如果有TLB檔案,則可以用tlbimp.exe產生COM的封裝程式集。

我在查看檔案Windows SDK的時候,發現裡麵包含了各個版本的BITS的idl檔案,midl.exe編譯idl檔案,發現產生TLB檔案,tlbimp.exe發現能產生封裝程式集,但有個問題,無論用哪個版本的idl檔案,都只能產生BITS 1.0的COM介面的封裝程式集;並且如果用bits4_0.idl產生的封裝類,在建立BackgroundCopyManager4_0Class對象時,在兩台Win7機器上實驗,都出現異常,異常資訊為:
Retrieving the COM class factory for component with CLSID {BB6DF56B-CACE-11DC-9992-0019B93A3A84} failed due to the following error: 80080005.
暫時沒找到解決辦法,我十分懷疑是MS的BUG。

4.0的不能用,就用3.0的,可只產生1.0的封裝程式集不行啊。下面對bits3_0.idl檔案進行手術進行手術:library BackgroundCopyManager3_0
{
    [
        uuid(659cdea7-489e-11d9-a9cd-000d56965251),
        helpstring("Background copy manager 3.0")
    ]
    coclass BackgroundCopyManager3_0
    {
        [default] interface IBackgroundCopyManager;
    };
    interface IBackgroundCopyJob4; 
     
    interface IBackgroundCopyJobHttpOptions;
    interface IBackgroundCopyCallback2;
    interface IBackgroundCopyFile3;
    interface IBitsPeerCacheAdministration;

}

粗體部分是我們手工添加的,目的是產生TLB檔案時,將粗體部分的interface包含進去,tlbimp.exe對這些介面進行封裝。
命令列為:midl bits3_0.idl
tlbimp bits3_0.tlb

最終產生BackgroundCopyManager3_0.dll檔案,在.NET項目中添加引用即可。如果要從低版本的介面獲得高版本的介面,需要低版本的介面調用QueryInterface,但對COM介面的封裝會屏蔽引用計數及QueryInterface。翻一翻System.Runtime.InteropServices命名空間會發現Marshal類中有個QueryInterface的方法:public static int QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv);

這個問題解決。

寫代碼測試一下:using System;
using System.Runtime.InteropServices;
using BackgroundCopyManager3_0;

namespace www.Xianfen.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundCopyManager3_0Class mgr = new BackgroundCopyManager3_0Class();
            GUID guid;
            IBackgroundCopyJob job;

            //create job
            mgr.CreateJob("job", BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, out guid, out job);

            //新版本interface的GUID.
            Guid gJob4 = new Guid("659cdeae-489e-11d9-a9cd-000d56965251");

            //擷取老版本interface的指標
            IntPtr pUnknwJob = Marshal.GetIUnknownForObject(job);
            IntPtr pUnknwJob4;

            //擷取新版本interface的指標
            int hresult = Marshal.QueryInterface(pUnknwJob, ref gJob4, out pUnknwJob4);

            //擷取新版本interface執行個體
            IBackgroundCopyJob4 job4 = (IBackgroundCopyJob4)Marshal.GetObjectForIUnknown(pUnknwJob4);

            job4.AddFile("http://www.xianfen.net/GRMSDKX_EN_DVD.iso", "C:\\WinSDK.iso");
            job4.SetPriority(BG_JOB_PRIORITY.BG_JOB_PRIORITY_FOREGROUND);
            //各種操作。。。
        }
    }
}

下載:修改後的bits3_0.idl、TLB檔案及程式集下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.