Results 1 to 7 of 7

Thread: Weird System Info!

Threaded View

  1. #1

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Question Weird System Info!

    I am using the following code to get the system information of the user's machine. A ImageCombo lists all the hard drives. Since I want only the hard drives, I am using the ImageCombo & not a DriveListBox (NOTE: This isn't my code):
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias..........
    2. Private Declare Function GetVersionEx Lib "kernel32.dll" Alias..........
    3. Private Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias..........
    4. Private Declare Sub CopyMemory Lib "kernel32.dll" Alias..........
    5. Private Type ULARGE_INTEGER
    6.     LowPart As Long
    7.     HighPart As Long
    8. End Type
    9. Private Type OSVERSIONINFO
    10.     dwOSVersionInfoSize As Long
    11.     dwMajorVersion As Long
    12.     dwMinorVersion As Long
    13.     dwBuildNumber As Long
    14.     dwPlatformId As Long
    15.     szCSDVersion As String * 128
    16. End Type
    17.  
    18. Dim OsInfo As OSVERSIONINFO
    19. Dim tmp As String
    20. Dim FreeUser As ULARGE_INTEGER
    21. Dim Total As ULARGE_INTEGER
    22. Dim FreeSys As ULARGE_INTEGER
    23. Dim Temp As Currency
    24. Dim fTemp As Currency
    25. Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias.....
    26. Private Declare Function GetDriveType Lib "kernel32" Alias.....
    27. Private Function [b]GetDriveTypeFromLetter(strDriveLetter As String) As String[/b]
    28.     Dim strDrive As String
    29.     If Right(strDriveLetter, 1) <> "\" Then
    30.         strDrive = strDriveLetter & "\"
    31.     Else
    32.         strDrive = strDriveLetter
    33.     End If
    34.    
    35.     Select Case GetDriveType(strDrive)
    36.         Case 2
    37.             GetDriveTypeFromLetter = "Removable"
    38.         Case 3
    39.             GetDriveTypeFromLetter = "Fixed Hard Drive"
    40.         Case Is = 4
    41.             GetDriveTypeFromLetter = "Remote"
    42.         Case Is = 5
    43.             GetDriveTypeFromLetter = "CD-ROM"
    44.         Case Is = 6
    45.             GetDriveTypeFromLetter = "Ram disk"
    46.         Case Else
    47.             GetDriveTypeFromLetter = "Unrecognized"
    48.     End Select
    49. End Function
    50. Private Sub [b]Form_Load()[/b]
    51.     Dim strSave As String, i As Long, ret&
    52.     Me.AutoRedraw = True
    53.     strSave = String(255, Chr$(0))
    54.     ret& = GetLogicalDriveStrings(255, strSave)
    55.     For i = 1 To 100
    56.         If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
    57.         If GetDriveTypeFromLetter(Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)) = "Fixed Hard Drive" Then
    58.             imgCboDrives.ComboItems.Add , , Left$(strSave, InStr(1, strSave, Chr$(0)) - 1), 1
    59.         End If
    60.         strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
    61.     Next i
    62.     Call GetDrive("C:\")
    63.     imgCboDrives.ComboItems(1).Selected = True
    64. End Sub
    65. Function [b]GetDrive(DriveName)[/b]
    66.     On Error Resume Next
    67.     Dim retVal
    68.     lblSysInfo.Height = 985
    69.     OsInfo.dwOSVersionInfoSize = Len(OsInfo)
    70.     retVal = GetVersionEx(OsInfo)
    71.    
    72.     Select Case OsInfo.dwPlatformId
    73.         Case 0
    74.             tmp = "Windows 3.x"
    75.         Case 1
    76.             tmp = "Windows 95/98"
    77.         Case 2
    78.             tmp = "Windows 2000"
    79.     End Select
    80.     lblSysInfo.Caption = "Drive: " & Left(DriveName, 2) & vbNewLine
    81.     lblSysInfo.Caption = lblSysInfo.Caption & "Version: " & OsInfo.dwMajorVersion & "." & OsInfo.dwMinorVersion & vbNewLine
    82.     lblSysInfo.Caption = lblSysInfo.Caption & "Platform: " & tmp & vbNewLine
    83.  
    84.     GetDiskFreeSpaceEx Left(DriveName, 2), FreeUser, Total, FreeSys
    85.     CopyMemory Temp, Total, 8
    86.     CopyMemory fTemp, FreeUser, 8
    87.     lblSysInfo.Caption = lblSysInfo.Caption & "Total Space: " & Format$(CCur(Temp) * 10000, "#######,##") & " bytes" & vbNewLine
    88.     lblSysInfo.Caption = lblSysInfo.Caption & "Free Space: " & Format$(CCur(fTemp) * 10000, "#######,##") & " bytes" & vbNewLine
    89.     lblSysInfo.Caption = lblSysInfo.Caption & "Used Space: " & Format$(CCur(Temp) - CCur(fTemp) * 10000, "#######,##") & " bytes" & vbNewLine
    90. End Function
    91. Private Sub [b]imgCboDrives_Click()[/b]
    92.     Call GetDrive(imgCboDrives.Text)
    93. End Sub
    The above correctly computes the total space & the free space of the drives. Now it's pretty obvious that since I have the total space & the free space, the used space will be (Total Space - Free Space) but strangely, that gives the wrong result & that too a negative value! For e.g. in my machine, the total space of C:\ computed comes to 10,000,343,040 bytes & the free space comes to 934,223,872 bytes; so the used space should be 9,066,119,168 bytes but VB computes it to -933,223,838 bytes! I have checked the other drives as well - the outcome is always a wrong negative value.

    Why this huge discrepancy & wrong result in the used space?
    Last edited by arpan_de; Jan 2nd, 2006 at 01:27 PM.


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

Posting Permissions

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



Click Here to Expand Forum to Full Width