Here is a code snippet that populates a image combo box with all the printers currently installed:
procedure TMyForm.RefreshPrinterList;
var
I: Integer;
NewItem: TComboExItem;
PPrinterEnumArray, PLocator: PPrinterInfo2;
ArraySize, BufferSize: cardinal;
strBuffer: string;
const
idx_Default_Net_printer = 0;
idx_Net_printer = 1;
idx_Default_Local_printer = 2;
idx_Local_printer = 3;
begin
DefaultPrinterName := getDefaultPrinterName;
cbPrinterList.ItemsEx.Clear;
// S.G. 4/4/2008: list all other printers
// S.G. 4/4/2008: Get the necessary buffer size
ArraySize := 0;
BufferSize := 0;
PPrinterEnumArray := nil;
EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, nil, 0, BufferSize, ArraySize);
PPrinterEnumArray := AllocMem(BufferSize);
try
if EnumPrinters(PRINTER_ENUM_LOCAL or PRINTER_ENUM_CONNECTIONS, nil, 2, PPrinterEnumArray, BufferSize, BufferSize, ArraySize) then
begin
PLocator := PPrinterEnumArray;
if ArraySize > 0 then
begin
for I := 0 to ArraySize - 1 do // Iterate
begin
NewItem := cbPrinterList.ItemsEx.Add;
strBuffer := StrPas(PLocator^.pPrinterName);
UniqueString(strBuffer); // make sure we have a unique string instance and not a pointer
NewItem.Caption := strBuffer;
if AnsiSameText(DefaultPrinterName, strBuffer) then
begin
// default printer
if (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
begin
// Local, default printer
NewItem.ImageIndex := idx_Default_Local_printer;
end
else
begin
// Network default printer
NewItem.ImageIndex := idx_Default_Net_printer;
end;
cbPrinterList.ItemIndex := NewItem.Index;
end
else
begin
// default printer
if (PRINTER_ATTRIBUTE_LOCAL AND PLocator^.Attributes) <> 0 then
begin
// Local, default printer
NewItem.ImageIndex := idx_Local_printer;
end
else
begin
// Network default printer
NewItem.ImageIndex := idx_Net_printer;
end;
end;
Inc(PLocator);
end; // for
end;
end;
finally // wrap up
FreeMem(PPrinterEnumArray);
end; // try/finally
end;