[Class] Printers

Post your working scripts, libraries and tools for AHK v1.1 and older
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: [Class] Printers

16 Jul 2021, 15:12

ostius wrote:
09 Jul 2021, 10:04
Hi Bkid, thank you for the code! I can now send ZPL to a Zebra ZD410 printer. Do you know of a way to query the printer status? I can send an ~HS command using the Zebra communication tool but would like to integrate that functionality into an AHK program.
Sorry for the delay. I'll be honest, I don't check the forums all too often. :P Anyway, I'm not sure exactly how you would get that information. I know how to send data *to* the printer, but I'm not exactly sure how you would receive any kind of response *back*. There's a ReadPrinter function that could potentially do it, but I haven't researched it enough to know for sure, and it's not part of the AHK class so I'd have to write it..

Edit: I don't know if ReadPrinter is going to do what you're needing after all.. Looking at the Direct Communication option Zebra has, it must communicate with the printer on some kind of driver level that we can't see, as it doesn't seem to send commands through the print queue. This would make sense, as it requires two-way communication. I'm not sure how this can be replicated, as it doesn't use the print queue and it doesn't use a COM port.
RussF
Posts: 1301
Joined: 05 Aug 2021, 06:36

Re: [Class] Printers

03 Aug 2023, 14:34

I've written a script in V2 that prints labels on a Zebra printer. Currently, I write the ZPL codes to a text file, then Run an external DOS utility to send the file to a printer. It works well, but I would like to have the script self contained. When I came across this Printers class, I was quite excited. I've been trying to convert it to V2, but I'm still in the very early stages of learning DLLCalls. @jNizM, have you by any chance converted this to V2, or could you perhaps give me a couple of pointers?

Take for example, this code segment:

Code: Select all

	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 buf
		}
		return false
	}
V2 is throwing up warnings about size not having been defined, even if I turn warnings off. V1 (I think) treated an undefined variable as null, but threw no errors. What should the value of size be - zero or null or empty string?

"Size" first appears early in the method here:

Code: Select all

if !(DllCall("winspool.drv\GetDefaultPrinter", "ptr", 0, "uint*", size))
and I get a warning about it.

I understand that I need to change

Code: Select all

size := VarSetCapacity(buf, size << 1, 0)
to

Code: Select all

buf := Buffer(size << 1, 0)
but there's that "size" again, this time left shifted. What value, exactly, is shifted one bit to the left?

Any insight would be greatly appreciated.

Russ
iPhilip
Posts: 830
Joined: 02 Oct 2013, 12:21

Re: [Class] Printers

03 Aug 2023, 16:08

@RussF, The code below should work with AutoHotkey v2:

Code: Select all

	GetDefault()
	{
		if !(DllCall("winspool.drv\GetDefaultPrinter", "ptr", 0, "uint*", &size := 0))
		{
			buf := Buffer(size << 1, 0)
			if (DllCall("winspool.drv\GetDefaultPrinter", "ptr", buf, "uint*", &size))
				return StrGet(buf)
		}
		return false
	}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
RussF
Posts: 1301
Joined: 05 Aug 2021, 06:36

Re: [Class] Printers

03 Aug 2023, 16:47

@iPhilip, but size is still not defined anywhere. Won't the var reference (&size) and the shift (size << 1) still flag an error? How can you reference a var that doesn't exist?

Edit - Oops, I missed the assignment "&size := 0" I will give this a try in the morning. Thanks.

Russ
iPhilip
Posts: 830
Joined: 02 Oct 2013, 12:21

Re: [Class] Printers

03 Aug 2023, 17:21

@RussF, Below is similar code with comments:

Code: Select all

	GetDefault()
	{
		size := 0  ; Initialize the "size" variable. 
		if !(DllCall("winspool.drv\GetDefaultPrinter", "ptr", 0, "uint*", &size))  ; Write to the "size" variable the number of characters necessary to store the default printer name, including the terminating null character.
		{
			buf := Buffer(size << 1, 0)  ; Create a buffer twice the size of "size". The << 1 operation is just another way to multiply by 2. AutoHotkey v2 stores strings using WCHARS (two bytes per character).
			if (DllCall("winspool.drv\GetDefaultPrinter", "ptr", buf, "uint*", &size))  ; Store the name of the default printer in the buffer "buf".
				return StrGet(buf)  ; Get the string from the buffer "buf" and return it.
		}
		return false
	}
I hope this helps.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: [Class] Printers

17 Aug 2023, 14:43

Thanks, tested on Win 7 and worked great!
MikeOrlando
Posts: 22
Joined: 05 Jun 2017, 08:18

Re: [Class] Printers

02 Sep 2023, 17:27

I started a post over in the AHK v2 forum and am excited to hear if anyone has success stories they are willing to share in printing ZLP directly to a Zebra printer using this class.

> viewtopic.php?f=82&t=121050


Thanks,
Mike

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 97 guests