Method of C calling Nero SDK to burn optical disc

  • 2021-07-10 20:34:14
  • OfStack

This paper describes the method of C # calling Nero SDK to burn CD. Share it for your reference. The details are as follows:

There is a demand for multi-CD-ROM burning in the project, but in one process, multiple burning tasks are started at the same time, and the process often reports abnormalities, so the burning part is made a console program separately, and each CD-ROM burning starts one process to execute the burning task, so that even if the burning process goes wrong, it will not affect the parent process.

The following is the Nero carving class, and SDK uses NeroSDK-1. 08 version to call NeroCOM component. You must reference Interop. NEROLib. dll and Interop. NeroVisionAPI. dll when calling.


public class NeroBurn
{
  #region  Attribute 
  private NeroDrive m_neroDerive;
  private Nero m_nero;
  private bool m_aborted;
  private string m_burnPath;
  private bool m_isDoneBurn;
  private int m_returnCode;//0: Failure; 1 Success; -1 : Unknown 
  private string m_discvol;
  //nero  Delegate 
  private _INeroDriveEvents_OnDoneBurnEventHandler m_evOnDoneBurn;
  private _INeroDriveEvents_OnAddLogLineEventHandler m_evOnAddLogLine;
  private _INeroEvents_OnWaitCDEventHandler m_evOnWaitCD;
  private _INeroDriveEvents_OnAbortedEventHandler m_evOnAborted;
  private _INeroDriveEvents_OnProgressEventHandler m_evOnProgress;
  private _INeroEvents_OnWaitCDDoneEventHandler m_evOnWaitCDDone;
  #endregion 
  #region  Constructor 
  /// <summary></summary>
  ///  Constructor 
  /// 
  /// <param name="driverletter"> Disk letter 
  /// <param name="burnpath"> File path to burn 
  /// <param name="discvol"> Volume label of disk 
  public NeroBurn(string driverletter,string burnpath,string discvol)
  {
   m_burnPath = burnpath;
   m_nero = new NeroClass();
   m_neroDerive = allocDevice(driverletter);
   m_aborted = false;
   m_isDoneBurn = false;
   m_discvol = discvol;
  }
  #endregion 
  #region Nero Event handling 
  /// <summary></summary>
  ///  Initiate a burn event 
  /// 
  /// <param name="bSubscribe">
  private void SubscribeToEvents(bool bSubscribe)
  {
   if (bSubscribe)
   {
    m_evOnAddLogLine = new _INeroDriveEvents_OnAddLogLineEventHandler(m_drive_OnAddLogLine);
    m_neroDerive.OnAddLogLine += m_evOnAddLogLine;
    m_evOnWaitCD = new _INeroEvents_OnWaitCDEventHandler(m_nero_OnWaitCD);
    m_nero.OnWaitCD += m_evOnWaitCD;
    m_evOnDoneBurn = new _INeroDriveEvents_OnDoneBurnEventHandler(m_drive_OnDoneBurn);
    m_neroDerive.OnDoneBurn += m_evOnDoneBurn;
    m_evOnWaitCDDone = new _INeroEvents_OnWaitCDDoneEventHandler(m_nero_OnWaitCDDone);
    m_nero.OnWaitCDDone += m_evOnWaitCDDone;
    m_evOnProgress = new _INeroDriveEvents_OnProgressEventHandler(m_drive_OnProgress);
    m_neroDerive.OnProgress += m_evOnProgress;
   }
   else
   {    
    m_neroDerive.OnAddLogLine -= m_evOnAddLogLine;
    m_nero.OnWaitCD -= m_evOnWaitCD;
    m_nero.OnWaitCDDone -= m_evOnWaitCDDone;
    m_neroDerive.OnDoneBurn -= m_evOnDoneBurn;
    m_neroDerive.OnProgress -= m_evOnProgress;
   }
  }
  private void m_drive_OnProgress(ref int ProgressInPercent, ref bool Abort)
  {
   // This events gives us an opportunity to show progress
   // as well as abort if needed.
   // 
   Abort = m_aborted;   
   Console.WriteLine(" CD-ROM " + m_discvol + " " + ProgressInPercent.ToString() + "% had Burned!!");
   //c_TaskPercent.Text = ProgressInPercent.ToString() + "%";
  }
  private void m_nero_OnWaitCDDone()
  {
   // When waiting on a disc is done, make sure to
   // enable us and hide the WaitCD form.
   // 
   Console.WriteLine("a Disc has inserted!");
  }
  /// <summary></summary>
  /// Burn complete, regardless of failure or success!  
  /// 
  /// <param name="StatusCode">
  private void m_drive_OnDoneBurn(ref NERO_BURN_ERROR StatusCode)
  {
   // When burning is over, make sure to unsubscribe from all
   // events.
   // 
   SubscribeToEvents(false);
   if (StatusCode == NERO_BURN_ERROR.NERO_BURN_OK)
   {
    m_returnCode = 1;
    Console.WriteLine(m_neroDerive.DriveLetter + " The disk volume is labeled as  " + m_discvol + "  Burn Successfully !!");
   }
   else
   {
    m_returnCode = 0;
    Console.WriteLine(m_neroDerive.DriveLetter + " The disk volume is labeled as  " + m_discvol + "  Burn failed !!");
   }
   m_isDoneBurn = true;
   Console.WriteLine("Burn Finish!!");
  }
  /// <summary></summary>
  ///  If the CD-ROM drive is empty, wait for the CD-ROM drive 
  /// 
  /// <param name="WaitCD">
  /// <param name="WaitCDLocalizedText">
  private void m_nero_OnWaitCD(ref NERO_WAITCD_TYPE WaitCD, ref string WaitCDLocalizedText)
  {
   Console.WriteLine("Wait CD...");
  }
  /// <summary></summary>
  ///  Write a journal 
  /// 
  /// <param name="TextType">
  /// <param name="Text">
  private void m_drive_OnAddLogLine(ref NERO_TEXT_TYPE TextType, ref string Text)
  {
   Console.WriteLine( Text);
  }
  #endregion 
  #region  Apply for equipment 
  /// <summary></summary>
  ///  Apply for equipment 
  /// 
  /// <param name="driverletter">
  /// <returns></returns>
  private NeroDrive allocDevice(string driverletter)
  {
   NeroDrives drives = m_nero.GetDrives(NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M);
   if (drives.Count > 0)
   {
    foreach (NeroDrive d in drives)
    {
     if (driverletter.ToLower().Contains(d.DriveLetter.ToLower()))
      return d;
    }
   }
   return null;
  }
  #endregion 
  #region  Burn 
  /// <summary></summary>
  ///  Burn 
  /// 
  /// <returns></returns>0:  Failure; 1 Success; -1 : Unknown error 
  public int Burn()
  {
   m_isDoneBurn = false; 
   SubscribeToEvents(true);
   NeroISOTrack isoTrack = new NeroISOTrackClass();
   isoTrack.BurnOptions = (NERO_BURN_OPTIONS)((uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_CREATE_ISO_FS + (uint)NERO_BURN_OPTIONS.NERO_BURN_OPTION_USE_JOLIET);
   isoTrack.Name = m_discvol;
   Console.WriteLine("Begin AddFilesAndFoldersToISOTrack.");
   AddFilesAndFoldersToISOTrack(ref isoTrack, m_burnPath);
   Console.WriteLine("End AddFilesAndFoldersToISOTrack.");
   if (isoTrack.RootFolder.Files.Count == 0 &&
     isoTrack.RootFolder.Folders.Count == 0)
   {
    isoTrack = null;
    return 0;
   }
   NERO_BURN_FLAGS flags = new NERO_BURN_FLAGS();
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_WRITE;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_CLOSE_SESSION;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_BUF_UNDERRUN_PROT;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DISABLE_ABORT;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DAO;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_CD_TEXT;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_DETECT_NON_EMPTY_CDRW;
   flags = flags | NERO_BURN_FLAGS.NERO_BURN_FLAG_SPEED_IN_KBS; 
   try
   {
    if (null == m_neroDerive)
    {
     Console.WriteLine("m_neroDerive is null!!");
     return -1;
    }
    int speed = 10000;
    string BurnSpeed_s = System.Configuration.ConfigurationSettings.AppSettings["BurnSpeed"];
    if (!string.IsNullOrEmpty(BurnSpeed_s))
     speed = int.Parse(BurnSpeed_s);
    m_neroDerive.BurnIsoAudioCD("", "", false,
     isoTrack,
     null,
     null,
     flags,
      speed,
      NERO_MEDIA_TYPE.NERO_MEDIA_DVD_M);
    // Loop and wait for burning to complete 
    while (true)
    {
     if(m_isDoneBurn) // If burning is complete 
      return m_returnCode;
     System.Threading.Thread.Sleep(1000);
    }
   }
   catch (Exception e)
   {
    m_aborted = true;
    Console.WriteLine("Burn Error : "+e.Message);
    return 0;
   }
   return -1;
  }
  #endregion 
  #region  Auxiliary function 
  /// <summary></summary>
  /// This function adds the files and folders to the supplied NeroISOTrack.
  /// Add by Cola 2008-4-14
  /// 
  /// <param name="isoTrack">
  /// <param name="discPath">
  private void AddFilesAndFoldersToISOTrack(ref NeroISOTrack isoTrack, string discPath)
  {
   string sPath = discPath;
   // The string should really not be empty...
   // 
   if (sPath != "")
   {
    // If path ends in a backslash, it is a folder.
    // 
    if (sPath[sPath.Length - 1] == '//')
    {
     NeroFolder folder = isoTrack.RootFolder;
     AddFolderRecursively(ref folder, sPath);
    }
    else
    {
     // This is a file. Create a new NeroFile
     // change its properties.
     // 
     NeroFile file = new NeroFileClass();
     file.SourceFilePath = sPath;
     file.Name = Path.GetFileName(sPath);
     file.EntryTime = Directory.GetLastWriteTime(sPath);
     // In this implementation, specified files are added
     // to the root of the disc only.
     // 
     isoTrack.RootFolder.Files.Add(file);
    }
   }
  }
  /// <summary></summary>
  /// This function is used to recursively add the path to the supplied
  /// parent NeroFolder. Add by Cola 2008-4-14
  /// 
  /// <param name="folderParent">
  /// <param name="sPath">
  private void AddFolderRecursively(ref NeroFolder folderParent, string sPath)
  {
   NeroFolder folder = new NeroFolderClass();
   folderParent.Folders.Add(folder);
   string[] sSplits = sPath.Split(new char[] { '//' }, sPath.Length);
   if (sSplits.GetLength(0) >= 2)
   {
    string sFolderName = sSplits[sSplits.GetLength(0) - 2];
    folder.Name = sFolderName;
    string[] sDirectories = Directory.GetDirectories(sPath);
    foreach (string sSubDirPath in sDirectories)
    {
     AddFolderRecursively(ref folder, sSubDirPath + "//");
    }
   }
   string[] sFiles = Directory.GetFiles(sPath);
   foreach (string sFile in sFiles)
   {
    NeroFile file = new NeroFileClass();
    file.SourceFilePath = sFile;
    file.Name = Path.GetFileName(sFile);
    file.EntryTime = Directory.GetLastWriteTime(sFile);
    folder.Files.Add(file);
   }
  }
  #endregion 
}

Then, the method of the NeroBurn class is called in the Main function, which takes the following arguments:


static int Main(string[] args) 
{ 
   string driverletter; // Drive letter , Containing ':' No.  
   string burnpath;  // Burn file directory  
   string discvol;  // Disc label  
   if (args.Length == 3) 
   { 
    driverletter = args[0]; 
    burnpath = args[1]; 
    discvol = args[2]; 
    NeroBurn b = new NeroBurn(driverletter, burnpath, discvol); 
    int ret = b.Burn(); 
    return ret; 
   } 
   return -1;

I hope this article is helpful to everyone's C # programming.


Related articles: