MsftDiscFormat2Data Обработчик событий - PullRequest
5 голосов
/ 29 ноября 2010

Я успешно интегрировал IMAPI2, используя Interop.cs, в свое приложение. Я могу записывать CD / DVD без проблем. Однако обработчик событий для обновления MsftDiscFormat2Data не работает, поэтому я не могу заставить двигаться индикатор выполнения.

Вот код, который я нашел на сайте codeproject:

private void 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);
            }
        }
    }

void discFormatData_Update (отправитель объекта [In, MarshalAs (UnmanagedType.IDispatch)], объект [In, MarshalAs (UnmanagedType.IDispatch)]) { // // Проверить, если мы отменили // if (backgroundBurnWorker.CancellationPending) { var format2Data = (IDiscFormat2Data) отправитель; format2Data.CancelWrite (); вернуть; }

        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);
    }

К сожалению, обработчик обновлений никогда не вызывается. Я искал повсюду в Интернете, но не смог найти решение. Я видел некоторых людей с такой же проблемой, но никто не смог ответить.

Оригинальный код от Эрика, http://www.codeproject.com/KB/miscctrl/imapi2.aspx?msg=2695517, работает по какой-то причине.

Может кто-нибудь помочь мне?

1 Ответ

9 голосов
/ 01 сентября 2011

Я знаю, уже слишком поздно, но у меня та же проблема с обратными вызовами обновления.

Откройте Project-> Properties-> Application-> Информация о сборке и отметьте «Сделать сборку видимой для COM».

...