怎么监视打印机?我希望能用HOOK,不知可不可以?我希望能HOOK打印发送程序和打印处理程序之间交流的消息,不知可不可以,谢谢。

解决方案 »

  1.   

    用HOOK可能不行,毕竟HOOK只能处理UI方面的东西关注
      

  2.   

    那么我该怎么做?要监视WINSPOOL.DRV吗?
    为什么GetProcAddress不能对WINSPOOL.DRV作用呢?
      

  3.   

    我找到了一个例子,大家可以一起看看:http://www.codeproject.com/vb/net/printwatchvbnet.asp?target=print%7CjobHow to monitor a printer queue from VB.NET
    By Merrion
      

  4.   

    我是单独的做一个监听线程
    然后
    做监听处理
    不过
    用hook我想是可以的
    不过
    hook我没有用过!
      

  5.   

    WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks
    WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks使你可以监视发送到窗口过程的消息。系统在消息发送到接收窗口过程之前调用WH_CALLWNDPROC Hook子程,并且在窗口过程处理完消息之后调用WH_CALLWNDPROCRET Hook子程。
    如果某个应用程序调用打印 可以用HOOk钩住 但是从Print Pool到Printer不知真么监视
    DDk不是很熟
      

  6.   

    打印队列中打印任务发生变化时(添加/删除等状态变化时)
    发出消息wm_spoolrestatus
    处理该消息
    CWnd::OnSpoolerStatus See AlsoThe framework calls this member function from Print Manager whenever a job is added to or removed from the Print Manager queue.afx_msg void OnSpoolerStatus(
       UINT nStatus,
       UINT nJobs 
    );
    不过
    我没有这么写
    用很笨的办法
    线程处理:
    bool CPrintList::OperateJob(int index)
    {
    HANDLE handle;
    JOB_INFO_2          *pJobStorage = NULL;
    PRINTER_INFO_2       *pPrinterInfo = NULL;
    DWORD  cByteNeeded,cByteUsed,nReturned; char buf[256];
    DWORD dw = 256;
    BOOL isadd = false; if (!m_bselected) return false;    isadd =GetDefaultPrinter(buf,&dw);
        if (!isadd) 
    return false ;
        if(!OpenPrinter(buf,&handle,NULL))
        {
              MessageBox(_T("Could not open printer"),_T("Error"),  MB_ICONINFORMATION);
              return false;
        }
    if (!GetPrinter(handle, 2, NULL, 0, &cByteNeeded))
    {
    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                return false;
    }
    pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
           if (!(pPrinterInfo))
               return false;   /* Failure to allocate memory. */    if (!GetPrinter(handle, 2,(LPBYTE)pPrinterInfo, cByteNeeded, &cByteUsed))
           {
               /* Failure to access the printer. */ 
               free(pPrinterInfo);
               pPrinterInfo = NULL;
               return false;
           } if (!EnumJobs(handle, 0, pPrinterInfo->cJobs, 2,NULL,0,(LPDWORD)&cByteNeeded,(LPDWORD)&nReturned))
           {
               if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
               {
                   free(pPrinterInfo);
                   pPrinterInfo = NULL;
                   return false;
               }
           }
    pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
           if (!pJobStorage)
           {
               /* Failure to allocate Job storage space. */ 
               free(pPrinterInfo);
               pPrinterInfo = NULL;
               return false;
           }
       ZeroMemory(pJobStorage, cByteNeeded);  if (!EnumJobs(handle,0,pPrinterInfo->cJobs, 2,(LPBYTE)pJobStorage, cByteNeeded,(LPDWORD)&cByteUsed,(LPDWORD)&nReturned))
           {
               free(pPrinterInfo);
               free(pJobStorage);
               pJobStorage = NULL;
               pPrinterInfo = NULL;
               return false;
           }    if (!nReturned)   //打印队列中没有数据
       {
       free(pPrinterInfo);
               free(pJobStorage);
               pJobStorage = NULL;
               pPrinterInfo = NULL;
       return true; 
       } for (int i = 0; i < (int)nReturned; i++)

    if (m_selectjob.JobID == pJobStorage[i].JobId ) 
    {
    if (index == 0)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_PAUSE);
    else if (index == 1)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESTART);
    else if (index == 2)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_DELETE);
    else if (index == 3)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESUME);
    break;
    }
    }
            free(pPrinterInfo);
            free(pJobStorage);
    pPrinterInfo = NULL;
    pJobStorage = NULL;
    m_bselected  = false;
       return true;
    }
      

  7.   

    对了
    关于
    hookWH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks
    WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks使你可以监视发送到窗口过程的消息。系统在消息发送到接收窗口过程之前调用WH_CALLWNDPROC Hook子程,并且在窗口过程处理完消息之后调用WH_CALLWNDPROCRET Hook子程。怎么用的
    给我发一点?
    thank you !
      

  8.   

    当然本机的打印消息我可以通过HOOKAPI StartDoc来搞定,但是非本机的打印请求我只能通过监视打印队列来知道了,有现存的软件(大都用DELPHI写的),但是那位兄台推说太忙不愿透露细节.我现在为难的是怎么可以让系统通知我的程序有打印请求了(详细到页),有打印请求可以通过CHANGE NOTIFICATION 来解决,但是详细到页我不知到如何做,希望大家提供一些别的解决方法.总不能让我HOOK WritePrinter吧.
      

  9.   

    回复人: isdong(有些事情应该忘记) (☆★☆★☆★☆★) ( ) 信誉:100  2003-1-24 9:29:31  得分:0 
     
     
      
    Sample Code   BOOL GetJobs(HANDLE hPrinter,        /* Handle to the printer. */                 JOB_INFO_2 **ppJobInfo, /* Pointer to be filled.  */ 
                    int *pcJobs,            /* Count of jobs filled.  */ 
                    DWORD *pStatus)         /* Print Queue status.    */    {   DWORD               cByteNeeded,
                            nReturned,
                            cByteUsed;
        JOB_INFO_2          *pJobStorage = NULL;
        PRINTER_INFO_2       *pPrinterInfo = NULL;   /* Get the buffer size needed. */ 
           if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
           {
               if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                   return FALSE;
           }       pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
           if (!(pPrinterInfo))
               /* Failure to allocate memory. */ 
               return FALSE;       /* Get the printer information. */ 
           if (!GetPrinter(hPrinter,
                   2,
                   (LPSTR)pPrinterInfo,
                   cByteNeeded,
                   &cByteUsed))
           {
               /* Failure to access the printer. */ 
               free(pPrinterInfo);
               pPrinterInfo = NULL;
               return FALSE;
           }       /* Get job storage space. */ 
           if (!EnumJobs(hPrinter,
                   0,
                   pPrinterInfo->cJobs,
                   2,
                   NULL,
                   0,
                   (LPDWORD)&cByteNeeded,
                   (LPDWORD)&nReturned))
           {
               if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
               {
                   free(pPrinterInfo);
                   pPrinterInfo = NULL;
                   return FALSE;
               }
           }       pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
           if (!pJobStorage)
           {
               /* Failure to allocate Job storage space. */ 
               free(pPrinterInfo);
               pPrinterInfo = NULL;
               return FALSE;
           }       ZeroMemory(pJobStorage, cByteNeeded);       /* Get the list of jobs. */ 
           if (!EnumJobs(hPrinter,
                   0,
                   pPrinterInfo->cJobs,
                   2,
                   (LPBYTE)pJobStorage,
                   cByteNeeded,
                   (LPDWORD)&cByteUsed,
                   (LPDWORD)&nReturned))
           {
               free(pPrinterInfo);
               free(pJobStorage);
               pJobStorage = NULL;
               pPrinterInfo = NULL;
               return FALSE;
           }       /*
            *  Return the information.
            */ 
           *pcJobs = nReturned;
           *pStatus = pPrinterInfo->Status;
           *ppJobInfo = pJobStorage;
           free(pPrinterInfo);       return TRUE;   }   BOOL IsPrinterError(HANDLE hPrinter)
       {       JOB_INFO_2  *pJobs;
           int         cJobs,
                       i;
           DWORD       dwPrinterStatus;       /*
            *  Get the state information for the Printer Queue and
            *  the jobs in the Printer Queue.
            */ 
           if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
               return FALSE;       /*
            *  If the Printer reports an error, believe it.
            */ 
           if (dwPrinterStatus &
               (PRINTER_STATUS_ERROR |
               PRINTER_STATUS_PAPER_JAM |
               PRINTER_STATUS_PAPER_OUT |
               PRINTER_STATUS_PAPER_PROBLEM |
               PRINTER_STATUS_OUTPUT_BIN_FULL |
               PRINTER_STATUS_NOT_AVAILABLE |
               PRINTER_STATUS_NO_TONER |
               PRINTER_STATUS_OUT_OF_MEMORY |
               PRINTER_STATUS_OFFLINE |
               PRINTER_STATUS_DOOR_OPEN))
           {
               return TRUE;
           }       /*
            *  Find the Job in the Queue that is printing.
            */ 
           for (i=0; i < cJobs; i++)
           {
               if (pJobs[i].Status & JOB_STATUS_PRINTING)
               {
                   /*
                    *  If the job is in an error state,
                    *  report an error for the printer.
                    *  Code could be inserted here to
                    *  attempt an interpretation of the
                    *  pStatus member as well.
                    */ 
                   if (pJobs[i].Status &amp;
                       (JOB_STATUS_ERROR |
                       JOB_STATUS_OFFLINE |
                       JOB_STATUS_PAPEROUT |
                       JOB_STATUS_BLOCKED_DEVQ))
                   {
                       return TRUE;
                   }
               }
           }       /*
            *  No error condition.
            */ 
           return FALSE;   } 
      

  10.   

    ccjerk(岚)大哥:
    这段程序中:
    if (m_selectjob.JobID == pJobStorage[i].JobId ) 
    {
    if (index == 0)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_PAUSE);
    else if (index == 1)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESTART);
    else if (index == 2)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_DELETE);
    else if (index == 3)
    SetJob(handle,pJobStorage[i].JobId,0,NULL,JOB_CONTROL_RESUME);
    break;
    index是干什么的?m_selectjob是那儿来的?希望详解,我明白了就结帖.谢谢.