您的位置:程序门 -> vb ->



怎样获取网络打印机的打印记录


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎样获取网络打印机的打印记录[已结贴,结贴人:swife]
发表于:2007-06-19 14:12:15 楼主
我想获取以下记录
打印时间/打印机器/页数/....
发表于:2007-06-19 17:05:521楼 得分:20
如果想得到各打印作业的状态,通常我们可以通过调用以下api:

   openprinter

   getprinter

   enumjobs

   closeprinter  

 
  目前在微软的网站上没有现成的关于以上api调用的vb代码,但有一篇vc的示例代码,您可以参考一下:

   howto:   get   the   status   of   a   printer   and   a   print   job
   (http://support.microsoft.com/support/kb/articles/q160/1/29.asp)  

  另外,您还可以试一下adsi中的iadsprintqueueoperations和iadsprintjoboperations接口。具体信息您可以参考以下文档:

   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/iadsprintjoboperations.asp?frame=true  

  它需要win2000及以后版本或装了active   directory   client   extension的winnt   4.0sp6a/win95/98/me来运行。关于active   directory   client   extension的安装信息,您可以参考以下文档:

   http://www.microsoft.com/windows2000/server/evaluation/news/bulletins/adextension.asp

http://support.microsoft.com/kb/160129/zh-cn
如何获取打印机和打印作业的状态
发表于:2007-06-28 16:01:492楼 得分:0
up
发表于:2007-06-29 11:42:513楼 得分:0
没人知道么
发表于:2007-06-29 13:00:114楼 得分:0
attribute   vb_name   =   "module1 "

'       dim   printerstatus   as   string
'       dim   jobstatus   as   string
'       dim   errorinfo   as   string
'
'       'clear   the   status   info   for   new   info/status.
'       text1.text   =   " "
'       text2.text   =   " "
'       text3.text   =   " "
'
'       'call   sub   to   perform   check.
'       text1.text   =   checkprinter(printerstatus,   jobstatus)
'       text2.text   =   printerstatus
'       text3.text   =   jobstatus
option   explicit

public   declare   function   lstrcpy   lib   "kernel32 "   _
      alias   "lstrcpya "   _
      (byval   lpstring1   as   string,   _
      byval   lpstring2   as   string)   _
      as   long

public   declare   function   openprinter   lib   "winspool.drv "   _
      alias   "openprintera "   _
      (byval   pprintername   as   string,   _
      phprinter   as   long,   _
      pdefault   as   printer_defaults)   _
      as   long

public   declare   function   getprinter   lib   "winspool.drv "   alias   "getprintera "   _
      (byval   hprinter   as   long,   _
      byval   level   as   long,   _
      pprinter   as   byte,   _
      byval   cbbuf   as   long,   _
      pcbneeded   as   long)   _
      as   long

public   declare   function   closeprinter   lib   "winspool.drv "   _
      (byval   hprinter   as   long)   _
      as   long

public   declare   sub   copymemory   lib   "kernel32 "   alias   "rtlmovememory "   _
      (destination   as   any,   _
      source   as   any,   _
      byval   length   as   long)

public   declare   function   enumjobs   lib   "winspool.drv "   alias   "enumjobsa "   _
      (byval   hprinter   as   long,   _
      byval   firstjob   as   long,   _
      byval   nojobs   as   long,   _
      byval   level   as   long,   _
      pjob   as   byte,   _
      byval   cdbuf   as   long,   _
      pcbneeded   as   long,   _
      pcreturned   as   long)   _
      as   long
     
'   constants   for   printer_defaults   structure
public   const   printer_access_use   =   &h8
public   const   printer_access_administer   =   &h4

'   constants   for   devmode   structure
public   const   cchdevicename   =   32
public   const   cchformname   =   32

public   type   printer_defaults
      pdatatype   as   string
      pdevmode   as   long
      desiredaccess   as   long
end   type

public   type   devmode
      dmdevicename   as   string   *   cchdevicename
      dmspecversion   as   integer
      dmdriverversion   as   integer
      dmsize   as   integer
      dmdriverextra   as   integer
      dmfields   as   long
      dmorientation   as   integer
      dmpapersize   as   integer
      dmpaperlength   as   integer
      dmpaperwidth   as   integer
      dmscale   as   integer
      dmcopies   as   integer
      dmdefaultsource   as   integer
      dmprintquality   as   integer
      dmcolor   as   integer
      dmduplex   as   integer
      dmyresolution   as   integer
      dmttoption   as   integer
      dmcollate   as   integer
      dmformname   as   string   *   cchformname
      dmlogpixels   as   integer
      dmbitsperpel   as   long
      dmpelswidth   as   long
      dmpelsheight   as   long
      dmdisplayflags   as   long
      dmdisplayfrequency   as   long
end   type

type   systemtime
      wyear   as   integer
      wmonth   as   integer
      wdayofweek   as   integer
      wday   as   integer
      whour   as   integer
      wminute   as   integer
      wsecond   as   integer
      wmilliseconds   as   integer
end   type

type   job_info_2
      jobid   as   long
      pprintername   as   long
      pmachinename   as   long
      pusername   as   long
      pdocument   as   long
      pnotifyname   as   long
      pdatatype   as   long
      pprintprocessor   as   long
      pparameters   as   long
      pdrivername   as   long
      pdevmode   as   long
      pstatus   as   long
      psecuritydescriptor   as   long
      status   as   long
      priority   as   long
      position   as   long
      starttime   as   long
      untiltime   as   long
      totalpages   as   long
      size   as   long
      submitted   as   systemtime
      time   as   long
      pagesprinted   as   long
end   type

type   printer_info_2
      pservername   as   long
      pprintername   as   long
      psharename   as   long
      pportname   as   long
      pdrivername   as   long
      pcomment   as   long
      plocation   as   long
      pdevmode   as   long
      psepfile   as   long
      pprintprocessor   as   long
      pdatatype   as   long
      pparameters   as   long
      psecuritydescriptor   as   long
      attributes   as   long
      priority   as   long
      defaultpriority   as   long
      starttime   as   long
      untiltime   as   long
      status   as   long
      cjobs   as   long
      averageppm   as   long
end   type

public   const   error_insufficient_buffer   =   122
public   const   printer_status_busy   =   &h200
public   const   printer_status_door_open   =   &h400000
public   const   printer_status_error   =   &h2
public   const   printer_status_initializing   =   &h8000
public   const   printer_status_io_active   =   &h100
public   const   printer_status_manual_feed   =   &h20
public   const   printer_status_no_toner   =   &h40000
public   const   printer_status_not_available   =   &h1000
public   const   printer_status_offline   =   &h80
public   const   printer_status_out_of_memory   =   &h200000
public   const   printer_status_output_bin_full   =   &h800
public   const   printer_status_page_punt   =   &h80000
public   const   printer_status_paper_jam   =   &h8
public   const   printer_status_paper_out   =   &h10
public   const   printer_status_paper_problem   =   &h40
public   const   printer_status_paused   =   &h1
public   const   printer_status_pending_deletion   =   &h4
public   const   printer_status_printing   =   &h400
public   const   printer_status_processing   =   &h4000
public   const   printer_status_toner_low   =   &h20000
public   const   printer_status_user_intervention   =   &h100000
public   const   printer_status_waiting   =   &h2000
public   const   printer_status_warming_up   =   &h10000
public   const   job_status_paused   =   &h1
public   const   job_status_error   =   &h2
public   const   job_status_deleting   =   &h4
public   const   job_status_spooling   =   &h8
public   const   job_status_printing   =   &h10
public   const   job_status_offline   =   &h20
public   const   job_status_paperout   =   &h40
public   const   job_status_printed   =   &h80
public   const   job_status_deleted   =   &h100
public   const   job_status_blocked_devq   =   &h200
public   const   job_status_user_intervention   =   &h400
public   const   job_status_restart   =   &h800

public   function   getstring(byval   ptrstr   as   long)   as   string
      dim   strbuff   as   string   *   256
     
      'check   for   zero   address
      if   ptrstr   =   0   then
            getstring   =   "   "
            exit   function
      end   if
     
      'copy   data   from   ptrstr   to   buffer.
      copymemory   byval   strbuff,   byval   ptrstr,   256
     
      'strip   any   trailing   nulls   from   string.
      getstring   =   stripnulls(strbuff)
end   function

public   function   stripnulls(originalstr   as   string)   as   string
      'strip   any   trailing   nulls   from   input   string.
      if   (instr(originalstr,   chr(0))   >   0)   then
            originalstr   =   left(originalstr,   instr(originalstr,   chr(0))   -   1)
      end   if

      'return   modified   string.
      stripnulls   =   originalstr
end   function

public   function   ptrctovbstring(add   as   long)   as   string
        dim   stemp   as   string   *   512
        dim   x   as   long

        x   =   lstrcpy(stemp,   add)
        if   (instr(1,   stemp,   chr(0))   =   0)   then
                  ptrctovbstring   =   " "
        else
                  ptrctovbstring   =   left(stemp,   instr(1,   stemp,   chr(0))   -   1)
        end   if
end   function

发表于:2007-06-29 13:01:045楼 得分:0
public   function   checkprinterstatus(pi2status   as   long)   as   string
      dim   tempstr   as   string
     
      if   pi2status   =   0   then       '   return   "ready "
            checkprinterstatus   =   "printer   status   =   ready "   &   vbcrlf
      else
            tempstr   =   " "       '   clear
            if   (pi2status   and   printer_status_busy)   then
                  tempstr   =   tempstr   &   "busy     "
            end   if
           
            if   (pi2status   and   printer_status_door_open)   then
                  tempstr   =   tempstr   &   "printer   door   open     "
            end   if
           
            if   (pi2status   and   printer_status_error)   then
                  tempstr   =   tempstr   &   "printer   error     "
            end   if
           
            if   (pi2status   and   printer_status_initializing)   then
                  tempstr   =   tempstr   &   "initializing     "
            end   if
           
            if   (pi2status   and   printer_status_io_active)   then
                  tempstr   =   tempstr   &   "i/o   active     "
            end   if

            if   (pi2status   and   printer_status_manual_feed)   then
                  tempstr   =   tempstr   &   "manual   feed     "
            end   if
           
            if   (pi2status   and   printer_status_no_toner)   then
                  tempstr   =   tempstr   &   "no   toner     "
            end   if
           
            if   (pi2status   and   printer_status_not_available)   then
                  tempstr   =   tempstr   &   "not   available     "
            end   if
           
            if   (pi2status   and   printer_status_offline)   then
                  tempstr   =   tempstr   &   "off   line     "
            end   if
           
            if   (pi2status   and   printer_status_out_of_memory)   then
                  tempstr   =   tempstr   &   "out   of   memory     "
            end   if
           
            if   (pi2status   and   printer_status_output_bin_full)   then
                  tempstr   =   tempstr   &   "output   bin   full     "
            end   if
           
            if   (pi2status   and   printer_status_page_punt)   then
                  tempstr   =   tempstr   &   "page   punt     "
            end   if
           
            if   (pi2status   and   printer_status_paper_jam)   then
                  tempstr   =   tempstr   &   "paper   jam     "
            end   if

            if   (pi2status   and   printer_status_paper_out)   then
                  tempstr   =   tempstr   &   "paper   out     "
            end   if
           
            if   (pi2status   and   printer_status_output_bin_full)   then
                  tempstr   =   tempstr   &   "output   bin   full     "
            end   if
           
            if   (pi2status   and   printer_status_paper_problem)   then
                  tempstr   =   tempstr   &   "page   problem     "
            end   if
           
            if   (pi2status   and   printer_status_paused)   then
                  tempstr   =   tempstr   &   "paused     "
            end   if

            if   (pi2status   and   printer_status_pending_deletion)   then
                  tempstr   =   tempstr   &   "pending   deletion     "
            end   if
           
            if   (pi2status   and   printer_status_printing)   then
                  tempstr   =   tempstr   &   "printing     "
            end   if
           
            if   (pi2status   and   printer_status_processing)   then
                  tempstr   =   tempstr   &   "processing     "
            end   if
           
            if   (pi2status   and   printer_status_toner_low)   then
                  tempstr   =   tempstr   &   "toner   low     "
            end   if

            if   (pi2status   and   printer_status_user_intervention)   then
                  tempstr   =   tempstr   &   "user   intervention     "
            end   if
           
            if   (pi2status   and   printer_status_waiting)   then
                  tempstr   =   tempstr   &   "waiting     "
            end   if
           
            if   (pi2status   and   printer_status_warming_up)   then
                  tempstr   =   tempstr   &   "warming   up     "
            end   if
           
            'did   you   find   a   known   status?
            if   len(tempstr)   =   0   then
                  tempstr   =   "unknown   status   of   "   &   pi2status
            end   if
           
            'return   the   status
            checkprinterstatus   =   "printer   status   =   "   &   tempstr   &   vbcrlf
      end   if
end   function
                               
发表于:2007-06-29 13:03:206楼 得分:0
public   function   checkprinter(printername   as   string,   printerstr   as   string,   jobstr   as   string)   as   string
      'dim   printername   as   string
      dim   hprinter   as   long
      dim   bytebuf   as   long
      dim   bytesneeded   as   long
      dim   pi2   as   printer_info_2
      dim   ji2   as   job_info_2
      dim   printerinfo()   as   byte
      dim   jobinfo()   as   byte
      dim   result   as   long
      dim   lasterror   as   long
      dim   tempstr   as   string
      dim   numji2   as   long
      dim   pdefaults   as   printer_defaults
      dim   i   as   integer
     
      'set   a   default   return   value   if   no   errors   occur.
      checkprinter   =   "printer   info   retrieved "
     
      ' '==================================
    '此处做一简单的修改,让本函数遍历所有的打印机
    '   printername   =   printer.devicename
      '=============================
     
      'set   desired   access   security   setting.
      pdefaults.desiredaccess   =   printer_access_use
     
      'call   api   to   get   a   handle   to   the   printer.
      result   =   openprinter(printername,   hprinter,   pdefaults)
      if   result   =   0   then
            'if   an   error   occurred,   display   an   error   and   exit   sub.
            checkprinter   =   "cannot   open   printer   "   &   printername   &   _
                  ",   error:   "   &   err.lastdllerror
            exit   function
      end   if

      'init   bytesneeded
      bytesneeded   =   0

      'clear   the   error   object   of   any   errors.
      err.clear

      'determine   the   buffer   size   that   is   needed   to   get   printer   info.
      result   =   getprinter(hprinter,   2,   0&,   0&,   bytesneeded)
     
      'check   for   error   calling   getprinter.
      if   err.lastdllerror   <>   error_insufficient_buffer   then
            'display   an   error   message,   close   printer,   and   exit   sub.
            checkprinter   =   "   >   getprinter   failed   on   initial   call!   < "
            closeprinter   hprinter
            exit   function
      end   if

      redim   printerinfo(1   to   bytesneeded)
     
      bytebuf   =   bytesneeded
     
      'call   getprinter   to   get   the   status.
      result   =   getprinter(hprinter,   2,   printerinfo(1),   bytebuf,   _
          bytesneeded)
     
      'check   for   errors.
      if   result   =   0   then
            'determine   the   error   that   occurred.
            lasterror   =   err.lastdllerror()
           
            checkprinter   =   "couldn 't   get   printer   status!     error   =   "   _
                  &   lasterror
            closeprinter   hprinter
            exit   function
      end   if
      copymemory   pi2,   printerinfo(1),   len(pi2)
        printerstr   =   checkprinterstatus(pi2.status)

      printerstr   =   printerstr   &   "printer   name   =   "   &   _
          getstring(pi2.pprintername)   &   vbcrlf
      printerstr   =   printerstr   &   "printer   driver   name   =   "   &   _
          getstring(pi2.pdrivername)   &   vbcrlf
      printerstr   =   printerstr   &   "printer   port   name   =   "   &   _
          getstring(pi2.pportname)   &   vbcrlf
      debug.print   printerstr
      result   =   enumjobs(hprinter,   0&,   &hffffffff,   2,   byval   0&,   0&,   _
            bytesneeded,   numji2)
          if   bytesneeded   =   0   then
            jobstr   =   "no   print   jobs! "
      else
            redim   jobinfo(0   to   bytesneeded)
            result   =   enumjobs(hprinter,   0&,   &hffffffff,   2,   jobinfo(0),   _
                bytesneeded,   bytebuf,   numji2)
            if   result   =   0   then
                  'get   and   display   error,   close   printer,   and   exit   sub.
                  lasterror   =   err.lastdllerror
                  checkprinter   =   "   >   enumjobs   failed   on   second   call!   <     error   =   "   _
                        &   lasterror
                  closeprinter   hprinter
                  exit   function
            end   if
            for   i   =   0   to   numji2   -   1       '   loop   through   jobs   and   walk   the   buffer
                    copymemory   ji2,   jobinfo(i   *   len(ji2)),   len(ji2)
                         
                    '   list   info   available   on   jobs.
                    debug.print   "job   id "   &   vbtab   &   ji2.jobid
                    debug.print   "name   of   printer "   &   vbtab   &   _
                        getstring(ji2.pprintername)
                    debug.print   "name   of   machine   that   created   job "   &   vbtab   &   _
                        getstring(ji2.pmachinename)
                    debug.print   "print   job   owner 's   name "   &   vbtab   &   _
                        getstring(ji2.pusername)
                    debug.print   "name   of   document "   &   vbtab   &   getstring(ji2.pdocument)
                    debug.print   "name   of   user   to   notify "   &   vbtab   &   _
                        getstring(ji2.pnotifyname)
                    debug.print   "type   of   data "   &   vbtab   &   getstring(ji2.pdatatype)
                    debug.print   "print   processor "   &   vbtab   &   _
                        getstring(ji2.pprintprocessor)
                    debug.print   "print   processor   parameters "   &   vbtab   &   _
                        getstring(ji2.pparameters)
                    debug.print   "print   driver   name "   &   vbtab   &   _
                        getstring(ji2.pdrivername)
                    debug.print   "print   job   'p '   status "   &   vbtab   &   _
                        getstring(ji2.pstatus)
                    debug.print   "print   job   status "   &   vbtab   &   ji2.status
                    debug.print   "print   job   priority "   &   vbtab   &   ji2.priority
                    debug.print   "position   in   queue "   &   vbtab   &   ji2.position
                    debug.print   "earliest   time   job   can   be   printed "   &   vbtab   &   _
                        ji2.starttime
                    debug.print   "latest   time   job   will   be   printed "   &   vbtab   &   _
                        ji2.untiltime
                    debug.print   "total   pages   for   entire   job "   &   vbtab   &   ji2.totalpages
                    debug.print   "size   of   job   in   bytes "   &   vbtab   &   ji2.size
   
                    debug.print   "elapsed   print   time "   &   vbtab   &   ji2.time
                    debug.print   "pages   printed   so   far "   &   vbtab   &   ji2.pagesprinted
                         
                    'display   basic   job   status   info.
                    jobstr   =   jobstr   &   "job   id   =   "   &   ji2.jobid   &   _
                          vbcrlf   &   "total   pages   =   "   &   ji2.totalpages   &   vbcrlf
                   
                    tempstr   =   " "       'clear
                    if   ji2.pstatus   =   0&   then       '   if   pstatus   is   null,   check   status.
                        if   ji2.status   =   0   then
                              tempstr   =   tempstr   &   "ready!     "   &   vbcrlf
                        else     'check   for   the   various   print   job   states.
                              if   (ji2.status   and   job_status_spooling)   then
                                    tempstr   =   tempstr   &   "spooling     "
                              end   if
                             
                              if   (ji2.status   and   job_status_offline)   then
                                    tempstr   =   tempstr   &   "off   line     "
                              end   if
                             
                              if   (ji2.status   and   job_status_paused)   then
                                    tempstr   =   tempstr   &   "paused     "
                              end   if
                             
                              if   (ji2.status   and   job_status_error)   then
                                    tempstr   =   tempstr   &   "error     "
                              end   if
                             
                              if   (ji2.status   and   job_status_paperout)   then
                                    tempstr   =   tempstr   &   "paper   out     "
                              end   if
                             
                              if   (ji2.status   and   job_status_printing)   then
                                    tempstr   =   tempstr   &   "printing     "
                              end   if
                             
                              if   (ji2.status   and   job_status_user_intervention)   then
                                    tempstr   =   tempstr   &   "user   intervention   needed     "
                              end   if
                             
                              if   len(tempstr)   =   0   then
                                    tempstr   =   "unknown   status   of   "   &   ji2.status
                              end   if
                        end   if
                else
                            tempstr   =   ptrctovbstring(ji2.pstatus)
                end   if
 
                    jobstr   =   jobstr   &   tempstr   &   vbcrlf
                    debug.print   jobstr   &   tempstr
            next   i
      end   if

      closeprinter   hprinter
end   function

本代码取自网上,略有改动


快速检索

最新资讯
热门点击