0

I am making a program which has a functionality to print things. I have managed to put together this snippet which fetches the name of the default printer (if there is one):

import ctypes

buffer = ctypes.create_unicode_buffer(1024)
ctypes.WinDLL("winspool.drv").GetDefaultPrinterW(buffer, ctypes.byref(ctypes.c_ulong(1024)))

printerName = buffer.value

However I cannot figure out how to actually print the file with this. Using notepad.exe with the -P argument (or running a text file with the print verb) it can print but it opens a notepad window and such, which I want to be silent.

The print command does not allow direct printer names, it requires you to set an LPT port and use that.

If you have any clue how to print the file, now that I have the printer name (It's a txt file if that matters) I would greatly appreciate it!

DeKrypt
  • 33
  • 4
  • Any reason for using *CTypes* and not [\[GitHub\]: mhammond/pywin32 - Python for Windows (pywin32) Extensions](https://github.com/mhammond/pywin32)? Also check [\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/a/58611011/4788546) for a common pitfall when working with *CTypes* (calling functions). – CristiFati Feb 07 '23 at 07:53
  • Hi! Yes, I actually got this working with pywin32... I'm trying to keep this program to the base library, though. – DeKrypt Feb 07 '23 at 12:28
  • So, if you got it working what's the question's purpose? – CristiFati Feb 07 '23 at 12:33
  • I'd like to use only the base library if possible. – DeKrypt Feb 07 '23 at 21:38
  • Ok then, but you only need to replace the calls from one module with the other. Is there a particular reason why only the standard library is required? Can you share the current code (that works)? – CristiFati Feb 08 '23 at 05:54

0 Answers0