Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can a C++ program distinguish an actual printer from a virtual printer (e.g. a PDF creator)?
Posted

To do this you can use PRINTER_INFO_2 which has a field called pPortName. And then you can string compare it with the string "FILE" and get all the queues which are connected to the "FILE" port. And you can similarly add in more parsing logic for detecting other virtual printers. This structure is filled up by Windows when you call GetPrinter with the printer handle.
 
Share this answer
 
Comments
sooraj subramanya 2-Dec-11 2:51am    
@Laxmikant_Yadav: great idea...Thanks...PORT_INFO_(1,2,3) also helps.
Thanks again...
«_Superman_» 2-Dec-11 3:02am    
This will probably work in most situations, but it is possible to create a virtual printer where the port can be COM1 or USB1 in which case the port monitor will be customized.
sooraj subramanya 12-Dec-11 9:11am    
@Laxmikant_Yadav AND @johny10151981: Thanks for the above help. I have successfully implemented it in windows. Do u have any idea like how to implement the same in MAC OS X ???
to add with Laxmi,
I wrote(probably, cant recall, whether I wrote it or downloaded it from any site) to get all the list of printers
C++
int getAllPrinter(HWND hList)
{
	int i,j;
    wchar_t		*pPrInfo4=NULL;
    wchar_t		*pPrInfoP=NULL;
	wchar_t     *pStr    =NULL;
    DWORD       dwNeeded = 0;
	DWORD       dwReturned = 0;
	DWORD       dwReturnedP = 0;
	DWORD       dwBytes = 0;
	wchar_t     *strStr;
	wchar_t		strStrP[257];
	int pos;

	EnumPrinters( PRINTER_ENUM_LOCAL,NULL,1,NULL,dwBytes,&dwNeeded,&dwReturned);
    if( dwNeeded != 0)
       { pPrInfo4 = new wchar_t [dwNeeded+128];
		 dwBytes  = dwNeeded+128;
	     EnumPrinters( PRINTER_ENUM_LOCAL,NULL,1,(LPBYTE)pPrInfo4,dwBytes,&dwNeeded,&dwReturned);

		for( i=0; i<(int)dwReturned; i++ )
		   { 
				strStr = (wchar_t*)((PRINTER_INFO_1*)pPrInfo4)[i].pName;
				pos=ListBox_FindString(hList,0,strStr);
				if(pos==LB_ERR)
				{
					ListBox_AddString(hList,strStr);
				}
		   }
  	    delete pPrInfo4;
		pPrInfo4 = NULL;
	   }
    pPrInfo4=NULL;
    dwNeeded = 0;
	dwReturned = 0;
	dwBytes = 0;
	EnumPrinters( PRINTER_ENUM_LOCAL | PRINTER_ENUM_NETWORK,NULL,1,NULL,dwBytes,&dwNeeded,&dwReturned);
    if( dwNeeded != 0)
    { 
		pPrInfo4 = new wchar_t [dwNeeded+128];
		dwBytes  = dwNeeded+128;
        EnumPrinters( PRINTER_ENUM_LOCAL | PRINTER_ENUM_NETWORK,NULL,1,(LPBYTE)pPrInfo4,dwBytes,&dwNeeded,	&dwReturned);
		for( i=0; i<(int)dwReturned; i++ )
		{ 
			strStr = (wchar_t*)((PRINTER_INFO_1*)pPrInfo4)[i].pName;
			pos=ListBox_FindString(hList,0,strStr);
			if(pos==LB_ERR)
			{
				//wprintf(L"%s\n",strStr);
				ListBox_AddString(hList,strStr);
			}
		   }
  	    delete pPrInfo4;
		pPrInfo4 = NULL;
	   }
    pPrInfo4=NULL;
    dwNeeded = 0;
	dwReturned = 0;
	dwBytes = 0;
	EnumPrinters( PRINTER_ENUM_REMOTE,NULL,1,NULL,dwBytes,&dwNeeded,&dwReturned);
    if( dwNeeded != 0)
	{ 
		pPrInfo4 = new wchar_t [dwNeeded+128];
		dwBytes  = dwNeeded+128;
		EnumPrinters( PRINTER_ENUM_REMOTE,	NULL,	1,		(LPBYTE) pPrInfo4,	dwBytes,	&dwNeeded,	&dwReturned);
		for( i=0; i<(int)dwReturned; i++ )
		{	 
			wcscpy(strStr ,((PRINTER_INFO_1*)pPrInfo4)[i].pName);
			if( -1 == strposw(strStr,L"!!",0))   
			{
				pos=ListBox_FindString(hList,0,strStr);
				if(pos==LB_ERR)
				{
					//wprintf(L"%s\n",strStr);
					ListBox_AddString(hList,strStr);
				}
			}
			else 
			{ 
				pPrInfoP   = NULL;
				dwNeeded   = 0;
				dwReturnedP= 0;
				dwBytes    = 0;
				pStr = new wchar_t [wcslen(strStr)+1];
				wcscpy(pStr,strStr);
  				EnumPrinters(PRINTER_ENUM_NAME,pStr,1,NULL, dwBytes,&dwNeeded,	&dwReturnedP);
				if( dwNeeded != 0)
				{ 
					pPrInfoP = new wchar_t [dwNeeded+128];
					dwBytes  = dwNeeded+128;
  					EnumPrinters( PRINTER_ENUM_NAME,	pStr,	 1,		 (LPBYTE)pPrInfoP, dwBytes,		&dwNeeded,	&dwReturnedP);
					for( j=0; j<(int)dwReturnedP; j++ )
					{ 
						wcscpy(strStrP,((PRINTER_INFO_1*)pPrInfoP)[j].pName);
			            pos=ListBox_FindString(hList,0,strStr);
						if(pos==LB_ERR)
						{
							//wprintf(L"%s\n",strStr);
							ListBox_AddString(hList,strStr);
						}
		            }
					delete pStr;
  					delete pPrInfoP;
					pPrInfoP = NULL;
				}
			}
		}
  	    delete pPrInfo4;
		pPrInfo4 = NULL;
	}
	return true;  // return TRUE  unless you set the focus to a control
}
 
Share this answer
 
Comments
sooraj subramanya 2-Dec-11 3:01am    
Its helping a lot... Thanks..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900