Solved

function to delete the default printer+ delphi

Posted on 2006-06-29
6
464 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 500 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
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!

 

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

Technology Partners: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

689 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