Python如何使用IMAPI2监视进程刻录光盘

2024-10-03 15:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

用pim/pim编写程序的程序。 我使用了以下示例:codeplex和{a2}。在

import comtypes.client as cc

class MyProgram(object):
    def main(self):
        # index of recording drive
        index = 0

        # files to transfer to disc
        path = r'D:\Example'

        # DiscMaster2 object to connect to optical drives
        discMaster = cc.CreateObject('IMAPI2.MsftDiscMaster2')
        # cc.ShowEvents(discMaster) ### uncomment for print events

        # DiscRecorder object for specified burning device
        recorder = cc.CreateObject('IMAPI2.MsftDiscRecorder2')
        uniqueId = discMaster.Item(index)
        recorder.InitializeDiscRecorder(uniqueId)

        # create an image stream for a specified directory
        # create a new file system image and retrieve root directory
        _fsi = cc.CreateObject('IMAPI2FS.MsftFileSystemImage')
        _dir = _fsi.Root
        _fsi.VolumeName = 'TestName'

        # create the new disc format and set the recorder
        dataWriter = cc.CreateObject('IMAPI2.MsftDiscFormat2Data')
        # cc.ShowEvents(dataWriter) ### uncomment for print events
        dataWriter.Recorder = recorder
        dataWriter.ClientName = 'MyClient'
        dataWriter.ForceOverwrite = True

        _fsi.ChooseImageDefaults(recorder)

        # get events
        sink = Sink()
        _events = cc.GetEvents(dataWriter, sink)
        # print(_events)  ### <comtypes.client._events._AdviseConnection object at 0x...>

        # add the directory and its content to the file system
        _dir.AddTree(path, False)

        # create an image from the file system
        _result = _fsi.CreateResultImage()
        _stream = _result.ImageStream

        # write disc using the specified recorder
        print('Writing content to disc...')
        dataWriter.Write(_stream)
        print('Finished writing content')

使用名为“dataWriter”事件的方法创建自定义类:

^{2}$

然后运行这个脚本:

prog = MyProgram()
prog.main()

我可以写CD/DVD,但不能监视进程,我不明白为什么。在

有人知道吗?在

非常感谢。在


Tags: thetoforindexobjectcreateeventscc