atome
atome

Reputation: 21

How to save and restore printer device mode to file in Delphi?

I would like to save all printer settings of the chosen printer to a file in hex format. And later, load them from the file. How do I do that?

I tried with the following. This is not in hex format.

procedure SaveDevModeToFile(deviceHandle: THandle; filePath: string);
var
  pDevmode: PDeviceMode;
  fs : TFileStream;
begin
  pDevMode := GlobalLock(deviceHandle);
  if pDevmode <> nil then
    try
      fs:= TFileStream.Create(filePath, fmCreate);
      try
        fs.WriteBuffer(PChar(pDevmode)^, sizeof(TDevicemode));
      finally
        fs.Free;
      end;
    finally
      GlobalUnlock(deviceHandle);
    end;
end;

procedure AskSettingsAndSave;
var MyPrinter, MyDriver, MyPort: array[0..255] of Char;
  DeviceHandle: THandle;
begin
  if PrinterSetupDialog1.Execute(Self.Handle) then
    begin
      Printer.GetPrinter(MyPrinter, MyDriver, MyPort, DeviceHandle);

      if DeviceHandle <> 0 then
      begin
        SaveDevModeToFile(DeviceHandle, 'd:\temp\dm.txt');
      end;
    end;
end;

Upvotes: 0

Views: 84

Answers (0)

Related Questions