/// <summary> /// Backup processing method. Called from the worker thread. /// </summary> protected override void Backup() { if (String.IsNullOrEmpty(this.activeDriveId)) { throw new InvalidOperationException("Drive not initialized."); } // Reset the time remaining from previous burns. this.StatusUpdateArgs.TimeRemaining = TimeSpan.Zero; this.UpdateStatus(DriveStatus.Burning); MsftDiscRecorder2 discRecorder2 = new MsftDiscRecorder2(); discRecorder2.InitializeDiscRecorder(this.activeDriveId); discRecorder2.AcquireExclusiveAccess(true, ClientName); MsftDiscFormat2Data discFormatData = new MsftDiscFormat2Data(); if (!discFormatData.IsCurrentMediaSupported(discRecorder2)) { throw new IOException("Invalid media."); } discFormatData.Recorder = discRecorder2; discFormatData.ClientName = ClientName; discFormatData.ForceMediaToBeClosed = true; using (var stream = this.ImageReader.ImageFile.OpenRead()) { discFormatData.Update += this.DiscFormatData_Update; try { discFormatData.Write(ComStream.ToIStream(stream)); } catch (COMException ex) { // Ignore canceled hresult. Other errors should be reported to the UI thread. if (ex.ErrorCode != -1062600702) { throw; } } finally { discFormatData.Update -= this.DiscFormatData_Update; discRecorder2.EjectMedia(); } // Double check that the burn was completed. Some cases with XP and 2003 do not // return an error, but the burn is not successful. Using progress < 99 since // the last update isn't always returned. if (!this.WorkerThread.CancellationPending && this.progress < 99) { throw new IOException("Burn not completed."); } } discRecorder2.ReleaseExclusiveAccess(); }
private void EraseStart_Click(object sender, RoutedEventArgs e) { CancelButton.IsEnabled = false; EraseStartButton.IsEnabled = false; eraseProgress.IsEnabled = true; checkQuickErase.IsEnabled = false; String driveId = (String)Application.Current.Properties["driveId"]; eraseStatusText.Text = Properties.Resources.MediaErase_Erasing; ThreadStart start = delegate() { IDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(driveId); recorder.AcquireExclusiveAccess(true, "imapi_erase_test"); IDiscFormat2Erase discErase = new MsftDiscFormat2Erase(); discErase.Recorder = recorder; discErase.ClientName = "imapi_erase_test"; try { discErase.FullErase = (bool)checkQuickErase.IsChecked; } catch (System.InvalidOperationException) { discErase.FullErase = false; } DiscFormat2Erase_Events eraseEvent = discErase as DiscFormat2Erase_Events; //DiscFormat2Data_Events burnProgress = dataWriterImage as DiscFormat2Data_Events; if (discErase.FullErase) { eraseEvent.Update += new DiscFormat2Erase_EventsHandler(OnEraseProgress); } try{ discErase.EraseMedia(); } catch { //this.DialogResult = true; } if (discErase.FullErase) { eraseEvent.Update -= new DiscFormat2Erase_EventsHandler(OnEraseProgress); } recorder.ReleaseExclusiveAccess(); Dispatcher.Invoke(new System.EventHandler(OnEraseFinished), this, null); }; Thread thread = new Thread(start); thread.IsBackground = true; thread.Start(); }
/// <summary> /// Worker thread that Formats the Disc /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundFormatWorker_DoWork(object sender, DoWorkEventArgs e) { // // Create and initialize the IDiscRecorder2 // MsftDiscRecorder2 discRecorder = new MsftDiscRecorder2(); string activeDiscRecorder = (string)e.Argument; discRecorder.InitializeDiscRecorder(activeDiscRecorder); discRecorder.AcquireExclusiveAccess(true, m_clientName); // // Create the IDiscFormat2Erase and set properties // MsftDiscFormat2Erase discFormatErase = new MsftDiscFormat2Erase(); discFormatErase.Recorder = discRecorder; discFormatErase.ClientName = m_clientName; discFormatErase.FullErase = !checkBoxQuickFormat.Checked; // // Setup the Update progress event handler // discFormatErase.Update += new DiscFormat2Erase_EventHandler(discFormatErase_Update); // // Erase the media here // try { discFormatErase.EraseMedia(); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show(ex.Message, "IDiscFormat2.EraseMedia failed", MessageBoxButtons.OK, MessageBoxIcon.Stop); } // // Remove the Update progress event handler // discFormatErase.Update -= new DiscFormat2Erase_EventHandler(discFormatErase_Update); if (checkBoxEjectFormat.Checked) { discRecorder.EjectMedia(); } discRecorder.ReleaseExclusiveAccess(); }
private void OnLoaded(object sender, RoutedEventArgs rea) { NavigationService.RemoveBackEntry(); NavigationService.RemoveBackEntry(); String driveId = (String)Application.Current.Properties["driveId"]; String sourceFile = (String)Application.Current.Properties["sourceFile"]; int writingSpeed = (int)Application.Current.Properties["writingSpeed"]; bool toBeClosed = (bool)Application.Current.Properties["toBeClosed"]; bool toVerify = (bool)Application.Current.Properties["toBeVerified"]; ThreadStart startCreate = delegate() { IDiscRecorder2 recorder = new MsftDiscRecorder2(); recorder.InitializeDiscRecorder(driveId); recorder.DisableMcn(); recorder.AcquireExclusiveAccess(true, "imapi_test"); IDiscFormat2Data dataWriterImage = new MsftDiscFormat2Data(); dataWriterImage.Recorder = recorder; dataWriterImage.ForceMediaToBeClosed = toBeClosed; dataWriterImage.ClientName = "test_imapi"; dataWriterImage.SetWriteSpeed(writingSpeed, false); DiscFormat2Data_Events burnProgress = dataWriterImage as DiscFormat2Data_Events; burnProgress.Update += new DiscFormat2Data_EventsHandler(OnBurnProgress); IStream stream = null; SHCreateStreamOnFile(sourceFile, 0, ref stream); POWER_REQUEST_CONTEXT prc; IntPtr hPower = (IntPtr)null; try{ prc.Version = 0; prc.SimpleReasonString = "imapi_test"; prc.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; hPower = PowerCreateRequest(ref prc); PowerSetRequest(hPower, PowerRequestType.PowerRequestDisplayRequired); } catch { } IBurnVerification verify = dataWriterImage as IBurnVerification; if (toVerify) { verify.BurnVerificationLevel = IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_FULL; } else { verify.BurnVerificationLevel = IMAPI_BURN_VERIFICATION_LEVEL.IMAPI_BURN_VERIFICATION_NONE; } try { dataWriterImage.Write(stream); } catch (System.Runtime.InteropServices.COMException e) { string s = "IMAPI Error: " + Convert.ToString(e.ErrorCode, 16); if ((uint)e.ErrorCode == 0xC0AA0002) //E_IMAPI_REQUEST_CANCELLED { // canceled s += "a"; } else if ((uint)e.ErrorCode == 0xC0AA0404) //E_IMAPI_DF2DATA_STREAM_TOO_LARGE_FOR_CURRENT_MEDIA { // Not enought capacity s = Properties.Resources.Page3_Error_NotEnoughCapacity; System.Windows.Forms.DialogResult messageBoxResult = System.Windows.Forms.MessageBox.Show(s, "IMAPI Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } else { System.Windows.Forms.DialogResult messageBoxResult = System.Windows.Forms.MessageBox.Show(s, "IMAPI Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } //else return; //throw; } catch { //error } burnProgress.Update -= new DiscFormat2Data_EventsHandler(OnBurnProgress); try { recorder.ReleaseExclusiveAccess(); recorder.EnableMcn(); } catch { } if (hPower != (IntPtr)null) { PowerClearRequest(hPower, PowerRequestType.PowerRequestDisplayRequired); CloseHandle(hPower); } Dispatcher.Invoke(new System.EventHandler(OnWritingFinished), this, null); }; const UInt32 SC_CLOSE = 0x0000F060; const UInt32 MF_GRAYED = 0x00000001; WindowInteropHelper wih = new WindowInteropHelper(mainWindow); IntPtr hMenu = GetSystemMenu(wih.Handle, 0); EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED); Thread thread = new Thread(startCreate); thread.IsBackground = true; thread.Start(); }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (radioButtonData.Checked) { MsftDiscRecorder2 Recorder = null; MsftDiscFormat2Data Data = null; try { Recorder = new MsftDiscRecorder2(); BURN_INTERFACE burnMedia = (BURN_INTERFACE)e.Argument; Recorder.InitializeDiscRecorder(burnMedia.uniqueRecorderId); Recorder.AcquireExclusiveAccess(true, namaProgram); Data = new MsftDiscFormat2Data(); Data.Recorder = Recorder; Data.ClientName = namaProgram; Data.ForceMediaToBeClosed = checkBoxSekaliPakai.Checked; IBurnVerification burnVerification = (IBurnVerification)Data; burnVerification.BurnVerificationLevel = (IMAPI_BURN_VERIFICATION_LEVEL)verificationLevel; object[] multisessionInterfaces = null; if (!Data.MediaHeuristicallyBlank) { multisessionInterfaces = Data.MultisessionInterfaces; } IStream fileSystem = null; if (!membuatFileSystem(Recorder, multisessionInterfaces, out fileSystem)) { e.Result = -1; return; } Data.Update += new DiscFormat2Data_EventHandler(burningUpdate); try { Data.Write(fileSystem); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show("Sepertinya terjadi masalah dalam I/O stream. \nTidak perlu panik, coba cek parameter...", pesan, MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { if (fileSystem != null) { Marshal.FinalReleaseComObject(fileSystem); } } Data.Update -= new DiscFormat2Data_EventHandler(burningUpdate); if (this.checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } catch (COMException exception) { MessageBox.Show("Okay, ini mungkin masalah...\nCoba cek semua parameter dan lakukan ulang semua langkah dari awal.", pesan); e.Result = exception.ErrorCode; if (this.checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } finally { if (Recorder != null) { Recorder.ReleaseExclusiveAccess(); Marshal.ReleaseComObject(Recorder); } if (Data != null) { Marshal.ReleaseComObject(Data); } } } else if (radioButtonImage.Checked) { MsftDiscRecorder2 Recorder = null; MsftDiscFormat2Data Data = null; IMAPI2.Interop.FsiStream streamData = null; int imageStream = SHCreateStreamOnFile(textBoxImage.Text, 0x20, out streamData); if (imageStream < 0) { return; } try { Recorder = new MsftDiscRecorder2(); BURN_INTERFACE burnMedia = (BURN_INTERFACE)e.Argument; Recorder.InitializeDiscRecorder(burnMedia.uniqueRecorderId); Recorder.AcquireExclusiveAccess(true, namaProgram); Data = new MsftDiscFormat2Data(); Data.Recorder = Recorder; Data.ClientName = namaProgram; IBurnVerification burnVerification = (IBurnVerification)Data; burnVerification.BurnVerificationLevel = (IMAPI_BURN_VERIFICATION_LEVEL)verificationLevel; Data.Update += new DiscFormat2Data_EventHandler(burningUpdate); try { Data.Write(streamData); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show("Ups, terjadi kesalahan...\nHal ini karena ukuran *ISO yang tidak sesuai dengan ukuran media.", pesan, MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { if (streamData != null) { Marshal.FinalReleaseComObject(streamData); } } Data.Update -= new DiscFormat2Data_EventHandler(burningUpdate); if (this.checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } catch (COMException exception) { MessageBox.Show("Okay, ini mungkin masalah...\nCoba cek semua parameter dan lakukan ulang semua langkah dari awal.", pesan); e.Result = exception.ErrorCode; if (this.checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } finally { if (Recorder != null) { Recorder.ReleaseExclusiveAccess(); Marshal.ReleaseComObject(Recorder); } if (Data != null) { Marshal.ReleaseComObject(Data); } } } }
/// <summary> /// The thread that does the burning of the media /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundBurnWorker_DoWork(object sender, DoWorkEventArgs e) { // // Create and initialize the IDiscRecorder2 object // MsftDiscRecorder2 discRecorder = new MsftDiscRecorder2(); BurnData burnData = (BurnData)e.Argument; discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId); discRecorder.AcquireExclusiveAccess(true, m_clientName); // // Create and initialize the IDiscFormat2Data // MsftDiscFormat2Data discFormatData = new MsftDiscFormat2Data(); discFormatData.Recorder = discRecorder; discFormatData.ClientName = m_clientName; discFormatData.ForceMediaToBeClosed = checkBoxCloseMedia.Checked; // // Check if media is blank, (for RW media) // object[] multisessionInterfaces = null; if (!discFormatData.MediaHeuristicallyBlank) { multisessionInterfaces = discFormatData.MultisessionInterfaces; } // // Create the file system // IStream fileSystem = null; if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem)) { e.Result = -1; return; } // // add the Update event handler // discFormatData.Update += new DiscFormat2Data_EventHandler(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); } // // remove the Update event handler // discFormatData.Update -= new DiscFormat2Data_EventHandler(discFormatData_Update); if (this.checkBoxEject.Checked) { discRecorder.EjectMedia(); } discRecorder.ReleaseExclusiveAccess(); }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { MsftDiscRecorder2 Recorder = null; MsftDiscFormat2Erase Format = null; MsftFileSystemImage fileSystem = null; try { Recorder = new MsftDiscRecorder2(); string activeDiscRecorder = (string)e.Argument; Recorder.InitializeDiscRecorder(activeDiscRecorder); Recorder.AcquireExclusiveAccess(true, namaProgram); fileSystem = new MsftFileSystemImage(); fileSystem.VolumeName = ""; Format = new MsftDiscFormat2Erase(); Format.Recorder = Recorder; Format.ClientName = namaProgram; Format.FullErase = checkBoxFormatCepat.Checked; Format.Update += new DiscFormat2Erase_EventHandler(eraseUpdate); try { Format.EraseMedia(); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show("Sepertinya media ini tidak bisa dihapus...\nKemungkinan media terkunci, diproteksi atau sudah rusak.", pesan, MessageBoxButtons.OK, MessageBoxIcon.Stop); } Format.Update -= new DiscFormat2Erase_EventHandler(eraseUpdate); if (checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } catch (COMException exception) { MessageBox.Show(exception.Message, pesan); if (checkBoxKeluarkanTray.Checked) { Recorder.EjectMedia(); } } finally { if (Recorder != null) { Recorder.ReleaseExclusiveAccess(); Marshal.ReleaseComObject(Recorder); } if (Format != null) { Marshal.ReleaseComObject(Format); } } }
/// <summary> /// Worker thread that Formats the Disc /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundFormatWorker_DoWork(object sender, DoWorkEventArgs e) { MsftDiscRecorder2 discRecorder = null; MsftDiscFormat2Erase discFormatErase = null; try { // // Create and initialize the IDiscRecorder2 // discRecorder = new MsftDiscRecorder2(); string activeDiscRecorder = (string)e.Argument; discRecorder.InitializeDiscRecorder(activeDiscRecorder); discRecorder.AcquireExclusiveAccess(true, m_clientName); // // Create the IDiscFormat2Erase and set properties // discFormatErase = new MsftDiscFormat2Erase(); discFormatErase.Recorder = discRecorder; discFormatErase.ClientName = m_clientName; discFormatErase.FullErase = !checkBoxQuickFormat.Checked; // // Setup the Update progress event handler // discFormatErase.Update += new DiscFormat2Erase_EventHandler(discFormatErase_Update); // // Erase the media here // try { discFormatErase.EraseMedia(); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show(ex.Message, "Ошибка при форматировании!", MessageBoxButtons.OK, MessageBoxIcon.Stop); } // // Remove the Update progress event handler // discFormatErase.Update -= new DiscFormat2Erase_EventHandler(discFormatErase_Update); // // Eject the media // if (checkBoxEjectFormat.Checked) { discRecorder.EjectMedia(); } discRecorder.ReleaseExclusiveAccess(); } catch (COMException exception) { // // If anything happens during the format, show the message // // MessageBox.Show(exception.Message); } finally { if (discRecorder != null) { Marshal.ReleaseComObject(discRecorder); } if (discFormatErase != null) { Marshal.ReleaseComObject(discFormatErase); } } }
/// <summary> /// The thread that does the burning of the media /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundBurnWorker_DoWork(object sender, DoWorkEventArgs e) { MsftDiscRecorder2 discRecorder = null; MsftDiscFormat2Data discFormatData = null; try { // // Create and initialize the IDiscRecorder2 object // discRecorder = new MsftDiscRecorder2(); BurnData burnData = (BurnData)e.Argument; discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId); discRecorder.AcquireExclusiveAccess(true, m_clientName); // // Create and initialize the IDiscFormat2Data // discFormatData = new MsftDiscFormat2Data(); discFormatData.Recorder = discRecorder; discFormatData.ClientName = m_clientName; discFormatData.ForceMediaToBeClosed = checkBoxCloseMedia.Checked; // // Set the verification level // IBurnVerification burnVerification = (IBurnVerification)discFormatData; burnVerification.BurnVerificationLevel = (IMAPI_BURN_VERIFICATION_LEVEL)m_verificationLevel; // // Check if media is blank, (for RW media) // object[] multisessionInterfaces = null; if (!discFormatData.MediaHeuristicallyBlank) { multisessionInterfaces = discFormatData.MultisessionInterfaces; } // // Create the file system // IStream fileSystem = null; if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem)) { e.Result = -1; return; } // // add the Update event handler // discFormatData.Update += new DiscFormat2Data_EventHandler(discFormatData_Update); // // Write the data here // try { discFormatData.Write(fileSystem); e.Result = 0; } catch (COMException ex) { e.Result = ex.ErrorCode; MessageBox.Show(ex.Message, "Ошибка в процессе записи диска!", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { if (fileSystem != null) { Marshal.FinalReleaseComObject(fileSystem); } } // // remove the Update event handler // discFormatData.Update -= new DiscFormat2Data_EventHandler(discFormatData_Update); if (this.checkBoxEject.Checked) { discRecorder.EjectMedia(); } discRecorder.ReleaseExclusiveAccess(); } 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); } } }
public int WriteStream(System.Runtime.InteropServices.ComTypes.IStream stream, string initburner, string clientName, bool forceMediaToBeClosed, int speed, bool eject) { MsftDiscRecorder2 discRecorder = null; MsftDiscFormat2Data discFormat = null; var result = -1; try { discRecorder = new MsftDiscRecorder2(); discRecorder.InitializeDiscRecorder(initburner); discFormat = new MsftDiscFormat2Data(); //remove the comment for next 2 lines discFormat.Recorder = discRecorder; discRecorder.AcquireExclusiveAccess(true, clientName); //rec.DisableMcn(); // // initialize the IDiscFormat2Data // discFormat.ClientName = clientName; discFormat.ForceMediaToBeClosed = forceMediaToBeClosed; // // add the Update event handler // discFormat.Update += DiscFormatData_Update; //this is how it worked for my burner //speed = 0 => minimum speed descriptor in update // 0 < speed < minimum speed descriptor => half of minimum speed descriptor in update // minimum speed descriptor <= speed < next speed descriptor => minimum speed descriptor in update // next speed descriptor <= speed => next speed descriptorin update //discFormat.SetWriteSpeed(2000, true);//????????????? discFormat.SetWriteSpeed(speed, true); //write the stream discFormat.Write(stream); if (_backgroundWorker.CancellationPending) { return(1); } if (eject) { //wait to flush all the content on the media var state = IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN; while (state == IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN && !_backgroundWorker.CancellationPending) { try { state = discFormat.CurrentMediaStatus; } catch (Exception) { state = IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN; Thread.Sleep(3000); } } if (!_backgroundWorker.CancellationPending) { discRecorder.EjectMedia(); } } result = 0; } finally { if (_backgroundWorker.CancellationPending) { result = 1; } if (discFormat != null) { // remove the Update event handler // discFormat.Update -= DiscFormatData_Update; Marshal.FinalReleaseComObject(discFormat); } if (discRecorder != null) { //discRecorder.EnableMcn(); discRecorder.ReleaseExclusiveAccess(); Marshal.FinalReleaseComObject(discRecorder); } } return(result); }
public void FormatMedia(string activeDiscRecorder, string clientName, bool QuickFormat, bool eject) { MsftDiscFormat2Erase discFormatErase = null; MsftDiscRecorder2 discRecorder = null; try { // Create and initialize the IDiscRecorder2 // discRecorder = new MsftDiscRecorder2(); discRecorder.InitializeDiscRecorder(activeDiscRecorder); discRecorder.AcquireExclusiveAccess(true, clientName); // discRecorder.DisableMcn(); // // Create the IDiscFormat2Erase and set properties //// discFormatErase = new MsftDiscFormat2Erase(); discFormatErase.Recorder = discRecorder; discFormatErase.ClientName = clientName; discFormatErase.FullErase = !QuickFormat; // // Setup the Update progress event handler // discFormatErase.Update += DiscFormaEraseData_Update; // // Erase the media here // discFormatErase.EraseMedia(); if (eject) { Thread.Sleep(2000); if (!_backgroundWorker.CancellationPending) { discRecorder.EjectMedia(); } } } finally { // // remove the Update event handler // if (discFormatErase != null) { discFormatErase.Update -= DiscFormaEraseData_Update; } if (discFormatErase != null) { Marshal.FinalReleaseComObject(discFormatErase); } if (discRecorder != null) { //discRecorder.EnableMcn(); discRecorder.ReleaseExclusiveAccess(); Marshal.FinalReleaseComObject(discRecorder); } } }