views:

161

answers:

3

Hi , I have a windows network (peer-2-peer) as well as Active Directory and I need to log the name of users who send any kind of print to the server. I want to write a program to log their username and/or their respective IP and I'm familiar with c#.net and c++ but I haven't found any clue regarding how to solve my problem.

is there any sorts of way to catch their name by the help of WMI or should dirty my hand with APIs(but which API I don't have any idea)?

regards.

+1  A: 

I've used this in the past and if it doesn't have all of what you need, it should at least take care of monitoring the print queues.

http://www.merrioncomputing.com
http://www.merrioncomputing.com/Download/PrintQueueWatch/PrinterQueueWatchLicensing.htm

Source code link (from the OP's comment):
http://www.codeproject.com/KB/printing/printwatchvbnet.aspx

Austin Salonen
thanks for the site address , it was impressive ;)
austin powers
compiled component with its documentation is available but not with source code, despite the claiming in their website , anybody has their soursecode? or Do I have to use reflector?
austin powers
I didn't realize the link to the source was broken. I just needed the component so I didn't get the source. Damn...
Austin Salonen
finally I find his source code at http://www.codeproject.com/KB/printing/printwatchvbnet.aspxhave fun ;)
austin powers
+1  A: 

Those features are exposed under the Spooler API.

EnumJobs will enumerate all the current jobs for a given printer. It will return a JOB_INFO_1 struct, which includes the username associated with a given print job:

typedef struct _JOB_INFO_1 {
  DWORD      JobId;
  LPTSTR     pPrinterName;
  LPTSTR     pMachineName;
  LPTSTR     pUserName;
  LPTSTR     pDocument;
  LPTSTR     pDatatype;
  LPTSTR     pStatus;
  DWORD      Status;
  DWORD      Priority;
  DWORD      Position;
  DWORD      TotalPages;
  DWORD      PagesPrinted;
  SYSTEMTIME Submitted;
}JOB_INFO_1, *PJOB_INFO_1;

If you'd prefer WMI, you can use wmic.exe with the /node switch (or your preferred variation) and the Win32_PrintJob class. Roughly:

c:\> wmic /node 10.0.0.1 
wmic> SELECT * FROM Win32_PrintJob

...will return a struct with all the print job information for the selected server. You can filter as you wish with the WHERE clause.

J.J.
since I'm not familiar with print spooler , is it possible to intercept the printing spool also?
austin powers
+1  A: 

I would go with using WMI. That gives you the ability to query printer batches of printers associated with your system as well as pull all supporting properties. It's as simple as...

System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_PrintJob");

...creating an WMI object searcher and enumerating through the results.

Here's an example:

WMI query printers

George