怎么监视打印机?我希望能用HOOK,不知可不可以?

nobounded 2003-09-25 10:00:34
怎么监视打印机?我希望能用HOOK,不知可不可以?我希望能HOOK打印发送程序和打印处理程序之间交流的消息,不知可不可以,谢谢。
...全文
659 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nobounded 2003-12-05
  • 打赏
  • 举报
回复
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是那儿来的?希望详解,我明白了就结帖.谢谢.

Skt32 2003-10-27
  • 打赏
  • 举报
回复
回复人: 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 &
(JOB_STATUS_ERROR |
JOB_STATUS_OFFLINE |
JOB_STATUS_PAPEROUT |
JOB_STATUS_BLOCKED_DEVQ))
{
return TRUE;
}
}
}

/*
* No error condition.
*/
return FALSE;

}

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

WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks
WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks使你可以监视发送到窗口过程的消息。系统在消息发送到接收窗口过程之前调用WH_CALLWNDPROC Hook子程,并且在窗口过程处理完消息之后调用WH_CALLWNDPROCRET Hook子程。

怎么用的
给我发一点?
thank you !
ccjerk 2003-10-17
  • 打赏
  • 举报
回复
打印队列中打印任务发生变化时(添加/删除等状态变化时)
发出消息wm_spoolrestatus
处理该消息
CWnd::OnSpoolerStatus See Also

The 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;
}
flyingjust 2003-10-16
  • 打赏
  • 举报
回复
WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks
WH_CALLWNDPROC和WH_CALLWNDPROCRET Hooks使你可以监视发送到窗口过程的消息。系统在消息发送到接收窗口过程之前调用WH_CALLWNDPROC Hook子程,并且在窗口过程处理完消息之后调用WH_CALLWNDPROCRET Hook子程。
如果某个应用程序调用打印 可以用HOOk钩住 但是从Print Pool到Printer不知真么监视
DDk不是很熟
ccjerk 2003-10-16
  • 打赏
  • 举报
回复
我是单独的做一个监听线程
然后
做监听处理
不过
用hook我想是可以的
不过
hook我没有用过!
xiaohedou 2003-10-16
  • 打赏
  • 举报
回复
我找到了一个例子,大家可以一起看看:

http://www.codeproject.com/vb/net/printwatchvbnet.asp?target=print%7Cjob

How to monitor a printer queue from VB.NET
By Merrion
nobounded 2003-10-16
  • 打赏
  • 举报
回复
那么我该怎么做?要监视WINSPOOL.DRV吗?
为什么GetProcAddress不能对WINSPOOL.DRV作用呢?




Ah 2003-10-14
  • 打赏
  • 举报
回复
用HOOK可能不行,毕竟HOOK只能处理UI方面的东西

关注

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧