引言
作为一个单位的网管,很多时候要获取所管理电脑的系统信息、硬件信息等内容,用ahk写了一个小的工具获取这些信息!代码中相关函数已经集成到一个类中,大家可以方便的调用!如果不会使用请留言,河许人手把手教你使用。
寄语
希望本篇技术分享是一个起点,不是一个终点,技术分享不易,希望您多多鼓励,奖赏一个贝怎么样?!
代码分享
;获取电脑的信息,构建了计算机类方便有能力的朋友调用!
;河许人
;2017-12-15
#Persistent
#SingleInstance, force
#NoEnv
#WinActivateForce
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1
px:=A_ScreenWidth/2-200
py:=A_ScreenHeight/2-145
Gui,2:Default
Gui,2:Color, Black
Gui,2:Color, ControlColor, Black
Gui,2:Font,cYellow,Fixedsys
Gui,2:Add,Text , x220 y10 ,系统名称
Gui,2:Add,Text , x220 y40 ,操作系统版本号
Gui,2:Add,Text , x220 y70 ,操作系统的服务软件包
Gui,2:Add,Text , x220 y100 ,计算机名
Gui,2:Add,Text , x220 y130 ,用户名
Gui,2:Add,Text , x220 y160 ,IP地址
Gui,2:Add,Text , x220 y190 ,物理内存
Gui,2:Add,Text , x220 y220 ,Cpu序列号
Gui,2:Add,Text , x220 y250 ,MAC地址
Gui,2:Add,Edit , x10 y10 w200 h25 vED1 cRed Readonly Right,
Gui,2:Add,Edit , x10 y40 w200 h25 vED2 Readonly Right,
Gui,2:Add,Edit , x10 y70 w200 h25 vED3 Readonly Right,
Gui,2:Add,Edit , x10 y100 w200 h25 vED4 Readonly Right,
Gui,2:Add,Edit , x10 y130 w200 h25 vED5 cGreen Readonly Right,
Gui,2:Add,Edit , x10 y160 w200 h25 vED6 Readonly Right,
Gui,2:Add,Edit , x10 y190 w200 h25 vED7 cTeal Readonly Right,
Gui,2:Add,Edit , x10 y220 w200 h25 vED8 Readonly Right,
Gui,2:Add,Edit , x10 y250 w200 h25 vED9 Readonly Right,
Gui,2:show, x%px% y%py% w400 h290 ,电脑信息
gosub,a0
return
2Guiclose:
ExitApp
a0:
RegRead, ProductName, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion, ProductName
GMSEx := Computer.GlobalMemoryStatusEx()
GMSExM01 := Round(GMSEx[2] / 1024**2, 2)
CpuID:=Computer.GetCpuID()
MacAddress:=Computer.GetMacAddress()
if ( OSVersion := Computer.GetOSVersionInfo() )
GuiControl, 2:, ED1, % productname
GuiControl, 2:, ED2, % OSVersion.EasyVersion
GuiControl, 2:, ED3, % OSVersion.ServicePackString
GuiControl, 2:, ED4, % a_computername
GuiControl, 2:, ED5, % a_username
GuiControl, 2:, ED6, % a_ipaddress1
GuiControl, 2:, ED7, % GMSExM01 MB
GuiControl, 2:, ED8, % CpuID
GuiControl, 2:, ED9, % MacAddress
;~ CpuID:=Computer.GetCpuID()
;~ MacAddress:=Computer.GetMacAddress()
;~ StdoutToVar_CreateProcess("notepad") ;没有返回值
;~ Version := StdoutToVar_CreateProcess("wmic cpu get Processorid") ;有返回值
return
class Computer
{
; ***** FUNCTIONS *****
;;1.获取CPU序列号函数
;方法一
;~ GetCpuID()
;~ {
;~ objSWbemObject:=ComObjGet("winmgmts:Win32_Processor.DeviceID='cpu0'")
;~ 序列号:=objSWbemObject.ProcessorId
;~ return,%序列号%
;~ }
;方法二
;~ GetCpuID()
;~ {
;~ 序列号:=objSWbemObject.ProcessorId
;~ 命令:="wmic cpu get Processorid"
;~ RunWait, %ComSpec% /c %命令% | clip,,hide
;~ if RegExMatch(clipboard, "iO)([\w `t]+)[`r`n `t]+([^`r`n]+)", match)
;~ {
;~ m_First := match.Value(1)
;~ m_Second := match.Value(2)
;~ return % m_Second
;~ }
;~ }
;方法三
GetCpuID()
{
Version := computer.StdoutToVar_CreateProcess("wmic cpu get Processorid")
if RegExMatch(Version, "iO)([\w `t]+)[`r`n `t]+([^`r`n]+)", match)
{
m_First := match.Value(1)
m_Second := match.Value(2)
return % m_Second
}
}
;;2.获取网卡硬件地址
;~ ;方法一
;~ GetMacAddress()
;~ {
;~ RunWait, %ComSpec% /c getmac /NH | clip,,hide
;~ RegExMatch(clipboard, ".*?([0-9A-Z].{16})(?!\w\\Device)", mac)
;~ return %mac1%
;~ }
;~ ;方法二
;~ GetMacAddress()
;~ {
;~ NetworkConfiguration:=ComObjGet("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
;~ for mo in NetworkConfiguration
;~ {
;~ if(mo.IPEnabled <> 0)
;~ return mo.MacAddress
;~ }
;~ }
;方法三
GetMacAddress()
{
NetworkConfiguration := computer.StdoutToVar_CreateProcess("getmac")
RegExMatch(NetworkConfiguration, ".*?([0-9A-Z].{16})(?!\w\\Device)", mac)
return %mac1%
}
;获取系统版本信息
GetOSVersionInfo()
{
static Ver
if !Ver
{
VarSetCapacity(OSVer, 284, 0)
NumPut(284, OSVer, 0, "UInt")
if !DllCall("GetVersionExW", "Ptr", &OSVer)
return 0 ; GetSysErrorText(A_LastError)
Ver := Object()
Ver.MajorVersion := NumGet(OSVer, 4, "UInt")
Ver.MinorVersion := NumGet(OSVer, 8, "UInt")
Ver.BuildNumber := NumGet(OSVer, 12, "UInt")
Ver.PlatformId := NumGet(OSVer, 16, "UInt")
Ver.ServicePackString := StrGet(&OSVer+20, 128, "UTF-16")
Ver.ServicePackMajor := NumGet(OSVer, 276, "UShort")
Ver.ServicePackMinor := NumGet(OSVer, 278, "UShort")
Ver.SuiteMask := NumGet(OSVer, 280, "UShort")
Ver.ProductType := NumGet(OSVer, 282, "UChar")
Ver.EasyVersion := Ver.MajorVersion . "." . Ver.MinorVersion . "." . Ver.BuildNumber
}
return Ver
}
;获取内存物理状态
GlobalMemoryStatusEx()
{
static MEMORYSTATUSEX, init := VarSetCapacity(MEMORYSTATUSEX, 64, 0) && NumPut(64, MEMORYSTATUSEX, "UInt")
if (DllCall("Kernel32.dll\GlobalMemoryStatusEx", "Ptr", &MEMORYSTATUSEX))
{
return { 2 : NumGet(MEMORYSTATUSEX, 8, "UInt64")
, 3 : NumGet(MEMORYSTATUSEX, 16, "UInt64")
, 4 : NumGet(MEMORYSTATUSEX, 24, "UInt64")
, 5 : NumGet(MEMORYSTATUSEX, 32, "UInt64") }
}
}
; Im Original gilt "CP0"; zu CharSet CP850/CP858 vgl.: https://goo.gl/Y8xUYu , http://goo.gl/cMtc6i , https://goo.gl/ssCplI , https://goo.gl/s2P1jK
StdoutToVar_CreateProcess(sCmd, sEncoding:="CP858", sDir:="", ByRef nExitCode:=0)
{
DllCall( "CreatePipe", PtrP,hStdOutRd, PtrP,hStdOutWr, Ptr,0, UInt,0 )
DllCall( "SetHandleInformation", Ptr,hStdOutWr, UInt,1, UInt,1 )
VarSetCapacity( pi, (A_PtrSize == 4) ? 16 : 24, 0 )
siSz := VarSetCapacity( si, (A_PtrSize == 4) ? 68 : 104, 0 )
NumPut( siSz, si, 0, "UInt" )
NumPut( 0x100, si, (A_PtrSize == 4) ? 44 : 60, "UInt" )
NumPut( hStdInRd, si, (A_PtrSize == 4) ? 56 : 80, "Ptr" )
NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 60 : 88, "Ptr" )
NumPut( hStdOutWr, si, (A_PtrSize == 4) ? 64 : 96, "Ptr" )
if ( !DllCall( "CreateProcess", Ptr,0, Ptr,&sCmd, Ptr,0, Ptr,0, Int,True, UInt,0x08000000
, Ptr,0, Ptr,sDir?&sDir:0, Ptr,&si, Ptr,&pi ) )
return ""
, DllCall( "CloseHandle", Ptr,hStdOutWr )
, DllCall( "CloseHandle", Ptr,hStdOutRd )
DllCall( "CloseHandle", Ptr,hStdOutWr ) ; The write pipe must be closed before Reading the stdout.
VarSetCapacity(sTemp, 4095)
while ( DllCall( "ReadFile", Ptr,hStdOutRd, Ptr,&sTemp, UInt,4095, PtrP,nSize, Ptr,0 ) )
sOutput .= StrGet(&sTemp, nSize, sEncoding)
DllCall( "GetExitCodeProcess", Ptr,NumGet(pi,0), UIntP,nExitCode )
DllCall( "CloseHandle", Ptr,NumGet(pi,0) )
DllCall( "CloseHandle", Ptr,NumGet(pi,A_PtrSize) )
DllCall( "CloseHandle", Ptr,hStdOutRd )
return sOutput
}
}