MsftDiscFormat2Data事件处理程序

6
我已经成功地将IMAPI2与Interop.cs集成到我的应用程序中。我可以无问题地刻录CD/DVD。然而,MsftDiscFormat2Data更新的事件处理程序不起作用,所以我不能让进度条移动。
以下是我在codeproject网站上找到的代码: private void backgroundBurnWorker_DoWork(object sender, 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);
            }
        }
    }

void discFormatData_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender, [In, MarshalAs(UnmanagedType.IDispatch)] object progress) { // // 检查是否已取消 // 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,出于某种原因确实有效。

有人能帮帮我吗?

1个回答

10

我知道,可能太晚了,但我遇到了更新回调的同样问题。

打开项目->属性->应用程序->程序集信息并勾选“使程序集对COM可见”。


1
这是一个答案吗?如果是,请扩展它。如果不是,请删除它。 - random_user_name
1
哇!!!你刚刚帮我解决了一个问题,我一直在尝试解决整整一个月了!!!谢谢你,新年快乐! - Vova
这对我解决了问题。 - kenjara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接