How to determine if win32api.ShellExecute was successful using hinstance?

前端 未结 2 649
一向
一向 2021-01-14 01:33

I\'ve been looking around for an answer to my original issue.. how do i determine (programmatically) that my win32api.ShellExecute statement executed successfully, and if a

2条回答
  •  耶瑟儿~
    2021-01-14 01:59

    With ShellExecute, you will never know when the printing is complete, it depends on the size of the file and whether the printer driver buffers the contents (the printer might be waiting for you to fill the paper tray, for example).

    According to this SO answer, it looks like subprocess.call() is a better solution, since it waits for the command to complete, only in this case you would need to read the registry to obtain the exe associated with the file.

    ShellExecuteEx is available from pywin32, you can do something like:

    import win32com.shell.shell as shell
    param = '/d:"%s"' % printer
    shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=pfile, lpParameters=param)
    

    EDIT: code for waiting on the handle from ShellExecuteEx()

    import win32com.shell.shell as shell
    import win32event
    #fMask = SEE_MASK_NOASYNC(0x00000100) = 256 + SEE_MASK_NOCLOSEPROCESS(0x00000040) = 64
    dict = shell.ShellExecuteEx(fMask = 256 + 64, lpFile='Notepad.exe', lpParameters='Notes.txt')
    hh = dict['hProcess']
    print hh
    ret = win32event.WaitForSingleObject(hh, -1)
    print ret
    

提交回复
热议问题