The following code will allow you to talk to a printer (running on a specific IP Address) to query its current status.
OLEPRNLib is a COM object that appears to be installed on XP and greater machines.
On my machine I made a reference on the COM tab of the Add Reference dialog to “oleprn 1.0 Type Library“ which lived in “c:\Windows\System32\oleprn.dll“
using System;
using OLEPRNLib;
namespace PrinterStatus
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
string[] ErrorMessageText = new string[8];
ErrorMessageText[0] = "service requested";
ErrorMessageText[1] = "offline";
ErrorMessageText[2] = "paper jammed";
ErrorMessageText[3] = "door open";
ErrorMessageText[4] = "no toner";
ErrorMessageText[5] = "toner low";
ErrorMessageText[6] = "out of paper";
ErrorMessageText[7] = "low paper";
int DeviceID = 1;
int Retries = 1;
int TimeoutInMS = 2000;
string CommunityString = "public";
string IPAddressOfPrinter = "10.3.0.93";
// Create instance of COM object
OLEPRNLib.SNMP snmp = new OLEPRNLib.SNMP();
// Open the SNMP connect to the printer
snmp.Open(IPAddressOfPrinter, CommunityString, Retries, TimeoutInMS);
// The actual Warning/Error bits
uint WarningErrorBits = snmp.GetAsByte(String.Format("25.3.5.1.2.{0}", DeviceID));
// The actual Status
uint StatusResult = snmp.GetAsByte(String.Format("25.3.2.1.5.{0}", DeviceID));
// uint Result2 = snmp.GetAsByte(String.Format("25.3.5.1.1.{0}", DeviceID));
string Result1Str = "";
switch (StatusResult)
{
case 2 : Result1Str = "OK";
break;
case 3 : Result1Str = "Warning: ";
break;
case 4 : Result1Str = "Being Tested: ";
break;
case 5 : Result1Str = "Unavailable for any use: ";
break;
default : Result1Str = "Unknown Status Code : "+StatusResult;
break;
}
string Str = "";
if ((StatusResult == 3 || StatusResult == 5))
{
int Mask = 1;
int NumMsg = 0;
for (int i=0; i< 8; i++)
{
if ((WarningErrorBits & Mask) == Mask)
{
if (Str.Length > 0)
Str += ", ";
Str += ErrorMessageText[i];
NumMsg = NumMsg + 1;
}
Mask = Mask * 2;
}
}
Console.WriteLine(Result1Str + Str);
}
}
}
posted on 2009-06-18 17:24
Vincent-chen 阅读(6445)
评论(2) 编辑 收藏