This project implements a virtual printer in Windows using a robust architecture based on a Port Monitor and a Windows Service.
- Port Monitor (
VirtualPrinterPortMonitor.dll): A DLL loaded by the Windows Spooler that intercepts print data and sends it to the control service via Named Pipes. - Windows Service (
VirtualPrinterService.exe): A background service that receives the print data, saves it toC:\Temp\PrintJobs, and monitors the health of the printing subsystem. - Installer (
VirtualPrinter.inf): An installation script to register the port monitor with the system.
- Visual Studio 2019 or later (with the "Desktop development with C++" workload).
- CMake 15 or later.
Open a Developer Command Prompt for Visual Studio.
Create a build directory:
sh mkdir build cd build
Run CMake to generate the Visual Studio project files:
sh cmake ..
Compile the project:
sh cmake --build . --config Release
The binaries (VirtualPrinterPortMonitor.dll and VirtualPrinterService.exe) will be located in build\Release.
Install the Service:
Navigate to the build directory (build\Release) in an administrator terminal and run:
sh VirtualPrinterService.exe install
Start the Service:
sh sc start VirtualPrinterService
Install the Port Monitor:
Copy VirtualPrinterPortMonitor.dll to C:\Windows\System32. Then, navigate to the source code's Installer directory, right-click on VirtualPrinter.inf and select "Install".
Create the Virtual Printer:
Run the following command in an administrator terminal to create the printer. It will use the standard Microsoft XPS driver and associate it with our port.
sh powershell -Command "Add-Printer -Name 'Virtual Printer' -DriverName 'Microsoft XPS Class Driver v4' -PortName 'VIRTUAL_PORT:'"
Note: If the VIRTUAL_PORT: does not appear in the list of existing ports, restart the Print Spooler service (spoolsv) to load our new monitor: net stop spooler && net start spooler.
Open any application (Notepad, Word, etc.) and print a document.
Select the printer named "Virtual Printer".
After printing, look for a new file in the C:\Temp\PrintJobs\ directory. The file will contain the raw print data.
Check the Windows Event Viewer (under Windows Logs -> Application) for logs generated by the service.
Delete the Printer:
sh powershell -Command "Remove-Printer -Name 'Virtual Printer'"
Stop and Delete the Service:
sh sc stop VirtualPrinterService sc delete VirtualPrinterService
Uninstall the Port Monitor:
* Restart the Spooler service: net stop spooler && net start spooler.
* Delete the file C:\Windows\System32\VirtualPrinterPortMonitor.dll.
* Delete the registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Virtual Printer Port.