/************
本文档功能是对打印机进行监控,获取本地所有打印机名称,并根据打印机名称对打印机进行监控,实时获取打印机的打印任务信息。
*************/
#include <iostream>
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <fstream>
#include <io.h>
#include <direct.h>
using namespace std;


#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "WS2_32.lib")


#define PRINTER_NUM5
static char printer[PRINTER_NUM][256];//暂时最大可监控本地上的5个打印机
#definePRINT_INFO_FILE"print_info.xml"


//遍历电脑中的打印机
void get_all_printer_device()
{
    DWORD            dwFlags = PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL;     
    LPPRINTER_INFO_2 pPrinters;     
    DWORD            cbPrinters;     
    DWORD            cReturned, i;  
    char             buf[256];  
      
    EnumPrinters (dwFlags, NULL, 2, NULL, 0, &cbPrinters,     
        &cReturned);     
      
    if (!(pPrinters = (LPPRINTER_INFO_2) LocalAlloc (LPTR, cbPrinters + 4)))     
    {     
        ::MessageBox (NULL, "error",     
            "error", MB_OK | MB_ICONEXCLAMATION);     
          
    }     
      
    if (!EnumPrinters (dwFlags, NULL, 2, (LPBYTE) pPrinters,     
        cbPrinters, &cbPrinters, &cReturned))     
    {     
        ::MessageBox (NULL, "error",     
            "error", MB_OK | MB_ICONEXCLAMATION);     
    }     
      
    if (cReturned > 0)     
    {  
          
        for (i = 0; i < cReturned; i++)     
        {     
            // for each printer in the PRINTER_INFO_2 array: build a string that     
            //   looks like "DEVICE_NAME;PORT;DRIVER_NAME"       
if(i <= PRINTER_NUM-1){
strcpy(printer[i],(pPrinters+i)->pPrinterName);
}
             //printf("printer name:%s\n",(pPrinters+i)->pPrinterName);
//printf("printer portname:%s\n",(pPrinters+i)->pPortName);
#if 0
            strcpy (buf, (pPrinters + i)->pPrinterName);     
            strcat (buf, ";");     
            strcat (buf, (pPrinters + i)->pPortName);     
            strcat (buf, ";");     
            strcat (buf, (pPrinters + i)->pDriverName);     
#endif
        }        
    }  
    else     
        ::MessageBox (NULL, "No printers listed", "PRINTER.EXE", MB_OK);
}
//获取系统默认打印机
void GetSystemDefaultPrinter()
{
char szBuff[1024] = {0};
DWORD len = 1024;
int ret;


ret = GetDefaultPrinter(szBuff,&len);
if(ret == FALSE){
printf("getdefault printer error:%d\n",GetLastError());
return ;
}
printf("default printer:%s\n",szBuff);
return;
}


//获取本机的IP地址(一个网卡)
static string GetLocalIpAddress()
{
WORD wVersionRequested = MAKEWORD(2,2);


WSADATA wsaData;
if(WSAStartup(wVersionRequested,&wsaData) != 0){
return "";
}


char local[255] = {0};
gethostname(local,sizeof(local));
hostent *ph = gethostbyname(local);
if(ph == NULL)
return "";
in_addr addr;
memcpy(&addr,ph->h_addr_list[0],sizeof(in_addr));


string localIP;
localIP.assign(inet_ntoa(addr));


WSACleanup();
return localIP;
}

/*****************
函数名称:monitor_printer_ex
描述:打印机监控线程,根据打印机名称对打印机进行监控,当打印机上有打印任务时,获取打印任务的文档名,打印主机名,打印者名称,打印份数,打印时间等信息
参数:
lpParameter打印机名称,通过遍历打印机获取


***************/
unsigned int __stdcall monitor_printer_ex(PVOID lpParameter)
{
HANDLE printerHandle;
HANDLE chgObject;\
DWORD nByteNeeded;
DWORD nReturned;
DWORD nByteUsed;
BOOL fcnreturn;
DWORD dwChange;
int i;
char *lpPrinterName;
string str;
time_t tm;


PRINTER_DEFAULTS Defaults = { NULL, NULL, PRINTER_ALL_ACCESS};
lpPrinterName = (char *)lpParameter;
//if(!OpenPrinter(lpPrinterName,&printerHandle,NULL)){
if(!OpenPrinter(lpPrinterName,&printerHandle,&Defaults)){
printf("open printer failed\n");
return 0;
}
chgObject = FindFirstPrinterChangeNotification(printerHandle,PRINTER_CHANGE_JOB,0,NULL);
while(1){
WaitForSingleObject(chgObject,INFINITE);
fcnreturn = FindNextPrinterChangeNotification(chgObject,&dwChange,NULL,NULL);
if(fcnreturn){
if(dwChange == PRINTER_CHANGE_ADD_JOB){
//printf(" %s printer add new job\n",lpPrinterName);
#if 1
//通过调用GetPrinter()函数得到作业数量
PRINTER_INFO_2 *pPrinterInfo = NULL;
GetPrinter(printerHandle,2,NULL,0,&nByteNeeded);
pPrinterInfo = (PRINTER_INFO_2 *)malloc(nByteNeeded);
GetPrinter(printerHandle,2,(LPBYTE)pPrinterInfo,nByteNeeded,&nByteUsed);

// 通过调用EnumJobs()函数枚举任务
JOB_INFO_2 *pJobInfo = NULL;
EnumJobs(printerHandle,0,pPrinterInfo->cJobs,2,NULL,0,(LPDWORD)&nByteNeeded,(LPDWORD)&nReturned);
pJobInfo = (JOB_INFO_2 *)malloc(nByteNeeded);
ZeroMemory(pJobInfo,nByteNeeded);
EnumJobs(printerHandle,0,pPrinterInfo->cJobs,2,(LPBYTE)pJobInfo,nByteNeeded,(LPDWORD)&nByteUsed,(LPDWORD)&nReturned);

if(pPrinterInfo->cJobs == 0){
//printf("not jobs\n");
free(pPrinterInfo);
free(pJobInfo);
//ClosePrinter(printerHandle);
//return;
continue;
}


if(!SetJob(printerHandle,pJobInfo[0].JobId,2,(LPBYTE)pJobInfo,JOB_CONTROL_DELETE)){
printf("delete job failed\n");
cout<<"error:"<<GetLastError()<<endl;
}else{
printf("delete job success\n");
}
printf("cJObs:%d\n",pPrinterInfo->cJobs);
i = pPrinterInfo->cJobs-1;
for(i = 0;i<pPrinterInfo->cJobs;i++){
//文档名称
printf("document:%s\n",pJobInfo[i].pDocument);
//printf("提交打印任务的主机名称:%s\n",pJobInfo[i].pMachineName);
//printf("提交打印任务的用户名:%s\n",pJobInfo[i].pUserName);
//printf("打印时间:%s\n",pJobInfo[i].StartTime);
//纸张类型
#if 1
if(pJobInfo[i].pDevMode->dmPaperSize == DMPAPER_A4){
printf("paper is A4\n");
}else if(pJobInfo[i].pDevMode->dmPaperSize == DMPAPER_B5){
printf("paper is B5\n");
}
//打印份数
printf("打印份数:%d\n",pJobInfo[i].pDevMode->dmCopies);
//打印颜色
if(pJobInfo[i].pDevMode->dmColor == DMCOLOR_COLOR){
printf("彩色\n");
}else if(pJobInfo[i].pDevMode->dmColor == DMCOLOR_MONOCHROME){
printf("黑白\n");
}
//打印时间
printf("打印时间:%d-%d-%d %d:%d:%d\n",pJobInfo[i].Submitted.wYear, pJobInfo[i].Submitted.wMonth, pJobInfo[i].Submitted.wDay,pJobInfo[i].Submitted.wHour+8, pJobInfo[i].Submitted.wMinute, pJobInfo[i].Submitted.wSecond);
#endif
}
free(pPrinterInfo);
free(pJobInfo);
//str = pJobInfo[i].pUserName;
#endif
}//if(dwChange == PRINTER_CHANGE_ADD_JOB)
}//if(fcnreturn) end
}//while(1) end
ClosePrinter(printerHandle);

return 0;
}


#if 1
int main()
{
int i;
HANDLE hThread[PRINTER_NUM];
char ip[100];


//strcpy(ip,const_cast<char *>(GetLocalIpAddress().c_str()));
//cout<<"localip:"<<ip<<endl;
get_all_printer_device();
GetSystemDefaultPrinter();
for( i = 0;i < PRINTER_NUM;i++){
if(printer[i][0] == '\0')
break;
printf("printer name:%s\n",printer[i]);
#if 0
hThread = CreateThread(NULL,0,monitor_printer,printer[i],0,NULL);
CloseHandle(hThread);
#endif
//为每个打印机创建一个监控线程来监控打印任务
hThread[i] = (HANDLE)_beginthreadex(NULL,0,monitor_printer_ex,printer[i],0,NULL);
}
WaitForMultipleObjects(i,hThread,TRUE,INFINITE);
//get_all_job();
Sleep(10000);
return 0;
}
#endif
02-02 22:18