如何判断打印机是否联机(100分)

  • 主题发起人 主题发起人 smallfox
  • 开始时间 开始时间
S

smallfox

Unregistered / Unconfirmed
GUEST, unregistred user!
如何判断打印机是否联机
要能根据打印机名称来判断
 
可以用DeviceCapabilities() ,OpenPrinter()和GetPrinter()三个函数,
其它两个函数就不详解了,可以查阅相关的Win32API函数
注意在GetPrinter()函数中,有一个参数pPrinter 为PRINTER_INFO_*结构,
如果要检测打印是否联机,可以用 PRINTER_INFO_2 结构,此函数原形如下:

BOOL GetPrinter(
HANDLE hPrinter, // handle to printer of interest
DWORD Level, // version of printer info data structure
LPBYTE pPrinter, // pointer to array of bytes that receives printer info. structure
DWORD cbBuf, // size, in bytes, of array of bytes
LPDWORD pcbNeeded // pointer to variable with count of bytes retrieved (or required)
);


PRINTER_INFO_2 结构定义如下:
typedef struct _PRINTER_INFO_2 { // pri2
LPTSTR pServerName;

LPTSTR pPrinterName;

LPTSTR pShareName;

LPTSTR pPortName;

LPTSTR pDriverName;

LPTSTR pComment;

LPTSTR pLocation;

LPDEVMODE pDevMode;

LPTSTR pSepFile;

LPTSTR pPrintProcessor;

LPTSTR pDatatype;

LPTSTR pParameters;

PSECURITY_DESCRIPTOR pSecurityDescriptor;

DWORD Attributes;

DWORD Priority;

DWORD DefaultPriority;

DWORD StartTime;

DWORD UntilTime;

DWORD Status;

DWORD cJobs;

DWORD AveragePPM;

} PRINTER_INFO_2;


其中就有打印状态定义:Status,可以检验此值来检验当前打印机的连机状态,
具体的值如下:

Windows NT:

PRINTER_STATUS_PAUSED
PRINTER_STATUS_PENDING_DELETION

Windows 95:

PRINTER_STATUS_BUSY
PRINTER_STATUS_DOOR_OPEN
PRINTER_STATUS_ERROR
PRINTER_STATUS_INITIALIZING
PRINTER_STATUS_IO_ACTIVE
PRINTER_STATUS_MANUAL_FEED
PRINTER_STATUS_NO_TONER
PRINTER_STATUS_NOT_AVAILABLE
PRINTER_STATUS_OFFLINE
PRINTER_STATUS_OUT_OF_MEMORY
PRINTER_STATUS_OUTPUT_BIN_FULL
PRINTER_STATUS_PAGE_PUNT
PRINTER_STATUS_PAPER_JAM
PRINTER_STATUS_PAPER_OUT
PRINTER_STATUS_PAPER_PROBLEM
PRINTER_STATUS_PAUSED
PRINTER_STATUS_PENDING_DELETION
PRINTER_STATUS_PRINTING
PRINTER_STATUS_PROCESSING
PRINTER_STATUS_TONER_LOW
PRINTER_STATUS_UNAVAILABLE
PRINTER_STATUS_USER_INTERVENTION
PRINTER_STATUS_WAITING
PRINTER_STATUS_WARMING_UP
根据名称获得handle可以使用TPrinter类。
 
能不能给个实现的代码啊?
 
uses WinSpool
function IsPrinterReady(const APrinter: string): Boolean;
var
hGlobal, hPrinter: THandle;
dwNeeded: DWord;
pDefs: TPrinterDefaults;
pInfo: PPrinterInfo2;
bFlag: Boolean;
begin
Result := False;
if APrinter = '' then
Exit;
hGlobal := 0;
try
FillChar(pDefs, SizeOf(TPrinterDefaults), 0);
pDefs.DesiredAccess := {STANDARD_RIGHTS_REQUIRED or} PRINTER_ACCESS_USE;
bFlag := OpenPrinter(PChar(APrinter), hPrinter, @pDefs);
dwNeeded := GetLastError;
if not bFlag or (hPrinter = INVALID_HANDLE_VALUE) then
Exit;
FillChar(pInfo, SizeOf(pInfo), #0);
WinSpool.GetPrinter(hPrinter, 2, nil, 0, @dwNeeded);
if dwNeeded = 0 then
Exit;
hGlobal := GlobalAlloc(GHND, dwNeeded);
if hGlobal = 0 then
Exit;
pInfo := PPrinterInfo2(GlobalLock(hGlobal));
if pInfo = nil then
Exit;
bFlag := WinSpool.GetPrinter(hPrinter, 2, pInfo, dwNeeded, @dwNeeded);
if bFlag then
Result := pInfo.Status = 0
else
Result := False;
finally
if pInfo <> nil then
GlobalUnlock(hGlobal);
if hGlobal <> 0 then
GlobalFree(hGlobal);
if hPrinter <> 0 then
ClosePrinter(hPrinter);
end;
end;
 
后退
顶部