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