Results 1 to 7 of 7

Thread: Weird System Info!

  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?

  2. #2
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Weird System Info!

    Off topic, but do you have to have an explanation point at the end of *Every* thread title?

  3. #3
    Lively Member
    Join Date
    Dec 2005
    Location
    In front of my pc
    Posts
    78

    Re: Weird System Info!

    I think it's got something to do with this piece of code:

    VB Code:
    1. Dim FreeUser As ULARGE_INTEGER
    2. Dim Total As ULARGE_INTEGER
    3. Dim FreeSys As ULARGE_INTEGER

    I've never seen this type of variable... Are you sure vb6 can handle it properly? Maybe you should try if it works when you change it into this:

    VB Code:
    1. Dim FreeUser As Double
    2. Dim Total As Double
    3. Dim FreeSys As Double

    I don't know if it works, but you should give it a try...

  4. #4

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

    Re: Weird System Info!

    Off topic, but do you have to have an explanation point at the end of *Every* thread title?
    "Explanation point" or "exclamation mark"??? The latter I suppose. Why, any problem with that?
    Dim FreeUser As Double
    Dim Total As Double
    Dim FreeSys As Double
    I tried your suggestion gtheman but it generates the ByRef argument type mismatch error pointing to
    VB Code:
    1. GetDiskFreeSpaceEx Left(DriveName, 2), FreeUser, Total, FreeSys
    Any other ideas?
    Last edited by arpan_de; Jan 2nd, 2006 at 01:28 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?

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Weird System Info!

    arpan,

    I would suggest that instead of using ULARGE_INTEGER you use Currency.
    VB Code:
    1. Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" ( _
    2.     ByVal lpDirectoryName As String, _
    3.     lpFreeBytesAvailable As Currency, _
    4.     lpTotalNumberOfBytes As Currency, _
    5.     lpTotalNumberOfFreeBytes As Currency _
    6. ) As Long

    Then you can get used space by simple subtraction
    VB Code:
    1. Dim FreeUser As Currency, Total As Currency, FreeSys As Currency
    2. Dim UsedBytes As Currency
    3. GetDiskFreeSpaceEx Left(DriveName, 2), FreeUser, Total, FreeSys
    4. UsedBytes = Total - FreeUser
    I also have a request. Don't truncate your code lines by ...
    I may want to try out your code, but you force me to enter the code myself when you do this.

  6. #6
    Lively Member
    Join Date
    Dec 2005
    Location
    In front of my pc
    Posts
    78

    Re: Weird System Info!

    I just checked your code and I found this in the declarations :
    VB Code:
    1. Private Type ULARGE_INTEGER
    2.     LowPart As Long
    3.     HighPart As Long
    4. End Type

    This means that every ULARGE_INTEGER (for example FreeUser), has a LowPart and a HighPart property (so in the case of FreeUser it would be FreeUser.LowPart and FreeUser.HighPart). Try messing with these values, maybe you should first substract the HighPart values, than the LowPart values, and then average them, but I don't know for sure and I can't try the code.

  7. #7

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

    Re: Weird System Info!

    I also have a request. Don't truncate your code lines by ...
    I may want to try out your code, but you force me to enter the code myself when you do this.
    Yeah.....I agree.....you are very much correct........I shouldn't be truncating the code lines.........will keep in mind henceforth.

    I will try out your suggestions & get back to you.
    Last edited by arpan_de; Jan 2nd, 2006 at 01:28 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