Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337173
  • 博文数量: 64
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 589
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-08 15:50
文章分类

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-21 15:51:13

//遍历本机上的所有打印机设备,然后向所有打印机发送一个打印任务,发送的打印任务默认文件名为"本地下层文档"
#include <windows.h>

#define PRINTER_NUM 50
static char printer[PRINTER_NUM][256];
//遍历电脑中的打印机
static int get_all_printer_device()
{
DWORD            dwFlags = PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL;     
LPPRINTER_INFO_2 pPrinters;     
DWORD            cbPrinters;     
DWORD            cReturned, i;  
char             buf[256];  


EnumPrinters (dwFlags, NULL, 2, NULL, 0, &cbPrinters,     
&cReturned);     


if (!(pPrinters = (LPPRINTER_INFO_2) LocalAlloc (LPTR, cbPrinters + 4)))     
{     
//::MessageBox (NULL, "error", "error", MB_OK | MB_ICONEXCLAMATION);     
printf("Alloc failed\n");
return -1;


}     


if (!EnumPrinters (dwFlags, NULL, 2, (LPBYTE) pPrinters,     
cbPrinters, &cbPrinters, &cReturned))     
{     
//::MessageBox (NULL, "error", "error", MB_OK | MB_ICONEXCLAMATION); 
printf("Enum Printer failed\n");
free(pPrinters);
return -1;
}     


if (cReturned > 0)     
{  
for (i = 0; i < cReturned; i++)     
{     
// for each printer in the PRINTER_INFO_2 array: build a string that     
//   looks like "DEVICE_NAME;PORT;DRIVER_NAME"       
if(i <= PRINTER_NUM-1){
strcpy(printer[i],(pPrinters+i)->pPrinterName);
}
//printf("printer name:%s\n",(pPrinters+i)->pPrinterName);
//printf("printer portname:%s\n",(pPrinters+i)->pPortName);
}        
}  
else{
//::MessageBox (NULL, "No printers listed", "PRINTER.EXE", MB_OK);
printf("No printers listed\n");
return -1;
}
return 0;
}

int main(){
HANDLE printerHandle;
ADDJOB_INFO_1 *pJobInfo = NULL;
DWORD cbNeeded;
int i;
pJobInfo = (ADDJOB_INFO_1 *)malloc(1024);

get_all_printer_device();
for(i = 0; i < PRINTER_NUM;i++){
if(strlen(printer[i]) == 0){
continue;
}
if(!OpenPrinter(printer[i],&printerHandle,NULL)){
//printf("open printer failed:%s\n",printer[i]);
continue;
}
//printf("addjob printer:%s\n",printer[i]);
if(!AddJob(printerHandle,1,(LPBYTE)pJobInfo,1024,&cbNeeded)){
//printf("addjob failed\n");
continue;
}else{
//printf("addjob ok\n");
//printf("addjob path:%s\n",pJobInfo->Path);
ClosePrinter(printerHandle);
memset(printer[i],0,256);
}
memset(pJobInfo,0,1024);
}
free(pJobInfo);
return 0;
}
阅读(2663) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~