示例#1
0
        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();
        }