Writing a virtual printer driver for windows - windows

I am new to windows development and I am trying to write a user-mode windows(XP, Vista & 7) virtual printer driver. My aim is to intercept the output being sent to the hardware printer by a third party app and add some extra data(text + graphics) to that output towards the end of the output. Then send the final payload to be printed by the hardware printer. Note that my data would not be added to every print out from the machine but just from a particular third party app.
I want to add my extra data to the print output before it gets converted to any Page Description Language(PDL). Can I do this? Would I be able to predictably add my extra data at the end of the output with proper formatting? If yes, then what kind of driver would I need to write and at what layer of the architecture ?
Is there a better way to do the same thing then writing a user-mode printer driver?
Finally, is there a sample code, online blog or book which can help me with this ?

I think you'd be better off getting the application to write to a generic Postscript driver and post-processing the resulting Postscript rather than trying to make sense of the data written to a printer driver; there are excellent open-source tools available for manipulating Postscript.
This page describes setting up a driver on Windows that will produce Postscript although you will want to do something else with the Postscript other than sending it to another printer as described there.

I don't think you need a driver. Just use the standard Postscript driver provided with the WDK, adding .PPD and .INF files to name it and specify characteristics, and then put your code in a port monitor. Port monitors are considerably easier to build and maintain than print drivers.

Related

Triggering Powershell Program on barcode scan event windows 10

I am looking for using barcode capture event to executing a program (either powershell or simple vbscript) on Windows 10 machine. How can I achieve that? Do I need to create a barcode program embedded in it - if yes, how will it execute? Do I need to capture the scan event in windows machine and write a logic on scan event to execute program - is that possible?
I am not finding any help on internet. Please help.
There is no mechanism that can be called by simply registering an event handler.
Based on the mode and mechanism of the scanner device you are using, you need to create an application program that handles it.
Hardware type and mode
Keyboard input emulation
Serial (or Bluetooth) port connection
Camera image recognition
programming
The application handles the device directly
Libraries and APIs
OPOS
POS for.NET
Windows.Devices.PorintOfService
JavaPOS
Various libraries that recognize barcodes from cameras/images
At a minimum, you need to open the device or register a keystroke event handler.
In addition, it will be necessary to send a command to start/stop scanning of the scanner, to distinguish between human-operated keyboard input and notification from the scanner.
It's not impossible to implement them in PowerShell, but it's likely to be difficult and slow.

Ghostscript PDF printing

I would like to make my own "PDF printer" using Ghostscript.
Most of solutions I find on the internet involve a 3rd party like Redmon to redirect the PS printer to the Gs executable.
Is there a way to do the same without a 3rd party (other than Gs) ?
Here is my 2 ideas:
Bypass the printing driver and generate Postscript directly by software (From GDI to postscript, using maybe a custom device context like the Metafile device context exist)
Create programmatically a new local port for each document to convert. Maybe using this technique : https://stackoverflow.com/a/1331777/99276
The first solution would be great, but can't find anything about it.
And I am efraid the second is not efficient at all (and detect when the document is fully printed can be tricky)
Update: I used print apis to print in a temp file (the print is synchronous) and then convert the PS file with Gs
Firstly; note that the Windows printing system has been redesigned in Windows Vista and beyond. While the old system still works in WIndows 7 and 8, I am unsure of exactly how usable it is in Windows 10. In Windows 10 everything goes via XPS, and I think (could be wrong) that PostScript is generated by going via XPS first.
Your first idea doesn't sound plausible to me. How can you have a printer (and allow your application to print to it) if you don't have a printer driver ? Even if you could do so, why would you create PostScript from GDI just to send it to Ghostscript and have it create PDF ? It would be far more reasonable to create PDF directly.
In the second case I don't see how adding a new local port would help you at all. Ports are simply where the output of the printer driver is directed. What a Port Monitor (RedMon is a Port Monitor) does is redirect the data stream sent to the port, and 'do something' with it. In the case of Ghostscript + RedMon the 'something' is create a PDF file.
I'm unclear why you don't want to use RedMon, given that you are apparently happy with using Ghostscript. However, if you really don't want to do that then write your own Port Monitor. The process is documented in the Windows Device Driver Development Kit and isn't especially hard. If you get stuck you can even look at the RedMon code as a guide. For example, see here

Terminal program to CSV

I have a little project at work, but I'm stuck in my tracks at the moment. We have quite a few Motorola MC1000 hand held barcode scanners at work, doing nothing at the moment, and I want to get them working again for stock takes and what not.
Now, these were pre-installed with a program called MCA, which is a simple barcode scanner and quantity program, which I'm not sure where it saves this information (maybe in memory), but I am unable to get the data from just connecting the device via USB.
On the program, there is a section called "send data" which then outputs data via COM1 port. I have managed to intercept the old program we used to read the barcodes (there is no way to export from this old program), so I need something new, where I can export barcode, quantity via CSV.
Using serialmon, I have found that I can read the barcode, plus some extra confirmations? then the quantity, plus more confirmations, then the next barcode.
Now I need to know if there is a program out there already that can connect to the com1 port, and then read/respond to the device to download all the barcodes and quantities and then export this to a csv, or even if a simple command prompt bat file could do this?
After some testing earlier, I know the device needs to receive an ack in some way as it times out without this (I could manually do this by sending %0 on a regular basis).
Thanks in advance.

Developing a custom printer driver for Windows

For the needs of my software I need to create a printer driver which will allow me to save all the images which are sent to it to files and then open my program's window letting the user to do something with the rendered pages.
I have found this article, but the sample uses the FILE: port, while I need to make my own port, dump the images to files and run an executable. However, I think I can accomplish my task by doing that in the driver's code, but I am not really sure that it is a good decision, since it is a driver, even though it is a user-mode driver.
I would be glad to hear any advice on my problem. Thanks in advance.
You need a port monitor, not a driver. See my answer to this question. You can use RedMon, but I recommend using the sample port monitor found in the WDK instead.
If you are OK with using XPS as format you can use the Github project: Microsoft/Windows-driver-samples/print/XPSDrvSmpl
https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl
Installer: http://wixtoolset.org/
Application: Listen to internal port
Flow: Install printer and application from a single installer. User prints something with your driver and while the application listens to the internal port. When data is sent the application picks it up. This is for XPS, can be converted to PDF, but the flow is similar no matter what you are printing. If you need anything else check out Microsoft/Windows-driver-samples/print/ on GitHub or other sources specific to your needs.

Making a soft copy(file) of everything printed to any printer from a Windows workstation?

I have been looking into the possibility of creating a soft copy(image/EMF file) of everything printed from Windows - for archival purposes. Does anyone know if it is possible to create a hooking DLL that can grab the printed data in such a general way?
A low tech way of solving it might be to install pdf printer driver as the default printer and remove all others and set it up to automatically write all the files to certain directory on the network and then write a tiny app on another computer to monitor that folder for changes and if any new pdfs appear just print them out to a real printer.
Edit: Otherwise there's apparently something called the Print Monitor API. Here's an article that describes using that from VC++ 6 and seems to be pretty much what you want (assuming it's still supported by the OS you use).
Having looked at this problem in more detail the best solution seems to handle it through Spooler notifications in the Win32.

Resources