 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vincent Guest
|
Posted: Thu Dec 18, 2003 2:53 pm Post subject: Rename a local printer in windows... |
|
|
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
|
Posted: Wed Dec 24, 2003 8:25 pm Post subject: Re: Rename a local printer in windows... |
|
|
"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 |
|
 |
|
|
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
|
|