Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

155 views
Skip to first unread message

BGeorge

unread,
Apr 2, 2013, 4:53:09 PM4/2/13
to
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

Francesco F.

unread,
Apr 3, 2013, 2:25:19 AM4/3/13
to
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

BGeorge

unread,
Apr 6, 2013, 2:22:30 PM4/6/13
to
Francesco,
I put in cPrinter the oPrinter:Device and always return done=FALSE.
I don't know why. Any ideas?

Regards,
George

Francesco F.

unread,
Apr 7, 2013, 4:22:57 AM4/7/13
to
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

marc3l0

unread,
May 9, 2013, 9:59:35 AM5/9/13
to
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 new messages