公司需要做个禁止打印的程序。我的思路如下:隐藏程序,程序的逻辑如下:枚举所有打印机,
获取每个打印机的变动消息,
获得接收到打印任务的id,
使用SetJob,撤销该打印任务。
...
memset(pPrinterName, 0, cbNeeded);
bRet = EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, NULL, 4, pPrinterName, cbNeeded, &cbNeeded, &cbReturned);
if(bRet == false)
return; for(DWORD d = 0; d < cbReturned; d++)
{
CreateThread(NULL, 0, PrinterThread, (LPVOID)(((PPRINTER_INFO_4)(pPrinterName + d*sizeof(PRINTER_INFO_4)))->pPrinterName), 0, NULL);
}
....DWORD CEmptyClipBoardDlg::PrinterThread(LPVOID lp_param)
{
LPTSTR p = (LPTSTR)lp_param;

HANDLE hPrinter = NULL; PRINTER_NOTIFY_OPTIONS * p_NotifyOptions = NULL;
PRINTER_NOTIFY_OPTIONS_TYPE arr_NotifyOptionsType[2];
PWORD pJobFields, pPrinterFields;
HANDLE hwndNotify = NULL;
DWORD dwChanged = 0; PRINTER_NOTIFY_INFO * p_OutPut = NULL; if(OpenPrinter(p, &hPrinter, NULL))
{
pJobFields = (PWORD)malloc(4 * sizeof(WORD));
pJobFields[0] = JOB_NOTIFY_FIELD_STATUS; 
pJobFields[1] = JOB_NOTIFY_FIELD_MACHINE_NAME;  
pJobFields[2] = JOB_NOTIFY_FIELD_TOTAL_PAGES;    
pJobFields[3] = JOB_NOTIFY_FIELD_DOCUMENT;
arr_NotifyOptionsType[0].Type = JOB_NOTIFY_TYPE ;
arr_NotifyOptionsType[0].pFields = pJobFields;
arr_NotifyOptionsType[0].Count = 4; p_NotifyOptions = new PRINTER_NOTIFY_OPTIONS; p_NotifyOptions->Count = 1;
p_NotifyOptions->Version = 2;
p_NotifyOptions->pTypes = arr_NotifyOptionsType;
p_NotifyOptions->Flags = PRINTER_NOTIFY_OPTIONS_REFRESH; if((hwndNotify = FindFirstPrinterChangeNotification(hPrinter, 0, 0, p_NotifyOptions)) != INVALID_HANDLE_VALUE)
{
while(hwndNotify != INVALID_HANDLE_VALUE)
{
if(WaitForSingleObject(hwndNotify, INFINITE) == WAIT_OBJECT_0)
{
FindNextPrinterChangeNotification(hwndNotify, &dwChanged, (LPVOID)p_NotifyOptions, (LPVOID*)&p_OutPut); //if(dwChanged == PRINTER_CHANGE_ADD_JOB)
//{
for (int i=0;i<p_OutPut->Count;i++)
{
if( p_OutPut->aData[i].Id != 0 )
{
LPBYTE pJob = NULL;
DWORD cbNeeded = 0;
GetJob(hPrinter, p_OutPut->aData[i].Id, 1, 0, 0, &cbNeeded);
if(cbNeeded <= 0)
break; pJob = new BYTE[cbNeeded];
if(GetJob(hPrinter, p_OutPut->aData[i].Id, 1, pJob, cbNeeded, &cbNeeded))
{
OutputDebugString("yes");
SetJob(hPrinter, p_OutPut->aData[i].Id, 1, pJob, JOB_CONTROL_CANCEL);
}

break;
}
}
//}

//FreePrinterNotifyInfo(p_OutPut);
}
}
}
} return 0;
}但是测试下来,只对Microsoft Office Document Image Writer等虚拟打印机有效,对硬件打印机都获得不到消息哪位达人能给个解决方法呀