?
Solved

function to delete the default printer+ delphi

Posted on 2006-06-29
6
Medium Priority
?
467 Views
Last Modified: 2010-04-05
hi there!!!

i urgently need a function in delphi that deletes the default printer. i have a function that resumes the default printer. hope this helps.please help asap

thanks



**********************************************************************
procedure ResumePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  PrinterDefaults : TPrinterDefaultsA;
  DevMode : PDeviceModeA;
  Needed : integer;
  handle:thandle;
  begin
  try
    PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
    WinSpool.OpenPrinter(PrinterName,hPrinter,nil);
    Needed := WinSpool.DocumentProperties(Handle,hPrinter,PrinterName,DevMode^,DevMode^,0);
      if Needed < 0 then
        begin
          exit;
        end;
  GetMem(DevMode,Needed);
  WinSpool.DocumentProperties(Handle,hPrinter,PrinterName,DevMode^,DevMode^,DM_COPY);
  PrinterDefaults.pDatatype := nil;
  PrinterDefaults.pDevMode := @DevMode^;
  PrinterDefaults.DesiredAccess := PRINTER_ALL_ACCESS;
  ClosePrinter(hPrinter);
  if OpenPrinter(PrinterName,hPrinter,@PrinterDefaults) then
    begin
      if WinSpool.SetPrinter(hPrinter,0,nil,PRINTER_CONTROL_RESUME) then
        begin
          ClosePrinter(hPrinter);
        end
    end;
  ClosePrinter(hPrinter);
  FreeMem(DevMode);
  except
  on e: exception do HandleException(e, 'ResumePrinter2000');
  end;
  end;
**********************************************************
0
Comment
Question by:nikolaosk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17010047
procedure DeletePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  begin
    PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
    OpenPrinter(PrinterName,hPrinter,nil);
    DeletePrinter(hPrinter);
  end;
0
 

Author Comment

by:nikolaosk
ID: 17011292

hi there atul,

your solution does not work. i have a dummy printer that i call it "test". i set it as the default printer. then i run your code. it does not work. the printer is not deleted. i use windows xp english version

thanks

nikos
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 2000 total points
ID: 17016044
Nikos, you are right it was due to access permissions. Use the following version.

procedure DeletePrinter;
var
  hPrinter : THandle;
  PrinterName : PChar;
  r : longbool;
  d : PRINTER_DEFAULTS;
begin
  FillChar(d, SizeOf(PRINTER_DEFAULTS), #0);
  d.DesiredAccess := PRINTER_ALL_ACCESS;
  PrinterName := PChar(Printer.Printers[Printer.PrinterIndex]);
  OpenPrinter(PrinterName,hPrinter, @d);
  r := WinSpool.DeletePrinter(hPrinter);
  ClosePrinter(hPrinter);
  if r then
    ShowMessage('Printer deleted successfully');
end;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nikolaosk
ID: 17016464
hi there atul.

your code works.
can you change the procedure below to the
procedure DeletePrinter(const printername: WideString);

so i can pass the name of any printer and not just the default i want to delete?

thanks
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17016482
Because the openstring requires AnsiString, wide string should not be used at it can be failed when converted to AnsiString.

procedure DeletePrinter(const printername: string);
var
  hPrinter : THandle;
  r : longbool;
  d : PRINTER_DEFAULTS;
begin
  FillChar(d, SizeOf(PRINTER_DEFAULTS), #0);
  d.DesiredAccess := PRINTER_ALL_ACCESS;
  OpenPrinter(PChar(PrinterName),hPrinter, @d);
  r := WinSpool.DeletePrinter(hPrinter);
  ClosePrinter(hPrinter);
  if r then
    ShowMessage('Printer deleted successfully');
end;
0
 

Author Comment

by:nikolaosk
ID: 17017413
thanks it works
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question