2010-11-29 292 views
4

我已經成功地將使用Interop.cs的IMAPI2集成到我的應用程序中。我可以刻錄CD/DVD沒有任何問題。但是,MsftDiscFormat2Data更新的事件處理程序不起作用,所以無法移動我的進度條。MsftDiscFormat2Data事件處理程序

這裏是我發現在CodeProject網站的代碼:

私人無效backgroundBurnWorker_DoWork(對象發件人,DoWorkEventArgs E) { MsftDiscRecorder2 discRecorder = NULL; MsftDiscFormat2Data discFormatData = null;

 try 
     { 
      // 
      // Create and initialize the IDiscRecorder2 object 
      // 
      discRecorder = new MsftDiscRecorder2(); 
      var burnData = (BurnData)e.Argument; 
      discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId); 

      // 
      // Create and initialize the IDiscFormat2Data 
      // 
      discFormatData = new MsftDiscFormat2Data 
       { 
        Recorder = discRecorder, 
        ClientName = ClientName, 
        ForceMediaToBeClosed = _closeMedia 
       }; 

      // 
      // Set the verification level 
      // 
      var burnVerification = (IBurnVerification)discFormatData; 
      burnVerification.BurnVerificationLevel = _verificationLevel; 

      // 
      // Check if media is blank, (for RW media) 
      // 
      object[] multisessionInterfaces = null; 
      if (!discFormatData.MediaHeuristicallyBlank) 
      { 
       multisessionInterfaces = discFormatData.MultisessionInterfaces; 
      } 

      // 
      // Create the file system 
      // 
      IStream fileSystem; 
      if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem)) 
      { 
       e.Result = -1; 
       return; 
      } 

      // 
      // add the Update event handler 
      // 
      discFormatData.Update += discFormatData_Update; 

      // 
      // Write the data here 
      // 
      try 
      { 
       discFormatData.Write(fileSystem); 
       e.Result = 0; 
      } 
      catch (COMException ex) 
      { 
       e.Result = ex.ErrorCode; 
       MessageBox.Show(ex.Message, "IDiscFormat2Data.Write failed", 
        MessageBoxButtons.OK, MessageBoxIcon.Stop); 
      } 
      finally 
      { 
       if (fileSystem != null) 
       { 
        Marshal.FinalReleaseComObject(fileSystem); 
       } 
      } 

      // 
      // remove the Update event handler 
      // 
      discFormatData.Update -= discFormatData_Update; 

      if (_ejectMedia) 
      { 
       discRecorder.EjectMedia(); 
      } 
     } 
     catch (COMException exception) 
     { 
      // 
      // If anything happens during the format, show the message 
      // 
      MessageBox.Show(exception.Message); 
      e.Result = exception.ErrorCode; 
     } 
     finally 
     { 
      if (discRecorder != null) 
      { 
       Marshal.ReleaseComObject(discRecorder); 
      } 

      if (discFormatData != null) 
      { 
       Marshal.ReleaseComObject(discFormatData); 
      } 
     } 
    } 

無效discFormatData_Update([在,的MarshalAs(UnmanagedType.IDispatch)對象發件人,[在,的MarshalAs(UnmanagedType.IDispatch)對象進度) {// // 檢查,我們已取消 // if(backgroundBurnWorker.CancellationPending) var format2Data =(IDiscFormat2Data)sender; format2Data.CancelWrite(); return; }

 var eventArgs = (IDiscFormat2DataEventArgs)progress; 

     _burnData.task = BURN_MEDIA_TASK.BURN_MEDIA_TASK_WRITING; 

     // IDiscFormat2DataEventArgs Interface 
     _burnData.elapsedTime = eventArgs.ElapsedTime; 
     _burnData.remainingTime = eventArgs.RemainingTime; 
     _burnData.totalTime = eventArgs.TotalTime; 

     // IWriteEngine2EventArgs Interface 
     _burnData.currentAction = eventArgs.CurrentAction; 
     _burnData.startLba = eventArgs.StartLba; 
     _burnData.sectorCount = eventArgs.SectorCount; 
     _burnData.lastReadLba = eventArgs.LastReadLba; 
     _burnData.lastWrittenLba = eventArgs.LastWrittenLba; 
     _burnData.totalSystemBuffer = eventArgs.TotalSystemBuffer; 
     _burnData.usedSystemBuffer = eventArgs.UsedSystemBuffer; 
     _burnData.freeSystemBuffer = eventArgs.FreeSystemBuffer; 

     // 
     // Report back to the UI 
     // 
     backgroundBurnWorker.ReportProgress(0, _burnData); 
    } 

不幸的是,更新處理被稱爲永遠不會。我在互聯網上到處查找,但無法找到解決方案。我看到一些人有同樣的問題,但沒有人能夠回答。

來自Eric的原始代碼http://www.codeproject.com/KB/miscctrl/imapi2.aspx?msg=2695517, 確實有效。

有人可以幫我嗎?

回答

7

我知道,這太晚了,但我有更新回調相同的問題。

打開Project-> Properties-> Application-> Assembly Information並勾選「Make Assembly COM-visible」。

+1

這是一個答案?如果是這樣,請展開它。如果沒有,請刪除它。 – 2012-11-13 03:21:38