Solved

function to delete the default printer+ delphi

Posted on 2006-06-29
6
449 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
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
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
Comment Utility

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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:nikolaosk
Comment Utility
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
Comment Utility
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
Comment Utility
thanks it works
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now