//遍历本机上的所有打印机设备,然后向所有打印机发送一个打印任务,发送的打印任务默认文件名为"本地下层文档"
#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;
}
阅读(2674) | 评论(0) | 转发(0) |