BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Rename a local printer in windows...

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi (General)
View previous topic :: View next topic  
Author Message
Vincent
Guest





PostPosted: Thu Dec 18, 2003 2:53 pm    Post subject: Rename a local printer in windows... Reply with quote



Hi,

I'd like to rename a local printer in windows with a Delphi tool.
I use GetPrinter and SetPrinter Windows Api to change the value of
PrinterInfo2.pPrinterName but this does not work and I have an access
violation when calling the SetPrinter Api :

var PrinterInfo2:TPrinterInfo2;
var PrinterHandle:THandle;

OpenPrinter(PChar(OldPrinterName),PrinterHandle,nil);
PrinterInfo2.pPrinterName:=PChar(NewPrinterName);
Result:=WinSpool.SetPrinter(PrinterHandle,2,Addr(PrinterInfo2),0);

Who has a solution ?

Thanks a lot

Vince
Back to top
James Mauldin
Guest





PostPosted: Wed Dec 24, 2003 8:25 pm    Post subject: Re: Rename a local printer in windows... Reply with quote



"Vincent" <vluc (AT) mis (DOT) mc> wrote

Quote:
Hi,

I'd like to rename a local printer in windows with a Delphi tool.
I use GetPrinter and SetPrinter Windows Api to change the value of
PrinterInfo2.pPrinterName but this does not work and I have an access
violation when calling the SetPrinter Api :

var PrinterInfo2:TPrinterInfo2;
var PrinterHandle:THandle;

OpenPrinter(PChar(OldPrinterName),PrinterHandle,nil);
PrinterInfo2.pPrinterName:=PChar(NewPrinterName);
Result:=WinSpool.SetPrinter(PrinterHandle,2,Addr(PrinterInfo2),0);

Who has a solution ?

Thanks a lot

Vince

See the Windows SDK about this. Before calling SetPrinter, you need to call
GetPrinter twice in order to obtain a valid PRINTER_INFO_2 (PrinterInfo2)
structure. This is tricky, because PrinterInfo2 contains a reference to the
device mode, and the size of the devmode structure that Windows returns is
bigger than the SizeOf(DEVMODE) because there is a section of private data
after the last member of the record.

You'll need to use a PPrinterInfo2 rather than a PrinterInfo2 variable. Your
code would look something like this:

var
pcbNeeded : DWORD;
PrinterInfo : PPrinterInfo2;
....

// get the printer handle
OpenPrinter(PChar(OldPrinterName),PrinterHandle,nil);

// get the required size of the PrinterInfo structure in pcbNeeded
GetPrinter(PrinterHandle,2,nil,0,@pcbNeeded);

// allocate the memory
PrinterInfo := AllocMem(pcbNeeded);

// call GetPrinter again with pcbNeeded passed to the cbBuf parameter
// if the method succeeds (and it should), PrinterInfo will contain a valid
structure
GetPrinter(PrinterHandle,2,PrinterInfo,pcbNeeded,@pcbNeeded);

// change whatever you want
PrinterInfo.pPrinterName:=PChar(NewPrinterName);

// now call set printer with the revised structure
SetPrinter(PrinterHandle,2,PrinterInfo2,0);

FreeMem(PrinterInfo);

HTH,

-- James



The first time is to get the required size of the PrinterInfo strucut



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi (General) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.