VC枚举本地所有的打印机

VC枚举本地所有的打印机

第一个例子:


#include <winspool.h>   
#include <commdlg.h>  
//函数体
	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"   
			//   
			
			strcpy (buf, (pPrinters + i)->pPrinterName);   
			strcat (buf, ";");   
			strcat (buf, (pPrinters + i)->pPortName);   
			strcat (buf, ";");   
			strcat (buf, (pPrinters + i)->pDriverName);   
			
		}      
	}
	else   
		::MessageBox (NULL, "No printers listed", "PRINTER.EXE", MB_OK);  

第二个例子:

// Use for enum printers
#include <Winspool.h>
#pragma comment(lib, "winspool.lib")

BOOL HasPrinters()
{
    DWORD Flags = PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL; //local   printers
    DWORD cbBuf;
    DWORD pcReturned;
	
    DWORD Level = 2;
    TCHAR Name[500]   ;
    LPPRINTER_INFO_2 pPrinterEnum = NULL;
	
    memset(Name,0,sizeof(TCHAR)*500);
    ::EnumPrinters(Flags, Name, Level, NULL, 0, &cbBuf, &pcReturned);
    pPrinterEnum = (LPPRINTER_INFO_2)LocalAlloc(LPTR, cbBuf+4);
	
    if (NULL == pPrinterEnum)
    {
        TRACE( "Error %1d/n ", GetLastError())   ;
        return   FALSE;
    }
	
    if (!EnumPrinters(
        Flags,    //   DWORD   Flags,   printer   object   types  
        Name,    //   LPTSTR   Name,   name   of   printer   object  
        Level,    //   DWORD   Level,   information   level  
        (LPBYTE)pPrinterEnum,    //   LPBYTE   pPrinterEnum,   printer   information   buffer  
        cbBuf,    //   DWORD   cbBuf,   size   of   printer   information   buffer
        &cbBuf, //   LPDWORD   pcbNeeded,   bytes   received   or   required  
        &pcReturned)    //   LPDWORD   pcReturned   number   of   printers   enumerated  
        )
    {
        TRACE( "Error %1d/n ", GetLastError());
        return FALSE;
    }

	LPPRINTER_INFO_2 p = pPrinterEnum;
	for (int i=0; i<pcReturned;i++)
	{		
		TRACE( "PrinterName: %s\n ", p->pPrinterName);
		p++;
	}
	
	
    if (pcReturned > 0)
    {
        return TRUE;
    }
    else 
	{
        return FALSE;
	}  
}


发布了306 篇原创文章 · 获赞 270 · 访问量 272万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览