Como mudar a impressora default no Windows via programação

 

uses WinSpool;

 

procedure ChangeDefaultPrinter(const Name: string);

var

    W2KSDP: function(pszPrinter: PChar): Boolean; stdcall;

    H: THandle;

    Size, Dummy: Cardinal;

    PI: PPrinterInfo2;

begin

    if (Win32Platform = VER_PLATFORM_WIN32_NT) and

     (Win32MajorVersion >= 5) then

    begin

      @W2KSDP := GetProcAddress(GetModuleHandle(winspl), 'SetDefaultPrinterA');

      if @W2KSDP = nil then RaiseLastOSError;

      if not W2KSDP(PChar(Name)) then RaiseLastOSError;

    end

    else

    begin

      if not OpenPrinter(PChar(Name), H, nil) then RaiseLastOSError;

      try

        GetPrinter(H, 2, nil, 0, @Size) ;

        if GetLastError <> ERROR_INSUFFICIENT_BUFFER then RaiseLastOSError;

        GetMem(PI, Size) ;

        try

          if not GetPrinter(H, 2, PI, Size, @Dummy) then RaiseLastOSError;

          PI^.Attributes := PI^.Attributes or PRINTER_ATTRIBUTE_DEFAULT;

          if not SetPrinter(H, 2, PI, PRINTER_CONTROL_SET_STATUS) then

            RaiseLastOSError;

        finally

          FreeMem(PI) ;

        end;

      finally

        ClosePrinter(H) ;

      end;

    end;

end; //ChangeDefaultPrinter

 

 

Nota: Usando a variável de Impressora global do Delphi (do tipo TPrinter) você pode preencher um ComboBox de todas as impressoras disponíveis:  

 

cboPrinters.Items.Assign(Printer.Printers) ;

 

Então, quando um usuário clica algum botão (por exemplo) chame o procedimento de

 

if cboPrinters.ItemIndex = -1 then Exit;

  ChangeDefaultPrinter(cboPrinters.Items[cboPrinters.ItemIndex]) ;