// this function returns a list of printers.
// It may be used for local or remote host. When used for local host,
// all the parameters must be empty. For remote computer, parameters must be:
// TargetHost: name or IP address of the host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// Adapters: the out parameter that this function fills with names of printers.
procedure ListPrinters(TargetHost, UserName, Password: string; Printers: TStrings);
var
Query: TWmiQuery;
Connection: TWmiConnection;
begin
Query := TWmiQuery.Create(nil);
Connection := TWmiConnection.Create(nil);
try
Connection.MachineName := TargetHost;
Connection.Credentials.UserName := UserName;
Connection.Credentials.Password := Password;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
Connection.Connected := true;
except
Exit;
end;
Query.WQL.Text := 'select Name from Win32_Printer';
Query.Connection := Connection;
Query.Open;
while not Query.EOF do
begin
Printers.Add(Query.FieldByName('Name').AsString);
Query.Next;
end;
finally
Query.Free;
Connection.Free;
end;
end;
|