Snippets ( Hardware Information ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (→‎_GetTotalScreenResolution ~ Author - LarryDalooza: fixed code formatting -- I'll get the hang of this wiki stuff one day)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<div class="center" style="width:auto; margin-left:auto; margin-right:auto;">'''Please always credit an author in your script if you use their code, Its only polite.'''</div>
__TOC__
===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ComputerNameAndModel() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[category:Snippets]]
 
{{Snippet Credit Header}}
 
== _ComputerNameAndModel ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 23: Line 32:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetComputerModel() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | Return To Contents]]
 
== _GetComputerModel ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 31: Line 47:
Func _GetComputerModel()
Func _GetComputerModel()
     Local $oWMIService = ObjGet("winmgmts:\\.\")
     Local $oWMIService = ObjGet("winmgmts:\\.\")
     Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_ComputerSystem", "WQL", 0x30), $sDescription
 
     Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_ComputerSystem", "WQL", 0x30)
Local $sDescription
 
     If IsObj($oColItems) Then
     If IsObj($oColItems) Then
         For $oObjectItem In $oColItems
         For $oObjectItem In $oColItems
             $sDescription &= $oObjectItem.Model
             $sDescription &= $oObjectItem.Model
         Next
         Next
         Return $sDescription
         Return $sDescription
     EndIf
     EndIf
     Return SetError(1, 1, 0)
     Return SetError(1, 1, 0)
EndFunc  ;==>_GetComputerModel
EndFunc  ;==>_GetComputerModel
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetDriveBusType() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top|Return To Contents]]
 
== _GetComputerModel_2 ==
 
{{Snippet Header
| AuthorURL = 84272-rindeal
| AuthorName = rindeal
| Desc=Faster version of the snippet above
}}
 
<syntaxhighlight lang="autoit">
; Can return nothing relevant if machine is not a factory build
ConsoleWrite(_GetComputerModel() & @CRLF)
 
Func _GetComputerModel()
Return RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS", "SystemProductName")
EndFunc  ;==>_GetComputerModel
</syntaxhighlight>
 
[[#top|Return To Contents]]
 
== _GetDriveBusType ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}
 
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include <APIConstants.au3>
#include <APIConstants.au3>
Line 49: Line 97:
Local $aDrive = DriveGetDrive('ALL')
Local $aDrive = DriveGetDrive('ALL')
For $i = 1 To $aDrive[0]
For $i = 1 To $aDrive[0]
    ConsoleWrite(StringUpper($aDrive[$i]) & ' => ' & _GetDriveBusType($aDrive[$i]) & @CRLF)
ConsoleWrite(StringUpper($aDrive[$i]) & ' => ' & _GetDriveBusType($aDrive[$i]) & @CRLF)
Next
Next


; Get a string representation of a drive's bus type.
; Get a string representation of a drive's bus type.
Func _GetDriveBusType($sDrive)
Func _GetDriveBusType($sDrive)
    Local $aArray[14][2] = [[$DRIVE_BUS_TYPE_UNKNOWN, 'UNKNOWN'], _
Local $aArray[14][2] = [[$DRIVE_BUS_TYPE_UNKNOWN, 'UNKNOWN'], _
            [$DRIVE_BUS_TYPE_SCSI, 'SCSI'], _
[$DRIVE_BUS_TYPE_SCSI, 'SCSI'], _
            [$DRIVE_BUS_TYPE_ATAPI, 'ATAPI'], _
[$DRIVE_BUS_TYPE_ATAPI, 'ATAPI'], _
            [$DRIVE_BUS_TYPE_ATA, 'ATA'], _
[$DRIVE_BUS_TYPE_ATA, 'ATA'], _
            [$DRIVE_BUS_TYPE_1394, '1394'], _
[$DRIVE_BUS_TYPE_1394, '1394'], _
            [$DRIVE_BUS_TYPE_SSA, 'SSA'], _
[$DRIVE_BUS_TYPE_SSA, 'SSA'], _
            [$DRIVE_BUS_TYPE_FIBRE, 'FIBRE'], _
[$DRIVE_BUS_TYPE_FIBRE, 'FIBRE'], _
            [$DRIVE_BUS_TYPE_USB, 'USB'], _
[$DRIVE_BUS_TYPE_USB, 'USB'], _
            [$DRIVE_BUS_TYPE_RAID, 'RAID'], _
[$DRIVE_BUS_TYPE_RAID, 'RAID'], _
            [$DRIVE_BUS_TYPE_ISCSI, 'ISCSI'], _
[$DRIVE_BUS_TYPE_ISCSI, 'ISCSI'], _
            [$DRIVE_BUS_TYPE_SAS, 'SAS'], _
[$DRIVE_BUS_TYPE_SAS, 'SAS'], _
            [$DRIVE_BUS_TYPE_SATA, 'SATA'], _
[$DRIVE_BUS_TYPE_SATA, 'SATA'], _
            [$DRIVE_BUS_TYPE_SD, 'SD'], _
[$DRIVE_BUS_TYPE_SD, 'SD'], _
            [$DRIVE_BUS_TYPE_MMC, 'MMC']]
[$DRIVE_BUS_TYPE_MMC, 'MMC']]
    Local $iDriveType = _WinAPI_GetDriveBusType($sDrive)
Local $iDriveType = _WinAPI_GetDriveBusType($sDrive)
    If @error Then
If @error Then
        Return $aArray[0][1]
Return $aArray[0][1]
    EndIf
EndIf
    Return $aArray[$iDriveType][1]
Return $aArray[$iDriveType][1]
EndFunc  ;==>_GetDriveBusType
EndFunc  ;==>_GetDriveBusType
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetUnusedDrives() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
== _GetUnusedDrives ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 87: Line 141:


Func _GetUnusedDrives()
Func _GetUnusedDrives()
Local $aReturn[1] = [0], $sReturn = ''
Local $sReturn = 'B:|C:|D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:|'
 
For $i = 1 To 26
$sReturn &= Chr($i + 64) & ':|'
Next
 
Local $aArray = DriveGetDrive('ALL')
Local $aArray = DriveGetDrive('ALL')
If @error Then
If @error Then
$aArray = $aReturn
Local $aError = [0]
$aArray = $aError
$aError = 0
EndIf
EndIf


For $i = 1 To $aArray[0]
For $i = 1 To $aArray[0]
$sReturn = StringReplace($sReturn, StringLeft($aArray[$i], 1) & ':|', '')
$sReturn = StringReplace($sReturn, $aArray[$i] & '|', '')
Next
Next
Return StringSplit(StringTrimRight($sReturn, 1), '|')
Return StringSplit(StringTrimRight($sReturn, StringLen('|')), '|')
EndFunc  ;==>_GetUnusedDrives
EndFunc  ;==>_GetUnusedDrives
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsSubst() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top|Return To Contents]]
 
== _IsSubst ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
| Desc=Checks if a drive is a subst drive.
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 118: Line 177:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">'''  _IsTrueCrypt() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top|Return To Contents]]
 
== _IsTrueCrypt ==
 
{{Snippet Header
| AuthorURL = 35302-guinness
| AuthorName = guinness
| Desc=Check if a drive is a TrueCrypt drive.
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 127: Line 194:
; Check if a drive is a TrueCrypt drive.
; Check if a drive is a TrueCrypt drive.
Func _IsTrueCrypt($sDrive)
Func _IsTrueCrypt($sDrive)
     Return StringInStr(_WinAPI_QueryDosDevice(StringLeft($sDrive, 1) & ':'), 'TrueCrypt', 2) > 0
     Return StringInStr(_WinAPI_QueryDosDevice(StringLeft($sDrive, StringLen('A')) & ':'), 'TrueCrypt') > 0
EndFunc  ;==>_IsTrueCrypt
EndFunc  ;==>_IsTrueCrypt
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' Toggle Monitor On/Off ~ Author - [http://www.autoitscript.com/forum/user/7688-greenmachine/ greenmachine] '''</blockquote> =====
[[#top|Return To Contents]]
 
== _MonitorToggle ==
 
{{Snippet Header
| AuthorURL = 7688-greenmachine
| AuthorName = greenmachine
| Desc = Toggle Monitor On/Off.
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Line 152: Line 227:
</syntaxhighlight>
</syntaxhighlight>


=====<blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _GetTotalScreenResolution ~ Author - [http://www.autoitscript.com/forum/user/28010-larrydalooza/ LarryDalooza]'''</blockquote> =====
[[#top|Return To Contents]]
 
== _GetTotalScreenResolution ==


<syntaxhighlight lang="autoit"> Global Const $aTSR = _GetTotalScreenResolution()
{{Snippet Header
| AuthorURL = 28010-larrydalooza
| AuthorName = LarryDalooza
| ModifierURL = 16404-brettf
| ModifierName = BrettF
}}
 
<syntaxhighlight lang="autoit">
Global Const $aTSR = _GetTotalScreenResolution()


  MsgBox(0, "Total Screen Resolution", "Width = " & $aTSR[0] & @TAB & "Height = " & $aTSR[1])
  MsgBox(0, "Total Screen Resolution", "Width = " & $aTSR[0] & @TAB & "Height = " & $aTSR[1])


; Original code by Larry. Edited by BrettF
  Func _GetTotalScreenResolution()
  Func _GetTotalScreenResolution()
     Local Const $SM_VIRTUALWIDTH = 78
     Local Const $SM_VIRTUALWIDTH = 78
Line 169: Line 253:


     Return $aRet
     Return $aRet
  EndFunc </syntaxhighlight>
  EndFunc
</syntaxhighlight>
 
[[#top|Return To Contents]]

Latest revision as of 19:47, 12 May 2014


Please always credit an author in your script if you use their code. It is only polite.


_ComputerNameAndModel

Author: guinness








; Can return nothing relevant if machine is not a factory build
Global $aArray = _ComputerNameAndModel() ; Returns an Array with 2 indexes.
MsgBox(64, "_ComputerNameAndModel()", 'The Product is a "' & $aArray[0] & '" and the Serial Number is "' & $aArray[1] & '".')

Func _ComputerNameAndModel()
    Local $aReturn[2] = ["(Unknown)", "(Unknown)"], $oColItems, $oWMIService

    $oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
    $oColItems = $oWMIService.ExecQuery("Select * From Win32_ComputerSystemProduct", "WQL", 0x30)
    If IsObj($oColItems) Then
        For $oObjectItem In $oColItems
            $aReturn[0] = $oObjectItem.Name
            $aReturn[1] = $oObjectItem.IdentifyingNumber
        Next
        Return $aReturn
    EndIf
    Return SetError(1, 0, $aReturn)
EndFunc   ;==>_ComputerNameAndModel

Return To Contents

_GetComputerModel

Author: guinness








; Can return nothing relevant if machine is not a factory build
ConsoleWrite(_GetComputerModel() & @CRLF)

Func _GetComputerModel()
    Local $oWMIService = ObjGet("winmgmts:\\.\")

    Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_ComputerSystem", "WQL", 0x30)
	Local $sDescription

    If IsObj($oColItems) Then
        For $oObjectItem In $oColItems
            $sDescription &= $oObjectItem.Model
        Next

        Return $sDescription
    EndIf

    Return SetError(1, 1, 0)
EndFunc   ;==>_GetComputerModel

Return To Contents

_GetComputerModel_2

Author: rindeal







Faster version of the snippet above


; Can return nothing relevant if machine is not a factory build
ConsoleWrite(_GetComputerModel() & @CRLF)

Func _GetComputerModel()
	Return RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS", "SystemProductName")
EndFunc   ;==>_GetComputerModel

Return To Contents

_GetDriveBusType

Author: guinness








#include <APIConstants.au3>
#include <WinAPIEx.au3>

Local $aDrive = DriveGetDrive('ALL')
For $i = 1 To $aDrive[0]
	ConsoleWrite(StringUpper($aDrive[$i]) & ' => ' & _GetDriveBusType($aDrive[$i]) & @CRLF)
Next

; Get a string representation of a drive's bus type.
Func _GetDriveBusType($sDrive)
	Local $aArray[14][2] = [[$DRIVE_BUS_TYPE_UNKNOWN, 'UNKNOWN'], _
			[$DRIVE_BUS_TYPE_SCSI, 'SCSI'], _
			[$DRIVE_BUS_TYPE_ATAPI, 'ATAPI'], _
			[$DRIVE_BUS_TYPE_ATA, 'ATA'], _
			[$DRIVE_BUS_TYPE_1394, '1394'], _
			[$DRIVE_BUS_TYPE_SSA, 'SSA'], _
			[$DRIVE_BUS_TYPE_FIBRE, 'FIBRE'], _
			[$DRIVE_BUS_TYPE_USB, 'USB'], _
			[$DRIVE_BUS_TYPE_RAID, 'RAID'], _
			[$DRIVE_BUS_TYPE_ISCSI, 'ISCSI'], _
			[$DRIVE_BUS_TYPE_SAS, 'SAS'], _
			[$DRIVE_BUS_TYPE_SATA, 'SATA'], _
			[$DRIVE_BUS_TYPE_SD, 'SD'], _
			[$DRIVE_BUS_TYPE_MMC, 'MMC']]
	Local $iDriveType = _WinAPI_GetDriveBusType($sDrive)
	If @error Then
		Return $aArray[0][1]
	EndIf
	Return $aArray[$iDriveType][1]
EndFunc   ;==>_GetDriveBusType

Return To Contents

_GetUnusedDrives

Author: guinness








#include <Array.au3>

;~ Retrieve unused drive letters.
Local $aDrives = _GetUnusedDrives()
_ArrayDisplay($aDrives)

Func _GetUnusedDrives()
	Local $sReturn = 'B:|C:|D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:|'
	Local $aArray = DriveGetDrive('ALL')
	If @error Then
		Local $aError = [0]
		$aArray = $aError
		$aError = 0
	EndIf

	For $i = 1 To $aArray[0]
		$sReturn = StringReplace($sReturn, $aArray[$i] & '|', '')
	Next
	Return StringSplit(StringTrimRight($sReturn, StringLen('|')), '|')
EndFunc   ;==>_GetUnusedDrives

Return To Contents

_IsSubst

Author: guinness







Checks if a drive is a subst drive.


#include <WinAPIEx.au3>

ConsoleWrite(_IsSubst(@HomeDrive) & @CRLF)

; Checks if a drive is a subst drive.
Func _IsSubst($sDrive)
    Return _WinAPI_PathIsDirectory(StringReplace(_WinAPI_QueryDosDevice(StringLeft($sDrive, 1) & ":"), "\??\", ""))
EndFunc   ;==>_IsSubst

Return To Contents

_IsTrueCrypt

Author: guinness







Check if a drive is a TrueCrypt drive.


#include <WinAPIEx.au3>

ConsoleWrite("Is " & @HomeDrive & "\ a TrueCrypt drive?: " & _IsTrueCrypt(@HomePath) & @CRLF)

; Check if a drive is a TrueCrypt drive.
Func _IsTrueCrypt($sDrive)
    Return StringInStr(_WinAPI_QueryDosDevice(StringLeft($sDrive, StringLen('A')) & ':'), 'TrueCrypt') > 0
EndFunc   ;==>_IsTrueCrypt

Return To Contents

_MonitorToggle

Author: greenmachine







Toggle Monitor On/Off.


#include <SendMessage.au3>
#include <WindowsConstants.au3>

_MonitorToggle(1)
Sleep(1000)
_MonitorToggle(0)

; Toggle Monitor On/Off.
Func _MonitorToggle($iTurnOff = 1)
	Local $hWnd = WinGetHandle("[CLASS:Progman]"), $SC_MONITORPOWER = 61808
	If $iTurnOff Then
		Return _SendMessage($hWnd, $WM_SYSCOMMAND, $SC_MONITORPOWER, 2)
	Else
		Return _SendMessage($hWnd, $WM_SYSCOMMAND, $SC_MONITORPOWER, -1)
	EndIf
EndFunc   ;==>_MonitorToggle

Return To Contents

_GetTotalScreenResolution

Author: LarryDalooza




Modified: BrettF





 Global Const $aTSR = _GetTotalScreenResolution()

 MsgBox(0, "Total Screen Resolution", "Width = " & $aTSR[0] & @TAB & "Height = " & $aTSR[1])

 Func _GetTotalScreenResolution()
     Local Const $SM_VIRTUALWIDTH = 78
     Local Const $VirtualDesktopWidth = DllCall("user32.dll", "int", "GetSystemMetrics", "int", $SM_VIRTUALWIDTH)

     Local Const $SM_VIRTUALHEIGHT = 79
     Local Const $VirtualDesktopHeight = DllCall("user32.dll", "int", "GetSystemMetrics", "int", $SM_VIRTUALHEIGHT)

     Local Const $aRet[2] = [$VirtualDesktopWidth[0], $VirtualDesktopHeight[0]]

     Return $aRet
 EndFunc

Return To Contents