列出本地打印机 [英] List local printers
问题描述
我正在使用此例程列出安装在计算机上的本地打印机:
I am using this routine to list the local printers installed on on a machine:
var
p: pointer;
hpi: _PRINTER_INFO_2A;
hGlobal: cardinal;
dwNeeded, dwReturned: DWORD;
bFlag: boolean;
i: dword;
begin
p := nil;
EnumPrinters(PRINTER_ENUM_LOCAL, nil, 2, p, 0, dwNeeded, dwReturned);
if (dwNeeded = 0) then exit;
GetMem(p,dwNeeded);
if (p = nil) then exit;
bFlag := EnumPrinters(PRINTER_ENUM_LOCAL, nil, 2, p, dwneeded, dwNeeded, dwReturned);
if (not bFlag) then exit;
CbLblPrinterPath.Properties.Items.Clear;
for i := 0 to dwReturned - 1 do
begin
CbLblPrinterPath.Properties.Items.Add(TPrinterInfos(p^)[i].pPrinterName);
end;
FreeMem(p);
TPrinterInfos(p ^ )[i] .pPrinterName
返回打印机名称的'P'。我已将PdfCreator安装为打印机。
TPrinterInfos(p^)[i].pPrinterName
returns a 'P' for printer name. I have a PdfCreator installed as a printer.
TPrinterInfos
是 _PRINTER_INFO_2A
。
我该如何解决?
推荐答案
好吧,首先要做的事情...自从您标记了Delphi-2010以来,我假设您在D2010中遇到了这个问题。
Ok, first thing first... Since you tagged this Delphi-2010, I'd assume you are having this problem with D2010.
您的问题始于您使用_PRINTER_INFO_2A,这是函数EnumPrinters的Ansi版本使用的结构。自从引入unicode以来, EnumPrinters函数就映射到该函数的unicode版本,因此,您应该使用_PRINTER_INFO_2W。 (或显式调用EnumPrintersA)。如果使用EnumPrinters(无A / W),则应使用_PRINTER_INFO_2(无A / W)。这样一来,如果有一天UTF32版本成为新标准,则中断的可能性就较小。
Your problem start with your use of _PRINTER_INFO_2A, which is the structure used by the Ansi version of the function EnumPrinters. Ever since unicode has been introduced, the "EnumPrinters" function maps to the unicode version of the function, and thus, you should use _PRINTER_INFO_2W. (Or explicitly call EnumPrintersA). If you are using EnumPrinters(Without A/W) you should use the _PRINTER_INFO_2(without A/W). That way, it will be less likely to break if one day a UTF32 version becomes the new standard.
这篇关于列出本地打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!