专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++ Builder

怎样知道打印机是否处于联机状态?该如何处理

发布时间:2011-06-28 12:53:53 文章来源:www.iduyao.cn 采编人员:星星草
怎样知道打印机是否处于联机状态?
我是不使用驱动程序,直接将数据发往打印机,这样打印机通电时正常,无联机时就会死机!所以需要在打印前确认打印机是否正常。查了好多天了,没有得出结论,希望大虾能给出最简单的原码,拜谢!

------解决方案--------------------
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);
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: