Skip to content

Commit

Permalink
don't access registry directly, use dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Rousse authored and Guillaume Rousse committed Mar 4, 2012
1 parent 9fcd8bf commit 24d85a6
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/Printers.pm
Expand Up @@ -4,11 +4,6 @@ use strict;
use warnings;

use English qw(-no_match_vars);
use Win32::TieRegistry (
Delimiter => '/',
ArrayValues => 0,
qw/KEY_READ/
);

use FusionInventory::Agent::Tools::Win32;

Expand Down Expand Up @@ -52,6 +47,7 @@ sub doInventory {
my (%params) = @_;

my $inventory = $params{inventory};
my $logger = $params{logger};

foreach my $object (getWmiObjects(
class => 'Win32_Printer',
Expand All @@ -76,7 +72,7 @@ sub doInventory {
$object->{VerticalResolution};
}

$object->{Serial} = _getUSBPrinterSerial($object->{PortName})
$object->{Serial} = _getUSBPrinterSerial($object->{PortName}, $logger)
if $object->{PortName} && $object->{PortName} =~ /USB/;

$inventory->addEntry(
Expand All @@ -103,22 +99,24 @@ sub doInventory {
}

sub _getUSBPrinterSerial {
my ($portName) = @_;

my $machKey = $Registry->Open('LMachine', {
Access => KEY_READ | KEY_WOW64_64 ## no critic (ProhibitBitwise)
}) or die "Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR";
my ($portName, $logger) = @_;

# first, find the USB container ID for this printer
my $prefix = _getUSBPrefix(
$machKey->{"SYSTEM/CurrentControlSet/Enum/USBPRINT"},
getRegistryKey(
path => "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Enum/USBPRINT",
logger => $logger
),
$portName
);
return unless $prefix;

# second, get the serial number from the ID container entry
my $serial = _getUSBSerial(
$machKey->{"SYSTEM/CurrentControlSet/Enum/USB"},
getRegistryKey(
path => "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Enum/USB",
logger => $logger
),
$prefix
);

Expand Down

0 comments on commit 24d85a6

Please sign in to comment.