last day (18 days later) » 

4:27 PM
5
A: How to catch printer event in python

eryksunTo be notified of new print jobs, you can use FindFirstPrinterChangeNotification, FindNextPrinterChangeNotification, and a wait function from kernel32 such as WaitForSingleObject. Here's an example to set a filter that waits for a new print job on the local print server. There's much more work t...

 
Some of the things don't seem to be defined such as LPHANDLE
 
@MalikBrahimi, the example should work in 2.x now. I also added more rigorous error checking.
 
@eryksun This is awesome, the most helpful thing on SO. Do you mind adding some comments as to what each portion does?
Additionally, after waiting for the first notification event, how can you obtain the id of the caught job?
 
@MalikBrahimi, I updated the answer.
 
@eryksun Damn that's a lot. How did you get to be so proficient with both ctypes and the Windows API?
 
4:27 PM
@MalikBrahimi, Stack Overflow says I have 124 ctypes answers. I also answer ctypes-related questions on python-list, python-tutor, and the (mostly inactive now) ctypes-users. Plus I contribute to resolving ctypes and Windows issues on Python's issue tracker.
 
Could you maybe post here a better answer?
I want to catch the event when print servers or USB printers actually receive a print job. I believe that it is PRINTER_CHANGE_WRITE_JOB but correct me if I'm wrong.
How can I reduce your code to just the functionality I need?
Correct me if I'm wrong, but I don't think we need to specify argument types as long as we use the defaults nor do we need to really check for timeouts either as long as we're in a continuous loop anyway.
@eryksun I would really appreciate your help here.
 
 
2 hours later…
6:22 PM
The default integer conversion type is a 32-bit C int. Thus restype is necessary for pointer results to be 64-bit safe. For example, the RPC handle returned by FindFirstPrinterChangeNotification is a pointer to data for the interprocess communication. Maybe Windows always allocates the RPC data from the lower 32-bit address space, but I'd rather be safe and declare it.
You can avoid argtypes if and only all pointers are passed as ctypes objects. If any pointer is passed as a Python integer, then argtypes is required. If you're sane you'll also keep the errcheck functions to get formatted exceptions using the safe get_last_error mechanism (do not rely on GetLastError using ctypes).
 
Is there a way to minimize the code?
I just want to keep the stuff I need. And is WRITE_JOB the right event?
 
For the structures with Count and a pointer / incomplete structure, I added properties for convenience and safety. They give you sized arrays. It's way too easy to crash Python when working directly with pointers. But really I don't know why you'd want to reduce this. It's not that much code. you can use a private _helper module with an __all__ to import * just the main functions and constants that you need.
 
This is for a school project and I need to maintain my own code.
I'm just trying to understand so I can do stuff on my own.
So if I wanted to handle the event when a print server or USB printer receives a job I would use PRINTER_CHANGE_WRITE_JOB right?
 
I could set up wait_for_print_job to wait on arbitrary fdwFilter conditions. PRINTER_CHANGE_WRITE_JOB is for when the server writes data to the job, such as for spooling, etc.
What problems are you having with using PRINTER_CHANGE_ADD_JOB?
 
ADD_JOB seems to trigger immediately.
Regardless of whether you're online or offline, plugged in or out.
I need my event to call exclusively once the job is received, not during initial local spooling.
Do you understand what I'm trying to get at?
 
6:46 PM
I could experiment, but I don't think the write job notification will help in that regard. Why not use the status that shows when a job is spooling, printing, and printed?
 
I'm creating a program for high volume networks.
I simply want to know the job id of a print job as it arrives on the print server.
It doesn't need to print immediately.
Does that make sense?
 
7:15 PM
@eryksun Is that incorrect?
 
I can confirm that PRINTER_CHANGE_WRITE_JOB is not what you want. It signals while spooling when the printer is offline and then doesn't signal once the printer is back online and the job begins printing.
 
So how do I achieve the functionality I have in mind?
 
For your setup, what does the job status and job position do normally compared to when the print server is offline?
 
What do you mean?
I simply want to trigger a callback function into which I pass the job id as soon as the print job reaches the print server or USB printer directly.
Basically, nothing happens when the print server is offline.
 
7:38 PM
The job status should show as error and offline, e.g. job_status: 0x002032 (ERROR,OFFLINE,PRINTING,RETAINED). As soon as the printer is online again the status should change to printing, assuming it's queued in position 1. otherwise as soon as it advances in the queue to position 1 the status should change to printing.
 
Yes, but how do I determine the id of a job that is just received?
@eryksun And by the way, I would suggest you put that script on PyPI as winspl or something.
So again ....
 
You're notified with the job ID of every print job as it's added to the queue of the printer, or local server when printer_name is None.
 
Okay, so how do I do that?
let me explain again.
I have several network printers.
As soon as any of these printers receives a print job, I want to take the id and pass it in a function.
How do I achieve that with code?
At the moment, you are only confusing me further.
But I do appreciate your help.
*I have several network printers.*
As soon as any of these printers receives a print job, I want to take the id and pass it in a function.
How do I achieve that with code?*
 
7:55 PM
wait_for_print_job_info returns the job ID, so I'm not sure what the problem is.
 
The problem is which event am I supposed to handle?
As your code is written I have no idea what it's waiting for?
PRINTER_CHANGE_ADD_JOB?
PRINTER_CHANGE_WRITE_JOB?
Or something else?
What am I supposed to be waiting for to create this functionality I have in mind?
 
wait_for_print_job_info doesn't use those. Its fields parameter is a tuple of JOB_NOTIFY_FIELD_* fields from the table shown for the PRINTER_NOTIFY_INFO_DATA structure.
 
I'm not following.
Does it wait for all events?
 
When one of the specified fields notifications is available, hChange is signaled and WaitForSingleObject returns. I set the default timeout to wait forever, but in my example I use 0.25 seconds so as to not block the main thread (otherwise Ctrl+C wouldn't work)
 
Okay, so how do I make the program work to handle only that event in which the job is received by the print server?
Something in this format:
while True:
    job = WaitForEvent(PRINT_SERVER_RECEPTION)
print job.id
Listen man, this is confusing as hell.
And please explain from layman's perspective.
I need wait for jobs until they reach a print server.
That's all I need. I don't need this extraneous code, and you're confusing, irrelevant explanations.
I'm sorry to be rude about it. But I'm been struggling with this for months, and I'm starting to lose you, who has been my one solid hope.
All I want to do is wait for jobs as they are received by the print server.
 
8:14 PM
You've added the complication that you want to ignore the print job while the printer is offline. Even in that you'll see notifications for the new print job, e.g. the printer name, document name, submission time, queue position, etc. But if it's in queue position 1, instead of the status going from spooling (8) to printing (16), it goes to error (2) (other status flags can be OR'd in here).
 
I don't know what that means man.
I just want to know how to implement this feature in code.
Would this help at all?
 
8:41 PM
@eryksun How can I do this?
 

  last day (18 days later) »