Printer.GetPrinter在Delphi XE2中引发异常

时间:2015-09-08 15:04:39

标签: delphi delphi-xe2

我试图将一系列标签打印到Zebra S4M打印机上,由于某种原因,页面高度似乎卡在5厘米上,我需要它是6.8厘米。

我正在尝试使用以下代码设置纸张尺寸

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;

其中fBaseSettings包含目标Label维度(除其他外),但我不断收到错误消息 Project SPXLabels.exe raised exception class $C0000005 with message 'access violation at 0x00rred82: write of address 0x59212b17

我无法看到我做错了什么!任何帮助非常感谢。

1 个答案:

答案 0 :(得分:4)

FDevice,FDriver和FPort是指向您传入以获取数据的字符串的指针。因此,您必须先为此设置内存。

试试这个

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);