win32api.ShellExecute() 函数的作用是什么?

Len*_*360 5 python printing winapi

我正在学习如何用 python 打印文件。我发现了很多方法可以做到这一点,我见过的最常见的方法之一是使用该win32api模块。

import win32api
win32api.ShellExecute(0, "print", path_for_file , None, ".", 0)
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时,文件被打印出来,没有任何问题。

但问题是我不明白函数中实际发生了什么win32api.ShellExecute()以及它的参数的函数是什么。通过参数,我的意思是:(0, "print", path_for_file , None, ".", 0)

谁能解释一下win32api.ShellExecute()函数中的每个参数的作用吗?

如果有人能帮助我,那就太好了。

dxi*_*xiv 3

基于ShellExecute文档:

ShellExecute(0,              // NULL since it's not associated with a window
             "print",        // execute the "print" verb defined for the file type
             path_for_file,  // path to the document file to print
             None,           // no parameters, since the target is a document file
             ".",            // current directory, same as NULL here
             0)              // SW_HIDE passed to app associated with the file type
Run Code Online (Sandbox Code Playgroud)

简而言之,这与path_for_file在 Windows 资源管理器中右键单击文档,然后print从上下文菜单中进行选择执行的操作相同。print与文件类型关联的应用程序使用动词和show 命令执行SW_HIDE,这通常意味着它将静默打印文档,而不显示任何 UI。

  • @Lenovo360 第一个请参阅[Handle for ShellExecute() - Parent Window?](/sf/ask/22086921/)(tl;dr 最常见的是“NULL”)。第四,如果您使用“ShellExecute”来运行可执行文件,而不是逐个关联,那么这些参数将被传递到命令行上的目标可执行文件(启动文档文件时不适用)。有关 shell 对象和动词的更多信息,请参见 [启动应用程序](https://learn.microsoft.com/en-us/windows/win32/shell/launch)。 (2认同)
  • ...第五,用于设置[当前工作目录](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory#remarks)目标进程,并在那里传递完整路径将允许“文件路径”使用相对路径。但是,在您的示例中,“.”*是*当前工作目录,因此它与传递“NULL”具有相同的效果。 (2认同)