这个过程错在哪?(50分)

  • 主题发起人 主题发起人 fisher2003
  • 开始时间 开始时间
F

fisher2003

Unregistered / Unconfirmed
GUEST, unregistred user!
//请问各位这个过程错在哪?
我想通过该过程获得打印机队列中的打印机名!
procedure TForm1.Button2Click(Sender: TObject);
var
//longbuffer: array of LongInt;
printInfo: array[0..50] of PRINTER_INFO_2;
pInfo: PRINTER_INFO_2;
numbytes,numneeded,numprinters: DWord;
c: integer;
retval: longbool;
retstr: array[0..255] of char;
begin
numneeded:= 0;
numprinters:= 0;
retval:= EnumPrinters(PRINTER_ENUM_LOCAL,nil,2,@printInfo,51*(sizeof(PRINTER_INFO_2)),numneeded,numprinters);
if not retval then
begin
numbytes:= numneeded;
showmessage(inttostr(numbytes));
retval:= Enumprinters(PRINTER_ENUM_LOCAL,nil,2,@printInfo,numbytes,numneeded,numprinters);
if not retval then
begin
showmessage('Could not Successfully enumerate the Printers');
Exit;
end;
end;
for c:= 0 to numprinters-1do
begin
try
pInfo:= printInfo[c];
retstr:= '';
strCopy(retstr,pInfo.pPrinterName);
showmessage(retstr);
memo1.Text:= memo1.Text + #13#10+retstr;
except
exit;
end;
end;

end;
 
太多了,不好看,你可以用以下一句话就能获得电脑中已经安装的所有打印机:
ListBox1.Items := Printer.Printers;
请你多看看Printer这个对象吧,它有很多东东的!:)
 
uses printers;
//
Printer.Printers返回已安装打印机的列表
 
不好意思,这个过程我还有其他用途,你们说的我都知道!
我调试过程序,我好象是
retval:= Enumprinters(PRINTER_ENUM_LOCAL,nil,2,@printInfo,numbytes,numneeded,numprinters);
有错误,不知道错在哪
 
以下摘自delphi自身带的Printers.Pas单元;
function TPrinter.GetPrinters: TStrings;
var
LineCur, Port: PChar;
Buffer, PrinterInfo: PChar;
Flags, Count, NumInfo: DWORD;
I: Integer;
Level: Byte;
begin
if FPrinters = nil then
begin
FPrinters := TStringList.Create;
Result := FPrinters;
try
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Flags := PRINTER_ENUM_CONNECTIONS or PRINTER_ENUM_LOCAL;
Level := 4;
end
else
begin
Flags := PRINTER_ENUM_LOCAL;
Level := 5;
end;
Count := 0;
EnumPrinters(Flags, nil, Level, nil, 0, Count, NumInfo);
if Count = 0 then
Exit;
GetMem(Buffer, Count);
try
if not EnumPrinters(Flags, nil, Level, PByte(Buffer), Count, Count, NumInfo) then
Exit;
PrinterInfo := Buffer;
for I := 0 to NumInfo - 1do
begin
if Level = 4 then
with PPrinterInfo4(PrinterInfo)^do
begin
FPrinters.AddObject(pPrinterName,
TPrinterDevice.Create(nil, pPrinterName, nil));
Inc(PrinterInfo, sizeof(TPrinterInfo4));
end
else
with PPrinterInfo5(PrinterInfo)^do
begin
LineCur := pPortName;
Port := FetchStr(LineCur);
while Port^ <> #0do
begin
FPrinters.AddObject(Format(SDeviceOnPort, [pPrinterName, Port]),
TPrinterDevice.Create(nil, pPrinterName, Port));
Port := FetchStr(LineCur);
end;
Inc(PrinterInfo, sizeof(TPrinterInfo5));
end;
end;
finally
FreeMem(Buffer, Count);
end;
except
FPrinters.Free;
FPrinters := nil;
raise;
end;
end;
Result := FPrinters;
end;
 
后退
顶部