3

I'm learning about printing a file in python. I found many ways to do this, One of the most common ways I've seen is using the win32api module.

import win32api
win32api.ShellExecute(0, "print", path_for_file , None, ".", 0)

When I run this program, the file gets printed without any problems.

But the thing is that I'm not understanding what is actually going on in the win32api.ShellExecute() function and what are the functions of it's arguments. By arugments, I mean this: (0, "print", path_for_file , None, ".", 0)

Can anyone please explain what does each and every argument inside the win32api.ShellExecute() function do?

It would be great if anyone could help me out.

Lenovo 360
  • 569
  • 1
  • 7
  • 27
  • 1
    are you looking for the corresponding winapi doc? https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew – Simon Mourier Jan 24 '21 at 07:18
  • As I said, if you don't know what window ownership is, and it's implications for dialogs, then you aren't going to get an answer in this format. You need to do some research. – David Heffernan Jan 24 '21 at 09:04
  • That said, your approach to printing is so flaky that parent window handles are the very least of your concerns. – David Heffernan Jan 24 '21 at 09:05

1 Answers1

3

Based on the ShellExecute documentation:

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

In brief, this performs the same action as right-clicking the path_for_file document in Windows Explorer, then choosing print from the context menu. The app associated with the file type is executed with the print verb and SW_HIDE show command, which normally means it will print the document silently, without displaying any UI.

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • 1
    Hi, thanks for explaining, but I did not understand the use of the first, fourth and fifth argument. Could you please explain the functions of three arguments in a little more detail? – Lenovo 360 Jan 24 '21 at 08:17
  • 2
    @Lenovo360 For the 1st see [Handle for ShellExecute() - Parent Window?](https://stackoverflow.com/questions/315527) (tl;dr most often `NULL`). For the 4th, if you used `ShellExecute` to run an executable, instead of a verb-by-association, then those parameters would be passed to the target executable on the command line (n/a when launching a document file). More about shell objects and verbs at [Launching Applications](https://learn.microsoft.com/en-us/windows/win32/shell/launch) for example. – dxiv Jan 24 '21 at 08:26
  • 2
    ... For the 5th, that's used to set the [current working directory](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory#remarks) for the target process, and passing a full path there would allow `path-to-file` to use a relative path. However, in your example `"."` *is* the current working directory, so it has the same effect as passing `NULL`, instead. – dxiv Jan 24 '21 at 08:26