Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone help with Win32/Perl

Status
Not open for further replies.

akelabanda

Programmer
Dec 19, 2001
61
IN
Hi

I'm using one of the samples from Dave Roth's site for
making use of Win32 module for perl. I'm getting the
following error when I run the script.

************************************************
Can't call method "InstancesOf" on an undefined value at memory.pl line 54
************************************************

Please describe the dependencies if any. Do I need to
register some DLL before using this ? How do I verify
if I have installed the package correctly.

It would be helpful, if there is a step by step
description to get started.

The script is as under. Cant figure out the problem.
Can someone help please

===========================================
# Memory.pl
# -----------
# This will attempt to display the memory configuration
# for the specified machine. It uses WMI so it may or
# may not work for your particular machine.
# Syntax:
# perl Memory.pl [Machine Name]
#
# Examples:
# perl Memory.pl
# perl Memory.pl \\server
#
# 2002.01.20 rothd@roth.net
#
# Permission is granted to redistribute and modify this code as long as
# the below copyright is included.
#
# Copyright © 2002 by Dave Roth
# Courtesty of Roth Consulting
#
use vars qw( $Log $Message $Name $Value );
use strict;
use Win32::OLE qw( EVENTS HRESULT in );

my @MEM_TYPES = qw(
Unable_to_deterime Unknown Other DRAM
Synchronous DRAM Cache DRAM EDO EDRAM
VRAM SRAM RAM ROM Flash EEPROM FEPROM
EPROM CDRAM 3DRAM SDRAM SGRAM
);

my @MEM_DETAIL = qw(
Unable_to_deterime Reserved Other
Unknown Fast-paged Static column
Pseudo-static RAMBUS Synchronous
CMOS EDO Window_DRAM Cache_DRAM
Non-volatile
);

my @FORM_FACTOR = qw(
Unknown_Type Non_Recognized_Type
SIP DIP ZIP SOJ Proprietary_Type
SIMM DIMM TSOP PGA RIMM SODIMM
);

my $Class = "Win32_PhysicalMemory";
my $Total;
my $iCount = 0;
(my $Machine = shift @ARGV || "." ) =~ s/^[\\\/]+//;
my $WMIServices = Win32::OLE->GetObject( "winmgmts:{impersonationLevel=impersonate,(security)}//$Machine/" );

$~ = "INFO";
foreach my $Object ( in( $WMIServices->InstancesOf( $Class ) ) )
{
my $Speed = $Object->{Speed} || "unknown speed";

print ++$iCount . ") $Object->{Name} ($FORM_FACTOR[$Object->{FormFactor}])\n";
DumpInfo( "Tag", $Object->{Tag} );
DumpInfo( "Type", $MEM_TYPES[$Object->{MemoryType}] );
DumpInfo( "Detail", $MEM_DETAIL[$Object->{TypeDetail}] );
DumpInfo( "Size", FormatMemory( $Object->{Capacity} ) . "bytes" );
DumpInfo( "Speed", "$Speed (ns)" );
DumpInfo( "Location", "$Object->{BankLabel} ($Object->{DeviceLocator})" );
if( $Object->{DataWidth} != $Object->{TotalWidth} )
{
print "\tThis is ECC memory.\n";
}
$Total += $Object->{Capacity};
print "\n";
}
printf( "\nTotal Memory: %s ( %sbytes )\n",
FormatNumber( $Total ),
FormatMemory( $Total ) );

sub FormatNumber
{
my($Number) = @_;
while( $Number =~ s/^(-?\d+)(\d{3})/$1,$2/ ){};
return( $Number );
}

sub FormatMemory
{
my( $Size ) = @_;
my $Format;
my $Suffix;
my $K = 1024;
my $M = $K * 1024;
if( $M < $Size )
{
$Suffix = &quot;M&quot;;
$Format = $Size / $M;
}
elsif( $K < $Size )
{
$Suffix = &quot;K&quot;;
$Format = $Size / $K;
}
else
{
$Format = $Size;
}
$Format =~ s/\.(\d){1,2}\d+/.$1/;
return( FormatNumber( $Format ) . &quot; $Suffix&quot; );
}

sub DumpInfo
{
local( $Name ) = shift @_;
local( $Value ) = shift @_;
write;
}

format INFO =
@<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
&quot;$Name:&quot;, $Value
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Value
.
===========================================
 
Hi,

It's the line that creates $WMIServices that is failing on your machine, this one:

my $WMIServices = Win32::OLE->GetObject( &quot;winmgmts:{impersonationLevel=impersonate,(security)}//$Machine/&quot; );

The author says in the comments that &quot;It uses WMI so it may or may not work for your particular machine&quot;.

It works fine on mine... Have you tried anything else that uses the Windows Management Interface (WMI) on your machine? Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Hi

I did try to register another windows script
but it failed. That's when I started exploring
perl until I get this patch from my developer.

But to answer, your question, no I am not using
another program that may be using WMI on my machine.

Does it look for some DLL registration ?

Thanks for your help.
Rajeev
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top