[已解决]自己做的获取实际内存大小的UDF,求精简及优化
本帖最后由 kingfirekkk 于 2010-10-28 19:02 编辑使用WMI方式获取的内存与实际内存大小总是有些区别,这点在集成显卡的主板上面犹为明显,特地配合CPUZ.exe写了一个“UDF”,请各位大大看看能否改进....另外,如果别人的机器上面没装CPUZ.exe或者CPUz.exe的路径不对的时候,怎么处理呢?
#include-once
Local $x=""
getmem()
MsgBox(0,"提示!","本机实际内存大小为:"&@CRLF&$x&"MB")
Func getmem()
$sFile ="report.txt"
Run("cpuz.exe"&' -txt=report',@ScriptDir)
Sleep(2000)
$getfile=FileRead($sFile)
$sR=stringRegExp($getfile,"\W+Max module size\W+(\d{4}\s+)MBytes",3)
;$sR1=stringRegExp($getfile,"\W+max capacity\W+(\d{4}\s+)MBytes",3)
;_ArrayDisplay($sR, UBound($sR))
;_ArrayDisplay($sR1, UBound($sR1))
$x=$sr
;$y=$sr1
;MsgBox(0,"提示!","本机实际内存大小为:"&@CRLF&$x&"MB")
;MsgBox(0,"提示!","本机实际内存大小为:"&@CRLF&$y&"MB")
Return $x
EndFunc
FileInstall打包一个不就完了吗
判断路径肯定不方便 因为我用的就是绿色版没法判断路径 调用第三方绝对会存在诸多问题,比如兼容问题和效率问题。 收下留着后用 换了台机器,就发现问题了,压根没有关键字:Max module size和max capacity,请楼上的兄弟不急着收藏了.而关键字Memsize又不是我想要的... 这个应该是准确的。
#include <array.au3>
#Include <WinAPI.au3>
Local $info,$str
$info=_WinAPI_GlobalMemoryStatus()
$str='内存使用百分比为:'&$info&"%"&@CRLF& _
'内存总数为(K):'&Int($info/1024)&""&@CRLF& _
'可用内存数为(K):'&Int($info/1024)&""
MsgBox(0,0,$str) 6#果然是高手,连这个API都知道 支持楼主继续改进。
cpuz是绿色的,可以不管目标机器是否有安装cpuz,都 FileInstall 。
6楼的方法,在 32位 WindowsXP 的情况下,不能发现实际存在的 4G 物理内存。 感谢3mile ,6#代码好用{:face (317):} 不错,6楼牛B 6楼兄弟的代码,仍然无法获取实际内存大小,不过仍然谢谢捧场.. 回复 11# kingfirekkk
上图看下问题出在什么地方。 使用3mile兄的程式运行后的结果:
使用Everest实际装的内存条,2条2G的...如下图:
所以想实现的功能是真真正正的找出实际的内存大小。
谢谢各位大大! 本帖最后由 3mile 于 2010-10-28 09:34 编辑
不知道任务管理器中表现内存如何啊?
用wmi来获取试试:
; 生成于 AutoIt Scriptomatic
__wmi_Win32_PhysicalMemory()
Func __wmi_Win32_PhysicalMemory()
Local $wbemFlagReturnImmediately = 0x10
Local $wbemFlagForwardOnly = 0x20
Local $colItems = ""
Local $strComputer = "localhost"
Local $Output=""
$Output &= "Computer: " & $strComputer& @CRLF
$Output &= "==========================================" & @CRLF
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemory", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
For $objItem In $colItems
$Output &= "BankLabel: " & $objItem.BankLabel & @CRLF
$Output &= "Capacity: " & $objItem.Capacity & @CRLF
$Output &= "Caption: " & $objItem.Caption & @CRLF
$Output &= "CreationClassName: " & $objItem.CreationClassName & @CRLF
$Output &= "DataWidth: " & $objItem.DataWidth & @CRLF
$Output &= "Description: " & $objItem.Description & @CRLF
$Output &= "DeviceLocator: " & $objItem.DeviceLocator & @CRLF
$Output &= "FormFactor: " & $objItem.FormFactor & @CRLF
$Output &= "HotSwappable: " & $objItem.HotSwappable & @CRLF
$Output &= "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
$Output &= "InterleaveDataDepth: " & $objItem.InterleaveDataDepth & @CRLF
$Output &= "InterleavePosition: " & $objItem.InterleavePosition & @CRLF
$Output &= "Manufacturer: " & $objItem.Manufacturer & @CRLF
$Output &= "MemoryType: " & $objItem.MemoryType & @CRLF
$Output &= "Model: " & $objItem.Model & @CRLF
$Output &= "Name: " & $objItem.Name & @CRLF
$Output &= "OtherIdentifyingInfo: " & $objItem.OtherIdentifyingInfo & @CRLF
$Output &= "PartNumber: " & $objItem.PartNumber & @CRLF
$Output &= "PositionInRow: " & $objItem.PositionInRow & @CRLF
$Output &= "PoweredOn: " & $objItem.PoweredOn & @CRLF
$Output &= "Removable: " & $objItem.Removable & @CRLF
$Output &= "Replaceable: " & $objItem.Replaceable & @CRLF
$Output &= "SerialNumber: " & $objItem.SerialNumber & @CRLF
$Output &= "SKU: " & $objItem.SKU & @CRLF
$Output &= "Speed: " & $objItem.Speed & @CRLF
$Output &= "Status: " & $objItem.Status & @CRLF
$Output &= "Tag: " & $objItem.Tag & @CRLF
$Output &= "TotalWidth: " & $objItem.TotalWidth & @CRLF
$Output &= "TypeDetail: " & $objItem.TypeDetail & @CRLF
$Output &= "Version: " & $objItem.Version & @CRLF
if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
$Output=""
Next
Else
Msgbox(0,"WMI 输出","没有在类 " & "Win32_PhysicalMemory" & "中找到WMI对象" )
Endif
EndFunc
Func WMIDateStringToDate($dtmDate)
Return (StringMid($dtmDate, 5, 2) & "/" & _
StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc 谢谢3Mile,正式的UDF出来了,我自己精简了一下,不知道会不会有问题,通过以下UDF,能够得到实际的内存大小值,希望对大家有用哦!!谢谢!
$x=getmem()
MsgBox(0,"MEM is",$x/1024/1024/1024&'GB')
Func getmem()
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$Output=""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemory", "WQL", _
$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
For $objItem In $colItems
$Output = $Output &$objItem.Capacity & @CRLF
;~ if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
$Output+=$output
Return $output
$Output=""
Next
Else
Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PhysicalMemory" )
Endif
EndFunc
页:
[1]
2