Class Printers

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2528
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Class Printers

21 Mar 2019, 10:29

Class Printers

Description:
...todo

Github:
...todo

Source:

Code: Select all

Class Printers
{
	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/addprinterconnection
	; ===========================================================================================================================
	AddConnection(printer)
	{
		if !(DllCall("winspool.drv\AddPrinterConnection", "str", printer))
			return false
		return true
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/closeprinter
	; ===========================================================================================================================
	CloseHandle(handle)
	{
		if !(DllCall("winspool.drv\ClosePrinter", "str", handle))
			return false
		return true
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/deleteprinterconnection
	; ===========================================================================================================================
	DeleteConnection(printer)
	{
		if !(DllCall("winspool.drv\DeletePrinterConnection", "str", printer))
			return false
		return true
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/enumprinters
	; ===========================================================================================================================
	Enum(flags)
	{
		if !(DllCall("winspool.drv\EnumPrinters", "uint", flags, "ptr", 0, "uint", 2, "ptr", 0, "uint", 0, "uint*", size, "uint*", 0))
		{
			size := VarSetCapacity(buf, size << 1, 0)
			if (DllCall("winspool.drv\EnumPrinters", "uint", flags, "ptr", 0, "uint", 2, "ptr", &buf, "uint", size, "uint*", 0, "uint*", count))
			{
				addr := &buf, PRINTER_INFO_2 := []
				loop % count
				{
					PRINTER_INFO_2[A_Index, "ServerName"]      := StrGet(NumGet(addr + 0,                   "ptr"))
					PRINTER_INFO_2[A_Index, "PrinterName"]     := StrGet(NumGet(addr + A_PtrSize,           "ptr"))
					PRINTER_INFO_2[A_Index, "ShareName"]       := StrGet(NumGet(addr + A_PtrSize *  2,      "ptr"))
					PRINTER_INFO_2[A_Index, "PortName"]        := StrGet(NumGet(addr + A_PtrSize *  3,      "ptr"))
					PRINTER_INFO_2[A_Index, "DriverName"]      := StrGet(NumGet(addr + A_PtrSize *  4,      "ptr"))
					PRINTER_INFO_2[A_Index, "Comment"]         := StrGet(NumGet(addr + A_PtrSize *  5,      "ptr"))
					PRINTER_INFO_2[A_Index, "Location"]        := StrGet(NumGet(addr + A_PtrSize *  6,      "ptr"))
					;DevMode                                   :=        NumGet(addr + A_PtrSize *  7,      "ptr")
					; https://docs.microsoft.com/en-us/windows/desktop/api/Wingdi/ns-wingdi-_devicemodea
					PRINTER_INFO_2[A_Index, "SepFile"]         := StrGet(NumGet(addr + A_PtrSize *  8,      "ptr"))
					PRINTER_INFO_2[A_Index, "PrintProcessor"]  := StrGet(NumGet(addr + A_PtrSize *  9,      "ptr"))
					PRINTER_INFO_2[A_Index, "Datatpye"]        := StrGet(NumGet(addr + A_PtrSize * 10,      "ptr"))
					PRINTER_INFO_2[A_Index, "Parameters"]      := StrGet(NumGet(addr + A_PtrSize * 11,      "ptr"))
					;SecurityDescriptor                        :=        NumGet(addr + A_PtrSize * 12,      "ptr")
					; https://docs.microsoft.com/de-de/windows/desktop/api/winnt/ns-winnt-_security_descriptor
					PRINTER_INFO_2[A_Index, "Attributes"]      :=        NumGet(addr + A_PtrSize * 13,      "uint")
					PRINTER_INFO_2[A_Index, "Priority"]        :=        NumGet(addr + A_PtrSize * 13 +  4, "uint")
					PRINTER_INFO_2[A_Index, "DefaultPriority"] :=        NumGet(addr + A_PtrSize * 13 +  8, "uint")
					PRINTER_INFO_2[A_Index, "StartTime"]       :=        NumGet(addr + A_PtrSize * 13 + 12, "uint")
					PRINTER_INFO_2[A_Index, "UntilTime"]       :=        NumGet(addr + A_PtrSize * 13 + 16, "uint")
					PRINTER_INFO_2[A_Index, "Status"]          :=        NumGet(addr + A_PtrSize * 13 + 20, "uint")
					PRINTER_INFO_2[A_Index, "Jobs"]            :=        NumGet(addr + A_PtrSize * 13 + 24, "uint")
					PRINTER_INFO_2[A_Index, "AveragePPM"]      :=        NumGet(addr + A_PtrSize * 13 + 28, "uint")
					addr += A_PtrSize * 13 + 32
				}
				return PRINTER_INFO_2
			}
		}
		return false
	}


	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/getdefaultprinter
	; ===========================================================================================================================
	GetDefault()
	{
		if !(DllCall("winspool.drv\GetDefaultPrinter", "ptr", 0, "uint*", size))
		{
			size := VarSetCapacity(buf, size << 1, 0)
			if !(DllCall("winspool.drv\GetDefaultPrinter", "str", buf, "uint*", size))
				return false
		}
		return buf
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/getprinter
	; ===========================================================================================================================
	GetInfo(printer)
	{
		if (handle := this.OpenHandle(printer))
		{
			if !(DllCall("winspool.drv\GetPrinter", "ptr", handle, "uint", 1, "ptr", 0, "uint", 0, "uint*", size))
			{
				size := VarSetCapacity(buf, size << 1, 0)
				if (DllCall("winspool.drv\GetPrinter", "ptr", handle, "uint", 1, "ptr", &buf, "uint", size, "uint*", 0))
				{
					PRINTER_INFO_1             := {}
					PRINTER_INFO_1.Flags       := NumGet(&buf + 0, "uint")
					PRINTER_INFO_1.Description := StrGet(NumGet(&buf + (A_PtrSize * 1), "uptr"), "utf-16")
					PRINTER_INFO_1.Name        := StrGet(NumGet(&buf + (A_PtrSize * 2), "uptr"), "utf-16")
					PRINTER_INFO_1.Comment     := StrGet(NumGet(&buf + (A_PtrSize * 3), "uptr"), "utf-16")
					this.CloseHandle(handle)
					return PRINTER_INFO_1
				}
			}
			this.CloseHandle(handle)
		}
		return false
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/openprinter
	; ===========================================================================================================================
	OpenHandle(printer)
	{
		if !(DllCall("winspool.drv\OpenPrinter", "ptr", &printer, "ptr*", handle, "ptr", 0))
			return false
		return handle
	}

	; ===========================================================================================================================
	; https://docs.microsoft.com/en-us/windows/desktop/printdocs/setdefaultprinter
	; ===========================================================================================================================
	SetDefault(printer)
	{
		if !(DllCall("winspool.drv\SetDefaultPrinter", "str", printer))
			return false
		return true
	}
}
Will add more functions later...
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1909) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
jNizM
Posts: 2528
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Class Printers

21 Mar 2019, 10:29

Examples:

Retrieves the default printer for the current user on the local computer

Code: Select all

MsgBox % Printers.GetDefault()

Sets the default printer for the current user on the local computer

Code: Select all

MsgBox % Printers.SetDefault("\\192.0.0.1\PRINTER_A")

Adds a connection to the specified printer for the current user

Code: Select all

MsgBox % Printers.AddConnection("\\192.0.0.1\PRINTER_A")

Deletes a connection to a printer that was established by a call to AddPrinterConnection or ConnectToPrinterDlg

Code: Select all

MsgBox % Printers.DeleteConnection("\\192.0.0.1\PRINTER_A")

Enumerates available printers, print servers, domains, or print providers

Code: Select all

PrintArr(Printers.Enum(0x2|0x4))

PrintArr(Arr, Option := "w1200 h800", GuiNum := 90)
{
	for index, obj in Arr {
		if (A_Index = 1) {
			for k, v in obj {
				Columns .= k "|"    
				cnt++
			}
			Gui, %GuiNum%: Margin, 5, 5
			Gui, %GuiNum%: Add, ListView, %Option%, % Columns
		}
		RowNum := A_Index        
		Gui, %GuiNum%: default
		LV_Add("")
		for k, v in obj {
			LV_GetText(Header, 0, A_Index)
			if (k <> Header) {    
				FoundHeader := False
				loop % LV_GetCount("Column") {
					LV_GetText(Header, 0, A_Index)
					if (k <> Header)
						continue
					else {
						FoundHeader := A_Index
						break
					}
				}
				if !(FoundHeader) {
					LV_InsertCol(cnt + 1, "", k)
					cnt++
					ColNum := "Col" cnt
				} else
					ColNum := "Col" FoundHeader
			} else
				ColNum := "Col" A_Index
			LV_Modify(RowNum, ColNum, (IsObject(v) ? "Object()" : v))
		}
	}
	loop % LV_GetCount("Column")
		LV_ModifyCol(A_Index, "AutoHdr")
	Gui, %GuiNum%: Show,, Array
}

...todo
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1909) x64 | [GitHub] Profile
Donations are appreciated if I could help you
gregster
Posts: 3378
Joined: 30 Sep 2013, 06:48

Re: Class Printers

21 Mar 2019, 10:53

Cool! Thank you. I will definitely use that.
DRocks
Posts: 524
Joined: 08 May 2018, 10:20

Re: Class Printers

21 Mar 2019, 13:19

Does that mean that you can Print with AHK scripts, sorry my ignorance

E.g. : Print content of a Gui window ?
iPhilip
Posts: 428
Joined: 02 Oct 2013, 12:21

Re: Class Printers

21 Mar 2019, 17:03

For reference:

Code: Select all

PRINTER_ENUM_DEFAULT     = 0x00000001
PRINTER_ENUM_LOCAL       = 0x00000002
PRINTER_ENUM_CONNECTIONS = 0x00000004
PRINTER_ENUM_FAVORITE    = 0x00000004
PRINTER_ENUM_NAME        = 0x00000008
PRINTER_ENUM_REMOTE      = 0x00000010
PRINTER_ENUM_SHARED      = 0x00000020
PRINTER_ENUM_NETWORK     = 0x00000040
PRINTER_ENUM_EXPAND      = 0x00004000
PRINTER_ENUM_CONTAINER   = 0x00008000
PRINTER_ENUM_ICONMASK    = 0x00ff0000
PRINTER_ENUM_ICON1       = 0x00010000
PRINTER_ENUM_ICON2       = 0x00020000
PRINTER_ENUM_ICON3       = 0x00040000
PRINTER_ENUM_ICON4       = 0x00080000
PRINTER_ENUM_ICON5       = 0x00100000
PRINTER_ENUM_ICON6       = 0x00200000
PRINTER_ENUM_ICON7       = 0x00400000
PRINTER_ENUM_ICON8       = 0x00800000
PRINTER_ENUM_HIDE        = 0x01000000
PRINTER_ENUM_CATEGORY_ALL= 0x02000000
PRINTER_ENUM_CATEGORY_3D = 0x04000000
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jNizM
Posts: 2528
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Class Printers

22 Mar 2019, 03:14

DRocks wrote:
21 Mar 2019, 13:19
Does that mean that you can Print with AHK scripts, sorry my ignorance

E.g. : Print content of a Gui window ?
Yes it is possible to print directly from Gui content like an Edit-Field.
Will add an example later.
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1909) x64 | [GitHub] Profile
Donations are appreciated if I could help you
DRocks
Posts: 524
Joined: 08 May 2018, 10:20

Re: Class Printers

22 Mar 2019, 06:26

jNizM wrote:
22 Mar 2019, 03:14
Yes it is possible to print directly from Gui content like an Edit-Field.
Will add an example later.
Wow. This is going to fill a hole! Thank you very much for this. I cant wait to have free time to experiment with the class and see your example.

I was going to need to learn html and css to print but maybe this will make it ! :D

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 83 guests