刻录光盘映像

使用 IMAPI 控制 (刻录光盘) 包括以下步骤:

  1. 构造包含要写入光盘的目录和文件的文件系统映像。
  2. 设置光盘记录器 以与光学设备通信。
  3. 创建数据编写器 并将映像刻录到光盘。

有关刻录光盘映像的示例,请参阅 VBScript 示例

构造刻录图像

刻录图像是准备写入光学介质的数据流。 ISO9660、Joliet 和 UDF 格式的刻录映像由单个文件和目录的文件系统组成。 CFileSystemImage 对象是保存要放置在光学介质上的文件和目录的文件系统对象。 IFileSystemImage 接口提供对文件系统对象和设置的访问权限。

创建文件系统对象后,请分别调用 IFileSystemImage::CreateFileItemIFileSystemImage::CreateDirectoryItem 方法来创建文件和目录对象。 文件和目录对象可用于提供有关文件和目录的特定详细信息。 可用于 IFileSystemImage 的事件处理程序方法可以标识要添加到文件系统映像的当前文件、已复制的扇区数以及要复制的扇区总数。

(可选)可以使用 IFileSystemImage::p ut_BootImageOptions 属性将启动映像附加到文件系统。 有关示例,请参阅 添加启动映像

最后,调用 IFileSystemImage::CreateResultImage 以创建数据流,并通过 IFileSystemImageResult 提供访问权限。 然后,可以将新数据流直接提供给 IDiscFormat2Data::Write 方法,或保存到文件以供以后使用。

设置光盘录制器

MsftDiscMaster2 对象提供系统上光学设备的枚举。 IDiscMaster2 接口提供对生成的设备枚举的访问。 遍历枚举以找到相应的录制设备。 在计算机添加或删除光学设备时, MsftDiscMaster2 对象还提供事件通知。

找到光盘记录器并检索其 ID 后,创建 MsftDiscRecorder2 对象并使用设备 ID 初始化记录器。 IDiscRecorder2 接口提供对记录器对象以及一些基本设备信息(例如供应商 ID、产品 ID、产品修订版)的访问,以及弹出介质并关闭托盘的方法。

创建数据编写器并写入刻录映像

MsftDiscFormat2Data 对象提供写入方法、有关写入函数的属性和特定于媒体的属性。 IDiscFormat2Data 接口提供对 MsftDiscFormat2Data 对象的访问。

光盘录制器使用 IDiscFormat2Data::p ut_Recorder 属性链接到格式编写器。 将记录器绑定到格式编写器后,可以在使用 IDiscFormat2Data::Write 方法将结果图像写入光盘之前,对媒体执行查询并更新特定于写入的属性。

IMAPI 提供的其他格式写入接口的工作方式类似;其他格式写入接口包括:

注意

在刻录操作期间可能会发生电源状态转换, (即用户注销或系统暂停) 这会导致刻录过程中断和可能的数据丢失。 有关编程注意事项,请参阅 防止在燃烧期间注销或挂起

 

VBScript 示例

此脚本示例演示如何使用 IMAPI 对象来刻录光学介质;更具体地说,将目录写入空白光盘。该代码不包含任何错误检查,并假定以下内容:

  • 系统上安装了兼容的光盘设备。
  • 光盘设备是系统上的第二个驱动器。
  • 在光盘设备中插入兼容的光盘。
  • 光盘为空。
  • 要写入光盘的文件位于“g:\burndir”中。

可以将其他功能(例如错误检查、设备和媒体兼容性、事件通知以及计算光盘上的可用空间)添加到此脚本中。

' This script burns data files to disc in a single session 
' using files from a single directory tree.
 
' Copyright (C) Microsoft Corp. 2006

Option Explicit

' *** CD/DVD disc file system types
Const FsiFileSystemISO9660 = 1
Const FsiFileSystemJoliet  = 2
Const FsiFileSystemUDF102  = 4

WScript.Quit(Main)

Function Main
    Dim Index                ' Index to recording drive.
    Dim Recorder             ' Recorder object
    Dim Path                 ' Directory of files to burn
    Dim Stream               ' Data stream for burning device
    
    Index = 1                ' Second drive on the system
    Path = "g:\BurnDir"      ' Files to transfer to disc

    ' Create a DiscMaster2 object to connect to optical drives.
    Dim g_DiscMaster
    Set g_DiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")

    ' Create a DiscRecorder object for the specified burning device.
    Dim uniqueId
    set recorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")
    uniqueId = g_DiscMaster.Item(index)
    recorder.InitializeDiscRecorder( uniqueId )

    ' Create an image stream for a specified directory.
    Dim FSI                  ' Disc file system
    Dim Dir                  ' Root directory of the disc file system
    Dim dataWriter    
        
    ' Create a new file system image and retrieve root directory
    Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")
    Set Dir = FSI.Root

    'Create the new disc format and set the recorder
    Set dataWriter = CreateObject ("IMAPI2.MsftDiscFormat2Data")
    dataWriter.recorder = Recorder
    dataWriter.ClientName = "IMAPIv2 TEST"

    FSI.ChooseImageDefaults(recorder)
        
    ' Add the directory and its contents to the file system 
    Dir.AddTree Path, false
        
    ' Create an image from the file system
    Dim result
    Set result = FSI.CreateResultImage()
    Stream = result.ImageStream
    
    ' Write stream to disc using the specified recorder.
    WScript.Echo "Writing content to disc..."
    dataWriter.write(Stream)

    WScript.Echo "----- Finished writing content -----"
    Main = 0
End Function

使用 IMAPI

IDiscFormat2Data

IDiscFormat2Erase

IDiscFormat2RawCD

IDiscFormat2TrackAtOnce

IDiscMaster2

IDiscRecorder2

IFileSystemImage

IStream