Paul
Paul

Reputation: 43

Printer.GetPrinter raises an exception in Delphi XE2

I'm trying to print a series of labels to a Zebra S4M printer, for some reason the page height seems to be stuck on 5 cm and I need it to be 6.8 cm.

I am trying to set the Paper size using the following code

var
  FDevice: PChar;
  FDriver: PChar;
  FPort: PChar;
  DeviceMode: THandle;
  DevMode: PDeviceMode;
begin
  {to get a current printer settings}
  Printer.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
  {lock a printer device}
  DevMode := GlobalLock(DeviceMode);
  DevMode^.dmPaperSize := 0;
  DevMode^.dmPaperWidth := fBaseSettings.Width;
  DevMode^.dmPaperLength := fBaseSettings.Height;
  Printer.SetPrinter(FDevice, FDriver, FPort, DeviceMode);
  {unlock a device}
  GlobalUnlock(DeviceMode);
end;

where fBaseSettings contains the target Label dimensions (amongst other things), but I keep getting an error message Project SPXLabels.exe raised exception class $C0000005 with message 'access violation at 0x00rred82: write of address 0x59212b17

I can't see what I am doing wrong! Any help much appreciated.

Upvotes: 2

Views: 2082

Answers (1)

Larsdk
Larsdk

Reputation: 713

FDevice, FDriver and FPort are pointers to a string that you pass in to get the data back. Thus you must first allowcate memory for this.

try this

var 
  FDevice, FDriver, FPort: string;
begin
  SetLength(FDevice, 200);
  SetLength(FDriver, 200);
  SetLength(FPort, 200);
  {to get a current printer settings}
  Printer.GetPrinter(PChar(FDevice), PChar(FDriver), PChar(FPort), DeviceMode);

Upvotes: 5

Related Questions