如何使用 hinstance 确定 win32api.ShellExecute 是否成功? [英] How to determine if win32api.ShellExecute was successful using hinstance?

查看:52
本文介绍了如何使用 hinstance 确定 win32api.ShellExecute 是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找原始问题的答案..我如何确定(以编程方式)我的 win32api.ShellExecute 语句成功执行,如果成功执行,则执行 os.remove() 语句.

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 successful execution occurs, execute an os.remove() statement.

经过研究,我发现 ShellExecute() 调用返回 HINSTANCE.进一步挖掘我发现,如果成功,ShellExecute() 将返回 HINSTANCE > 32.我现在的问题/问题是,我如何使用它来控制程序流程的其余部分?我尝试使用 if HINSTANCE>32: 语句来控制下一部分,但我收到一个 NameError: name 'hinstance' is not defined 消息.通常这不会让我感到困惑,因为这意味着我需要在引用它之前定义变量 'hinstance';但是,因为我认为 ShellExecute 应该返回 HINSTANCE,所以我认为它可以使用?

Researching I found out that the ShellExecute() call returns the HINSTANCE. Further digging I found that ShellExecute() will return an HINSTANCE > 32 if it was successful. My problem/question now is, how do i use it to control the rest of my program's flow? I tried using an if HINSTANCE> 32: statement to control the next part, but I get a NameError: name 'hinstance' is not defined message. Normally this wouldn't confuse me because it means i need to define the variable 'hinstance' before referencing it; however, because i thought ShellExecute is supposed to return HINSTANCE, i thought that makes it available for use?

这是我尝试实现的完整代码.请注意,在我的 print_file() def 中,我将 hinstance 分配给完整的 win32api.ShellExecute() 命令,以尝试捕获 hinstance 并在函数结束时显式返回它......这也不起作用.

Here is my full code where i am trying to implement this. Note that in my print_file() def i am assigning hinstance to the full win32api.ShellExecute() command in attempt to capture the hinstance along with explicitly returning it at the end of the function.. this isn't working either.

import win32print
import win32api
from os.path import isfile, join
import glob
import os
import time

source_path = "c:\\temp\\source\\"

def main():
    printer_name = win32print.GetDefaultPrinter()
    while True:
        file_queue = [f for f in glob.glob("%s\\*.txt" % source_path) if isfile(f)]
        if len(file_queue) > 0:
            for i in file_queue:
                print_file(i, printer_name)
                if hinstance > 32:
                    time.sleep(.25)
                    delete_file(i)
                print "Filename: %r has printed" % i
                print
                time.sleep(.25)
                print                
        else:
            print "No files to print. Will retry in 15 seconds"
        time.sleep(15)


def print_file(pfile, printer):
    hinstance = win32api.ShellExecute(
        0,
        "print",
        '%s' % pfile,
        '/d:"%s"' % printer,
        ".",
        0
        )
    return hinstance


def delete_file(f):
    os.remove(f)
    print f, "was deleted!"

def alert(email):
    pass

main()

推荐答案

使用ShellExecute,你永远不知道什么时候打印完成,这取决于文件的大小和打印机驱动缓冲内容(例如,打印机可能正在等待您装满纸盘).

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).

根据 this SO answer,看起来 subprocess.call() 是一个更好的解决方案,因为它等待命令完成,只有在这种情况下,您才需要读取注册表以获取与文件关联的 exe.

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 可从 pywin32 获得,你可以这样做类似:

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)

<小时>

等待来自 ShellExecuteEx() 的句柄的代码


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

这篇关于如何使用 hinstance 确定 win32api.ShellExecute 是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆