Im currently trying to setup fake printer on server to obtain files and store them on disc to use them in another app. I need to intercept print request from other computer when it choose this fake printer and use windows build in print action (ctrl + p).
Now printer is added, and is visible through network -> server computer -> printer. I can use it as printing device for example in word document. On a server i can run powershell script for only monitoring port and already created print jobs.
I have seen a possibility to add script in this window, but there should be also option for it and im thinking that should be albe to intercept request at its start.
configue tcp/ip port window
Have anybody else wanted to intercept printer jobs to acces files? Does windows server need additional roles or options to be able to do that? Or maybe i should add specific driver for fake printer. I have seen hp universal print monitor port but i didnt use it. I have created tcp/ip port.
Also i heard about custom print processor but its is also black magic for me haha.
I used console app in c# to also monitor jobs but cannot extract actual file from it. I dont think it is usefull but it showed me each print job request to printers or fake printers throught server
#include <windows.h>
#include <stdio.h>
#include <winspool.h>
#pragma comment(lib, "winspool.lib")
#define MAX_PRINTER_NAME 256
#define MAX_JOB_NAME 256
void SavePrintJobData(HANDLE hPrinter, JOB_INFO_2* pJobInfo) {
DWORD dwNeeded = 0, dwReturned = 0;
BYTE* pJobData = NULL;
if (!GetJob(hPrinter, pJobInfo->JobId, 2, NULL, 0, &dwNeeded)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
pJobData = (BYTE*)malloc(dwNeeded);
if (pJobData) {
if (GetJob(hPrinter, pJobInfo->JobId, 2, pJobData, dwNeeded, &dwReturned)) {
JOB_INFO_2* pFullJobInfo = (JOB_INFO_2*)pJobData;
char filename[MAX_PATH];
sprintf_s(filename, sizeof(filename), "C:\PrintJobs\%s", pFullJobInfo->pDocument);
FILE* file;
if (fopen_s(&file, filename, "wb") == 0) {
if (pFullJobInfo->pDatatype && strcmp(pFullJobInfo->pDatatype, "RAW") == 0) {
// Próba pobrania surowych danych zadania
DWORD cbWritten;
if (ReadPrinter(hPrinter, file, pFullJobInfo->Size, &cbWritten)) {
printf("Raw data saved to: %sn", filename);
}
else {
printf("Error reading raw data.n");
}
}
else {
printf("Jos isn in raw format.n");
}
fclose(file);
}
else {
printf("Error open file to save.n");
}
}
free(pJobData);
}
}
}
}
void MonitorPrintJobs() {
PRINTER_NOTIFY_INFO* pInfo = NULL;
PRINTER_NOTIFY_OPTIONS options;
HANDLE hChange = NULL;
DWORD dwChange;
PRINTER_NOTIFY_OPTIONS_TYPE types[1];
types[0].Type = JOB_NOTIFY_TYPE;
types[0].Reserved0 = 0;
types[0].Reserved1 = 0;
types[0].Reserved2 = 0;
types[0].Count = 1;
types[0].pFields = (PWORD)calloc(1, sizeof(WORD));
types[0].pFields[0] = JOB_NOTIFY_FIELD_DOCUMENT;
options.Version = 2;
options.Flags = 0;
options.Count = 1;
options.pTypes = types;
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS defaults = { NULL, NULL, PRINTER_ACCESS_USE };
if (OpenPrinter(NULL, &hPrinter, &defaults)) {
hChange = FindFirstPrinterChangeNotification(hPrinter, PRINTER_CHANGE_ALL, 0, &options);
if (hChange != INVALID_HANDLE_VALUE) {
while (TRUE) {
dwChange = WaitForSingleObject(hChange, INFINITE);
if (dwChange == WAIT_OBJECT_0) {
DWORD dwResult;
if (FindNextPrinterChangeNotification(hChange, &dwResult, &options, (LPVOID*)&pInfo)) {
for (DWORD i = 0; i < pInfo->Count; i++) {
if (pInfo->aData[i].Type == JOB_NOTIFY_TYPE && pInfo->aData[i].Field == JOB_NOTIFY_FIELD_DOCUMENT) {
JOB_INFO_2* pJobInfo = NULL;
DWORD cbNeeded = 0, cReturned = 0;
EnumJobs(hPrinter, 0, 1, 2, NULL, 0, &cbNeeded, &cReturned);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
pJobInfo = (JOB_INFO_2*)malloc(cbNeeded);
if (EnumJobs(hPrinter, 0, 1, 2, (LPBYTE)pJobInfo, cbNeeded, &cbNeeded, &cReturned)) {
SavePrintJobData(hPrinter, pJobInfo);
}
free(pJobInfo);
}
}
}
}
FindNextPrinterChangeNotification(hChange, NULL, NULL, NULL);
}
}
FindClosePrinterChangeNotification(hChange);
}
ClosePrinter(hPrinter);
}
free(types[0].pFields);
}
int main() {
MonitorPrintJobs();
return 0;
}
Thank you in advance for your answer.
Obalony Pivvot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.