怎样知道打印机是否处于联机状态?
我是不使用驱动程序,直接将数据发往打印机,这样打印机通电时正常,无联机时就会死机!所以需要在打印前确认打印机是否正常。查了好多天了,没有得出结论,希望大虾能给出最简单的原码,拜谢!
[解决办法]
http://topic.csdn.net/t/20021203/22/1228402.html 这里面也有谈
倒是 C#能做,,要不你做C#写个DLL 来调用得了
using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using activeds; // Import activeds.tlb interop assembly
/************************************************************************** ******
// needs a reference to the interop assembly generated by tlbimp.exe
// Compile with csc /r:activeds.dll adprinter.cs
*************************************************************************** *****/
class Tester {
// Printer status flags - see SDK docs for status values (IADsPrintQueueOperations)
[Flags]
enum PrinterStatus {
Paused = 1,
DeletePending = 2,
Error = 3,
PaperJam = 4,
PaperOut = 5
}
public static void Main() {
DirectoryEntry printer = new DirectoryEntry("WinNT://servername/printqueuename", null, null, AuthenticationTypes.ServerBind);
Console.WriteLine(printer.Path);
PropertyCollection pcoll = printer.Properties;
try
{
foreach(string sc in pcoll.PropertyNames) {
Console.WriteLine(sc + ":\t" + (pcoll[sc])[0].ToString());
}
Console.WriteLine("---------------------------------");
}
catch(COMException ex)
{
Console.WriteLine(ex.Message);
}
// Use the PrintqueueOperations interface to contol the printer Queue
IADsPrintQueueOperations po = printer.NativeObject as IADsPrintQueueOperations ;
if(po != null) {
if(po.Status == (int)PrinterStatus.Paused) // If paused resume else pause printer
po.Resume();
else
po.Pause();
// Use the IADsPrintJob to control individual printjobs
foreach(IADsPrintJob pj in po.PrintJobs()) {
Console.WriteLine("{0} - {1}", pj.Description, pj.Name);
IADsPrintJobOperations pjo = pj as IADsPrintJobOperations;
Console.WriteLine(pjo.Name);
// Use IADsPrintJob.Name as arg. to remove the job from the queue
po.PrintJobs().Remove(pj.Name);
}
}
printer.Dispose();
}
}
这是 C#的代码。
[解决办法]
汗。。。
[解决办法]
告诉你一个简单方法,直接调用windows的打印机功能出来,由用户自己设定打印各项功能,就同Word的打印面版一模一样。
或者:新开一个线程专门用于打印,打印机没有联机也不影响到你主线程的运行啊。
总之,编程没有定式,可以说条条道路通罗马,只要你思维稍微灵活一点。
[解决办法]
/*
//从并行端口读取打印机状态
function GetPrinterStatus:byte;
asm
MOV DX,$379;
IN AL,DX;
end;
//获取打印机是否出错
function CheckPrinter:boolean;
var
temp:byte;
begin
temp:=GetPrinterStatus;
Result:=not ( ((temp and $80)=0) //打印机忙
or ((temp and $20)<>0) //打印机缺纸
or ((temp and $10)=0) //打印机未联机
or ((temp and $08)=0) ); //打印机出错;
end;
*/
unsigned char GetPrinterStatus()
{
asm
{
MOV DX,0x379
}
}
bool LptPrint(char prtdata[],int prtlen,int timeout)
{
HANDLE h;
DWORD n;
COMMTIMEOUTS t;
bool result=true;
h = CreateFile("lpt1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (h == INVALID_HANDLE_VALUE){
//AfxMessageBox("Can not open lpt1");
return false;
}
t.ReadIntervalTimeout = 0;
t.ReadTotalTimeoutMultiplier = 0;
t.ReadTotalTimeoutConstant = 0;
t.WriteTotalTimeoutMultiplier = timeout * 1000 / prtlen;
t.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(h,&t)){
//AfxMessageBox("SetCommTimeout error");
return false;
}
result = true;
/* if (!WriteFile(h,prtdata,prtlen,&n,NULL)){
//AfxMessageBox("Print error");
result = false;
}
*/
CloseHandle(h);
return result;
}