How to retrieve hard drive temperature?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to retrieve hard drive temperature?

  1. #1
    Join Date
    Nov 2005
    Posts
    10

    Question How to retrieve hard drive temperature?

    I am looking for a way to retrieve the temperature of hard drives in VB Express 2008. I have found posts at a few different places that accomplish this with the following:


    Code:
    Public Function GetDriveTemp() As Integer
    Try
    Dim searcher As New ManagementObjectSearcher("root\WMI", "SELECT * FROM MSStorageDriver_ATAPISmartData")
    For Each queryObj As ManagementObject In searcher.Get()
    Dim arrVendorSpecific As Byte() = queryObj("VendorSpecific")
    GetDriveTemp = arrVendorSpecific(115
    Next
    Catch ex As ManagementException
    MessageBox.Show(ex.Message)
    End Try
    End Function
    This function retrieves the temperature for all the hard drives in my system (excluding USB), two internal and one external esata drive. There are two problems, tough:

    1. I want to know the drive letter and maybe the drive label that the retrieved temperature corresponds to. I'm assuming that the * in the query, "SELECT * FROM MSStorageDriver_ATAPISmartData", can be changed to specify a particular drive rather than all drives, but I don't know how retrieve a list of all the drives in my system or how to write the query based on those results.

    2. If I call the funciton from a some click event like for a label or button, there are no problems and the return value seems valid. But when I call the function from a Timer_Tick event, the mouse cursor is the busy cursor as long as the mouse is within the form, the form doesn't receive any click events, and it cannot be closed.

    I know what I'm trying to do can be done because there are a few utilities that provide this information. So, does anyone have any ideas?
    Last edited by HanneSThEGreaT; February 2nd, 2010 at 09:19 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: How to retrieve hard drive temperature?

    Check the drive MFG for their MOF file that you can use, or else, check this out:

    http://www.hddtemp.com/
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,099

    Re: How to retrieve hard drive temperature?

    pescadore, please use [CODE] [/CODE] tags when posting code, as stated here :

    http://www.codeguru.com/forum/showthread.php?t=403073

    Thanx

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to retrieve hard drive temperature?

    Not sure exactly, but this seems to say something about my hd temp:
    Code:
    Imports System
    Imports System.Management
    
    Public Class Temperature
        Public Function GetDriveTemp() As String
            Dim retval As String = ""
            Try
                Dim searcher As New ManagementObjectSearcher("root\WMI", "Select * from MSAcpi_ThermalZoneTemperature")
                Dim Counter As Integer = 1
                For Each queryObj As ManagementObject In searcher.Get()
    
                    retval &= "DRIVE: " & Counter & "=" & GetCelsius(queryObj.GetPropertyValue("CurrentTemperature")).ToString("00.00")
                    Counter += 1
                Next
            Catch ex As ManagementException
                MessageBox.Show(ex.Message)
            End Try
            Return retval
        End Function
        Private Function GetCelsius(ByVal fromKelvin As Single) As Single
    
            Return (fromKelvin - 2732) / 10
    
        End Function
    End Class
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,099

    Re: How to retrieve hard drive temperature?

    Funky!!! Good One!

    Just a side not, in order to make use of the ManagementObject and the ManagementObjectSearcher you need to set a reference to System.Management, by clicking on Project, ProjectName proeprties, scroll for System.Management, OK.


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to retrieve hard drive temperature?

    ...might be I got cpu temp, instead of hd temp...
    I made a second function (and changed the name of the first)
    they retrieve slightly different values...
    Code:
    Imports System
    Imports System.Management
    
    Public Class Temperature
        Public Function GetCpuTemp() As String
            Dim retval As String = ""
            Try
                Dim searcher As New ManagementObjectSearcher("root\WMI", "Select * from MSAcpi_ThermalZoneTemperature")
                Dim Counter As Integer = 1
                For Each queryObj As ManagementObject In searcher.Get()
    
                    retval &= "Cpu: " & Counter & "=" & GetCelsius(queryObj.GetPropertyValue("CurrentTemperature")).ToString("00.00")
                    Counter += 1
                Next
            Catch ex As ManagementException
                MessageBox.Show(ex.Message)
            End Try
            Return retval
        End Function
        Private Function GetCelsius(ByVal fromKelvin As Single) As Single
    
            Return (fromKelvin - 2732) / 10
    
        End Function
        Public Function GetDriveTemp() As String
            Dim retval As String = "Temp: "
            Try
                Dim counter As Integer = 1
                Dim searcher As New ManagementObjectSearcher( _
                "root\WMI", _
                "SELECT * FROM MSStorageDriver_ATAPISmartData")
                For Each queryObj As ManagementObject In searcher.Get()
                    Dim arrVendorSpecific As Byte() = queryObj("VendorSpecific")
                    retval &= "DRIVE: " & Counter & "=" & arrVendorSpecific(115)
                    counter += 1
                Next
    
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
            Return retval
    
        End Function
    End Class
    Last edited by Cimperiali; February 23rd, 2010 at 10:36 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to retrieve hard drive temperature?

    still not able to get the name of the Hd as you would recognize it.
    The closest thing I can get is the name of the hardware:
    Code:
                    retval &= "DRIVE: " & queryObj.GetPropertyValue("InstanceName") & "=" & arrVendorSpecific(115)
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to retrieve hard drive temperature?

    I think I got it...a sort of: on my pc I get the drive temp and letter associated with instanceName, but had to trick a bit (had to remove 2 bytes from instancename, and do not know if it might be valid for any disk)

    Give it a try, and tell us if you find or work out a better way.

    Code:
    Public Function GetDriveTemp() As String
            Dim retval As String = "Temp: "
            Try
                'Dim counter As Integer = 1
                Dim searcher As New ManagementObjectSearcher( _
                "root\WMI", _
                "SELECT * FROM MSStorageDriver_ATAPISmartData")
                For Each queryObj As ManagementObject In searcher.Get()
                    Debug.Print(queryObj.GetText(TextFormat.WmiDtd20))
                    Dim arrVendorSpecific As Byte() = queryObj("VendorSpecific")
                    'retval &= "DRIVE: " & queryObj.GetPropertyValue("InstanceName") & "=" & arrVendorSpecific(115)
                    'counter += 1
                    retval &= "DRIVE: " & instanceNameToDriveLetter(queryObj.GetPropertyValue("InstanceName")) & "=" & arrVendorSpecific(115)
                Next
    
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
            Return retval
    
        End Function
        Private Function instanceNameToDriveLetter(ByVal Instancename As String) As String
            Dim sb As New System.Text.StringBuilder()
    
            ' on my os and with my disk, the instancename has 2 extra chars ("_0")
            Instancename = Instancename.Substring(0, Instancename.Length - 2)
            '----------
            Dim DiskDriveSearcher As New ManagementObjectSearcher("\root\cimv2", _
               "Select PNPDeviceID, DeviceID from Win32_DiskDrive where PNPDeviceID = '" & Instancename.Replace("\", "\\") & "'")
    
            For Each queryObj As ManagementObject In DiskDriveSearcher.Get()
                Debug.Print(Instancename)
                Debug.Print(queryObj.GetPropertyValue("PNPDeviceID"))
                Debug.Print(queryObj.GetPropertyValue("DeviceID"))
                Debug.Print("-----------")
                Dim PartitionSearcher As New ManagementObjectSearcher("\root\cimv2", _
                "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & queryObj.GetPropertyValue("DeviceID") & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
                For Each subQObj As ManagementObject In PartitionSearcher.Get()
                    Debug.Print(subQObj.GetPropertyValue("DeviceID"))
                    'Debug.Print(subQObj.GetText(TextFormat.WmiDtd20))
    
                    Dim LogicalPSearcher As New ManagementObjectSearcher _
                                        ("\root\cimv2", _
                   "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
                 & subQObj.GetPropertyValue("DeviceID") & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
    
                    For Each subQLogical In LogicalPSearcher.Get()
                        sb.Append(subQLogical.GetPropertyValue("DeviceID"))
                        Debug.Print(sb.ToString)
                    Next
    
                Next
            Next
    
            Return sb.ToString
        End Function
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Feb 2014
    Posts
    1

    Re: How to retrieve hard drive temperature?

    Hello everybody, I'm a system administrator and usually I use batch script to get information about system.
    After that I make targeted statistics using RRDTool (round-robin database tool)

    I'm looking for a piece of code in VBScript to get with WMI method only HD Temperature

    I use this script that is quite similar to first example

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
    Set colItems = objWMIService.ExecQuery( "SELECT * FROM MSStorageDriver_ATAPISmartData",,48)
    For Each objItem in colItems
    If isNull(objItem.VendorSpecific) Then
    Wscript.Echo "VendorSpecific: "
    Else
    WScript.Echo "VendorSpecific: " & Join(objItem.VendorSpecific, ",")
    End If
    Next
    but the return is:

    VendorSpecific: 16,0,1,11,0,100,100,0,0,0,0,0,0,0,2,5,0,100,100,0,0,0,0,0,0,0,3,7,0,172,172,1,0,0,0,13,0,0,4,18,0,100,100,27,1,0,0,0,0,0,5,5
    1,0,100,100,0,0,0,0,0,0,0,7,11,0,100,100,0,0,0,0,0,0,0,8,5,0,100,100,0,0,0,0,0,0,0,9,18,0,85,85,69,27,0,0,0,0,0,10,19,0,100,100,0,0,0,0,0,0,
    0,12,50,0,100,100,144,0,0,0,0,0,0,191,10,0,100,100,0,0,0,0,0,0,0,192,50,0,100,100,68,0,0,0,0,0,0,193,18,0,97,97,81,149,0,0,0,0,0,194,2,0,127
    ,127,47,0,18,0,52,0,0,196,50,0,100,100,0,0,0,0,0,0,0,197,34,0,100,100,0,0,0,0,0,0,0,198,8,0,100,100,0,0,0,0,0,0,0,199,10,0,200,200,0,0,0,0,0
    ,0,0,223,10,0,100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0
    I want get only value 47 placed at position 165

    Thanks in advance and regards

    Dario

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center