f



How can I send a print to a different printer than the default printer?

I want to send my print to a different printer than the default printer. The only argument i have is the 'cDevice'. Is it possible to do that ? 

Regards
George
0
BGeorge
4/2/2013 8:53:09 PM
comp.clipper.visual-objects 12618 articles. 0 followers. Post Follow

4 Replies
697 Views

Similar Articles

[PageSpeed] 45

Hi George,
try this:

FUNCTION SetDefaultPrinter(cPrinter AS STRING) AS LOGIC
// Set the given printer to be the default printer
// cPrinter: name of printer to be made default
// returns: whether successful

LOCAL hPrinter AS PTR
LOCAL pInfo AS _WinPrinter_Info_2
LOCAL size AS DWORD
LOCAL done AS LOGIC

IF OpenPrinter(String2Psz(cPrinter), @hPrinter, NULL_PTR)
     GetPrinter(hPrinter, 2, NULL_PTR, 0, @size)
     pInfo := MemAlloc(size)
     IF GetPrinter(hPrinter, 2, pInfo, size, @size)
         pInfo.attributes := _Or(pInfo.attributes, 
PRINTER_ATTRIBUTE_DEFAULT)
         done := SetPrinter(hPrinter, 2, pInfo, 0)
     ENDIF
     MemFree(pInfo)
     ClosePrinter(hPrinter)
ENDIF

RETURN done

HTH,
Francesco

Il 02/04/2013 22:53, BGeorge ha scritto:
> I want to send my print to a different printer than the default printer. The only argument i have is the 'cDevice'. Is it possible to do that ?
>
> Regards
> George
>
0
Francesco
4/3/2013 6:25:19 AM
Francesco,
I put in cPrinter the oPrinter:Device and always return done=FALSE.
I don't know why. Any ideas?

Regards,
George
0
BGeorge
4/6/2013 6:22:30 PM
George,
no idea why...
Anyway, digging in the past, I found an interesting thread opened by 
Andy Rintoul in 2002 with a couple of functions, SetPrinter and 
GetPrinter, that seem to be what you're looking for; here they are:
--------------------------------------------------------------------------------
FUNCTION SetW2KDefPrinter(cPrinter AS STRING) AS LOGIC
// Set the given printer to be the default printer
// cPrinter: name of printer to be made default
// returns whether successful

LOCAL done AS LOGIC
LOCAL hModule AS PTR
LOCAL pSet AS PSZ PTR 	
LOCAL pszPrinter AS PSZ PTR
LOCAL pszError AS PSZ

done := FALSE

hModule := _VOLoadLibrary(String2Psz("winspool.drv"))//.drv NOT .dll
IF hModule == NULL_PTR
	done := FALSE
     // Show error msg
ELSE	
     pSet := GetProcAddress(hModule, PSZ("SetDefaultPrinterA"))
     // "SetDefaultPrinterW" for UNICODE
     IF pSet == NULL_PTR
       	done := FALSE
       	// Show error msg
     ELSE
       	pszPrinter := PSZ(cPrinter)
       	IF PCALL(pSet, pszPrinter) = NULL_PTR
			done := FALSE
         	FormatMessage(  ;
	  					FORMAT_MESSAGE_ALLOCATE_BUFFER + FORMAT_MESSAGE_FROM_SYSTEM,;
			      	  	NULL,;
     	  				GetLastError(),;
     	 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),; // Default language
     	  				@pszError,;
     	  				0,;
     	  				NULL)
			// Show error msg including pszError
		  	LocalFree(pszError)
       	ELSE
			Done := TRUE	
       	ENDIF	
       	// Tell all open programs about the change	
       	SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L,;
                            SMTO_NORMAL, 1000, NULL)
	ENDIF	
ENDIF

RETURN done
--------------------------------------------------------------------------------
FUNCTION GetW2KDefPrinter() AS STRING
LOCAL hModule AS PTR
LOCAL pGet AS PSZ PTR  		
LOCAL cPrinter AS STRING
LOCAL dwNeeded AS DWORD
LOCAL PrintBuff AS PSZ PTR

dwNeeded := 0	//geoff's suggest

hModule := _VOLoadLibrary(String2Psz("winspool.drv"))	//.drv NOT .dll
IF hModule == NULL_PTR
     // Show error msg
ELSE	
     pGet := GetProcAddress(hModule, PSZ("GetDefaultPrinterA"))
     // "GetDefaultPrinterW" for UNICODE
     IF pGet == NULL_PTR
       	// Show error msg
     ELSE
       	PCALL(pGet, NULL_PSZ, @dwNeeded)
       	IF dwNeeded > 0
         	PrintBuff := PSZ(Space(dwNeeded))
         	PCALL(pGet, PrintBuff, @dwNeeded)
         	IF PrintBuff == NULL_PTR
           		// Show error msg
         	ELSE
	  			cPrinter := Psz2String(PrintBuff)
         	ENDIF	
       	ELSE
         	// Show error msg
       	ENDIF
     ENDIF
ENDIF	

RETURN cPrinter
--------------------------------------------------------------------------------
(not well indented, but...)
It compiles without errors in 2.7.
The functions were written for W2K, but I suppose they'll work on Win7 
too; I've no time to make a try, so let me know.
HTH,
Francesco


Il 06/04/2013 20:22, BGeorge ha scritto:
> Francesco,
> I put in cPrinter the oPrinter:Device and always return done=FALSE.
> I don't know why. Any ideas?
>
> Regards,
> George
>
0
Francesco
4/7/2013 8:22:57 AM
Hi!

Here I have done something like what you want... each sector of my company has its own default printer so I save it in my database doing something like this...

METHOD GetPrinter( ) CLASS ConfigPrinter
	LOCAL oPrinter AS PrintingDevice
	
	oPrinter := PrintingDevice{}
 	IF  oPrinter :setup()
 		sDefaultPrinterXX :=  oPrinter:Device+','+oPrinter:Driver+','+oPrinter:Port
 		ENDIF

 	ELSE
 		TEXTBox{,"Warning","Error Getting Printer"}:Show()
 	ENDIF

        //sDefaultPrinterXX is global, so I save it on DB and get it every time it starts, here just a model cause I have more sDefaultPrinters vars

RETURN SELF

I've done this and changed my report classes to use this info... Device string is there and there's driver and port too... I needed all of them, but as you asking just for cDevice maybe just that is enough...

hope it helps
0
marc3l0
5/9/2013 1:59:35 PM
Reply: