Article View: borland.public.cppbuilder.nativeapi
Article #12631Getting, Storing and setting printer properties
From: eric67
Date: Mon, 24 Nov 2003 16:04
Date: Mon, 24 Nov 2003 16:04
87 lines
3469 bytes
3469 bytes
I need help on restoring printer settings from a registry key. The
backup code works fine, retrieves printer settings and stores them
into the registry. Here is the restore code:
fPrtCfg is the printer configuration data buffer
fPrtCfgSize is it's byte size
/*********************************************************************
***********/
FIRST VERSION:
TPrinter * prt=Printer();
char * Device[256];
char * Driver[256];
char * Port [256];
prt->GetPrinter(Device,Driver,Port,DevMode); // Get printer device
name etc.
HGLOBAL myHandle=GlobalAlloc(GHND,fPrtCfgSize); // allocate a global
handle
void * pDevMode = (void *)GlobalLock(myHandle); // get the memory
pointer
memcpy(pDevMode,fPrtCfg,fPrtCfgSize); // fill it with previously
stored data
GlobalUnlock( (void*)DevMode ); // release memory (keep the handle)
prt->SetPrinter(Device,Driver,Port,(THandle)myHandle); // GlobalFree
will be done by TPrinter
/*********************************************************************
***********/
SECOND VERSION:
TPrinter * prt=Printer();
char * Device[256];
char * Driver[256];
char * Port [256];
prt->GetPrinter(Device,Driver,Port,DevMode); // Get printer device
name etc.
HANDLE prtHandle;
if (OpenPrinter(Device,&prtHandle,NULL)!=0) // retrieve printer handle
needed by document properties
{
unsigned size=DocumentProperties(0,prtHandle,Device,NULL,NULL, 0);
// retrieve size needed
HGLOBAL myHandle=GlobalAlloc(GHND,size); // allocate a global
handle of needed size
void * pDevMode = (void *)GlobalLock(myHandle); // get the memory
pointer
memcpy(pDevMode,fPrtCfg,std::min(size,fPrtCfgSize)); // fill it
with previously stored data, use std::min to prevent buffer overflow
DocumentProperties(0,prtHandle,Device,(DEVMODE*)pDevMode,(DEVMODE*)pDe
vMode, DM_IN_BUFFER|DM_OUT_BUFFER);
// do I need to set pDevMode->dmFields? I don't know what fields
changed, potentially all...
GlobalUnlock( (void*)DevMode );
ClosePrinter(prtHandle);
prt->SetPrinter(Device,Driver,Port,(THandle)myHandle); //
GlobalFree will be done by TPrinter
}
/*********************************************************************
***********/
Both work.
The second performs the DocumentProperties validation required by
Windows but I don't know if I use it fine.
To execute it I need the printer handle that i can retrieve only with
an OpenPrinter followed by a ClosePrinter. Does it conflict with
TPrinter encapsulation of the printer object?
I think the second version is better.
Any idea and help appreciated.
Thanks
Technical Details
Message-ID:
<3fc22c03$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,fbc54db82be36bb,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-11-24 08:11:09 PST From: "eric67" <myfantasy@libero.it> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Getting, Storing and setting printer properties Date: Mon, 24 Nov 2003 17:04:07 +0100 Lines: 89 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 217.56.76.254 Message-ID: <3fc22c03$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1069689859 217.56.76.254 (24 Nov 2003 08:04:19 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail