#133827 - 2005-02-15 07:56 PM
Detect amount of RAM / memory
|
M_Kiewitz
Fresh Scripter
Registered: 2005-02-15
Posts: 15
|
Here is a script I just wrote some hours ago.
It detects the total amount of RAM within a computer anyway it's "kinda" faulty and detects 256MB sometimes as being 248MB (note: this is NOT a script bug but a windows bug)
FUNCTION GETCOMPUTERMEMORY ()
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1
ENDIF
$GETCOMPUTERMEMORY = ""+$RAMAmount+" MB"
ENDIF
ENDFUNCTION
UPDATE: Code got fixed, should now work even till some TBs.
Regards
M. Kiewitz
Edited by M_Kiewitz (2005-02-16 06:28 PM)
|
|
Top
|
|
|
|
#133828 - 2005-02-15 08:27 PM
Re: Detect amount of RAM / memory
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
Well, will one of these UDFs work for you any better?
Memory() - Get Physical RAM size - local or remote
MemSize() - returns installed memory size
MemSize2() - Retrieve Physical memory size
And I've placed your code into TAGS to preserved the formatting.
Code:
FUNCTION GETCOMPUTERMEMORY () Dim $BaseKey,$RAMTranslated,$RAMAmount $BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory" $RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8) IF @ERROR=0 $RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2)+SUBSTR($RAMTranslated,3,2)+LEFT($RAMTranslated,2))+16371712+1048576)/1048576 IF ($RAMAmount mod 2)=1 $RAMAmount = $RAMAmount+1 ENDIF $GETCOMPUTERMEMORY = ""+$RAMAmount+" MB" ENDIF ENDFUNCTION
|
|
Top
|
|
|
|
#133829 - 2005-02-15 08:49 PM
Re: Detect amount of RAM / memory
|
M_Kiewitz
Fresh Scripter
Registered: 2005-02-15
Posts: 15
|
Quote:
Well, will one of these UDFs work for you any better?
No.
The first one uses the same technique, but contains huge amounts of bloated code.
The second one uses COM/OLE, which I don't use cause of compatibility reasons
The third one is badly coded. If the READVALUE operation doesn't succeed it bombs out instead of checking @ERROR.
All of them reply the same bad memory amount on some machines cause the bug is windows.
Edited by M_Kiewitz (2005-02-15 08:50 PM)
|
|
Top
|
|
|
|
#133833 - 2005-02-15 11:08 PM
Re: Detect amount of RAM / memory
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
WOW... I can't believe how much easier this code is... the amazing thing is, both this and the MEMORY UDF use the same .Translated Value, but pull the data together from completely different places within it.
Anyway, might you try this and see if you get a better result for 256MB. Any takers to try it on the GB ranges?
Code:
FUNCTION GETCOMPUTERMEMORY ()
Dim $BaseKey,$RAMTranslated,$RAMAmount
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = cdbl((VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2)+SUBSTR($RAMTranslated,3,2)+LEFT($RAMTranslated,2))+16371712.0+1048576.0))/1048576.0
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1.0
ENDIF
$GETCOMPUTERMEMORY = ""+round($RAMAmount)+" MB"
ENDIF
ENDFUNCTION
|
|
Top
|
|
|
|
#133837 - 2005-02-16 03:31 AM
Re: Detect amount of RAM / memory
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
Not that I'm asking for any "Glory" here, but I did have a hand in the Memory UDF that Glenn posted. Memory() Testing
Anyway, I still think its interesting this code works in some instances using totally different segments of the .Translated value. But since it "appears" it has it limits, I'll leave it up to M Kiewitz to figure that out.
|
|
Top
|
|
|
|
#133840 - 2005-02-16 09:36 AM
Re: Detect amount of RAM / memory
|
M_Kiewitz
Fresh Scripter
Registered: 2005-02-15
Posts: 15
|
Quote:
No problem. Can you please provide either a Microsoft KB or other URL where there is documented proof of this error you speak of.
If there would be a KB, there would be a fix.
I just got "some" computers in our network and some of them have 512MB RAM. Strangely some report 512MB and some of them report e.g. 504/503MB (!). I'm collecting that info via login-script currently and adjust our database accordingly. I'm now using a "round-up" algo that will fit badly reported RAM into the right amount, but that's REXX.
BTW. In fact actually they report 511MB and my code fixes those into 512MB. Also some are reported as BEING 512MB (without fixing them). Very strange.
[QUOTE]
NOTE: Your current code does appear to support the NoVarsInStrings option, just thought I would mention it as you are a new member here and may not know.
I'm using NoVarsInStrings myself. Doing without this option produces unreadable code (at least imho). I don't use DIMs anywhere though.
Also about that >2GB problem. I already know some (non bloated) solution. Will include that and post the new code then. Thanx for testing.
Edited by M_Kiewitz (2005-02-16 09:53 AM)
|
|
Top
|
|
|
|
#133841 - 2005-02-16 06:30 PM
Re: Detect amount of RAM / memory
|
M_Kiewitz
Fresh Scripter
Registered: 2005-02-15
Posts: 15
|
Quote:
Further testing.
Should work now till some Terabytes or even further. Still not bloated 
|
|
Top
|
|
|
|
#133843 - 2005-02-23 04:32 PM
Re: Detect amount of RAM / memory
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
Looks like he updated the topmost post with the new code. For readablity I've included it in this post using the code tags:
Code:
FUNCTION GETCOMPUTERMEMORY () $BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory" $RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8) IF @ERROR=0 $RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16 IF ($RAMAmount mod 2)=1 $RAMAmount = $RAMAmount+1 ENDIF $GETCOMPUTERMEMORY = ""+$RAMAmount+" MB" ENDIF ENDFUNCTION
I tested it with 512 MB with no problems... Doc you want to give it the GB test runs?
|
|
Top
|
|
|
|
#133844 - 2005-02-23 04:51 PM
Re: Detect amount of RAM / memory
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
M Kiewitz: I hacked on your function, dimming the variables, removing the MB so that it just returns a number, and adding the option for checking remote pcs. Once everyone has tested this, you might consider adding your function to the UDF forum.
Code:
FUNCTION GETCOMPUTERMEMORY (optional $target)
Dim $Basekey,$RAMTranslated
If $Target <> ''
$Target = IIf(Left($Target, 2) <> '\\', '\\' + $Target, $Target)
$Target = IIf(Right($Target, 1) <> '\', $Target + '\', $Target)
EndIf
$BaseKey =$target + "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$GetComputerMemory = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16
IF ($GetComputerMemory mod 2)=1
$GetComputerMemory = $GetComputerMemory+1
ENDIF
ENDIF
ENDFUNCTION
|
|
Top
|
|
|
|
#133845 - 2005-02-23 09:12 PM
Re: Detect amount of RAM / memory
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
Al and M Kiewitz,
I've made some other minor modifications to the code as well and will try them out on 3GB system in a few minutes. Here at home so have to login to work to test.
Function GetComputerMemory(optional $sComputer) Dim $Basekey,$RAMTranslated $BaseKey = 'HKLM\Hardware\ResourceMap\System Resources\Physical Memory' If Not $sComputer $sComputer = @WKSTA Else $BaseKey = '\\' + $sComputer + '\' + $BaseKey EndIf $RAMTranslated = Right(ReadValue($BaseKey, '.Translated'), 8) If Not @ERROR $GetComputerMemory = (Val('&'+Right($RAMTranslated,2)+SubStr($RAMTranslated,5,2))+250+16)/16 If ($GetComputerMemory Mod 2)=1 $GetComputerMemory = $GetComputerMemory+1 EndIf Else $GetComputerMemory=@ERROR Exit $GetComputerMemory EndIf EndFunction |
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|