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

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-02 14:44:18

#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;

//遍历电脑中的打印机
void 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);     
          
    }     
      
    if (!EnumPrinters (dwFlags, NULL, 2, (LPBYTE) pPrinters,     
        cbPrinters, &cbPrinters, &cReturned))     
    {     
        ::MessageBox (NULL, "error",     
            "error", MB_OK | MB_ICONEXCLAMATION);     
    }     
      
    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"     
            //     
             printf("printer name:%s\n",(pPrinters+i)->pPrinterName);
printf("printer portname:%s\n",(pPrinters+i)->pPortName);
#if 0
            strcpy (buf, (pPrinters + i)->pPrinterName);     
            strcat (buf, ";");     
            strcat (buf, (pPrinters + i)->pPortName);     
            strcat (buf, ";");     
            strcat (buf, (pPrinters + i)->pDriverName);     
#endif
        }        
    }  
    else     
        ::MessageBox (NULL, "No printers listed", "PRINTER.EXE", MB_OK);
#endif
}

//获取默认打印机
void GetSystemDefaultPrinter()
{
char szBuff[1024] = {0};
DWORD len = 1024;
int ret;


ret = GetDefaultPrinter(szBuff,&len);
if(ret == FALSE){
printf("getdefault printer error:%d\n",GetLastError());
return ;
}
printf("default printer:%s\n",szBuff);
return;
}

阅读(1839) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~