PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[VB6] Shell Drive Free Space-VBForums
Results 1 to 8 of 8

Thread: [VB6] Shell Drive Free Space

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,655

    Post [VB6] Shell Drive Free Space

    If you are using Shell objects there are lots of properties you can retrieve describing the filesystem and individual files. However the values returned are often types that VB is not prepared to cope with without some help.

    Here we'll look at one of the Volume properties that gives you the capacity and free space for each drive in ssfDrives.


    This program fetches the Drives Folder (ssfDrives) and retrieves the PKEY_Computer_DecoratedFreeSpace (as SCID_Computer_DecoratedFreeSpace).

    The returned value is a PROPVARIANT, so we first convert it to a Variant for use in VB6.

    The PROPVARIANT is empty or a "VT_VECTOR | VT_UI8" value, so if not empty we recast it as "VT_ARRAY | VT_CURRENCY" which is something we can play with in VB6.

    Update:

    It seems that VB6 can coerce these particular PROPVARIANTs to Variants itself. This saves an API call that isn't available in most pre-Vista Windows installations.

    Then we format these values (1.) using normal techniques after scaling by 10000 to convert from our pseudo-Currency values to whole numbers, or (2.) by calling StrFormatByteSize() which produces abbreviated formats in KB, MB, GB, TB units.


    You could probably also use PKEY_FreeSpace and PKEY_Capacity in separate calls of the ExtendedProperty() method, but I haven't tested that.


    See the program comments: this code won't work before Vista except on XP SP2 or later with Windows Desktop Search (WDS) 3.0 or later installed. Those old systems do not have propsys.dll in them.

    Update:

    No longer a restriction.

    Name:  sshot.jpg
Views: 632
Size:  33.5 KB
    Attached Files Attached Files
    Last edited by dilettante; Apr 21st, 2014 at 02:47 PM. Reason: update, reposted attachment

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,655

    Re: [VB6] Shell Drive Free Space

    Warning:

    If you compile this on Vista or Win7 (and probably Win8.x.y) the EXE will not run on XP, and most likely vice versa as well.

    Microsoft broke binary compatibility on Shell32 automation classes between XP and Vista, so you'd need to rewrite the code to use late binding.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    Re: [VB6] Shell Drive Free Space

    You know, you could save yourself another API call by formatting the bytes to the suffixed form yourself:-
    vb Code:
    1. '
    2. Private Function FormatSizeWithSuffix(ByVal size As Currency) As String
    3.  
    4.     Dim i As Long
    5.     Dim denoms(0 To 4) As String
    6.    
    7.     size = size * 10000@
    8.    
    9.     denoms(0) = "Bytes"
    10.     denoms(1) = "KB"
    11.     denoms(2) = "MB"
    12.     denoms(3) = "GB"
    13.     denoms(4) = "TB"
    14.  
    15.     For i = UBound(denoms) To 0 Step -1
    16.         Dim div As Currency
    17.        
    18.         div = 1024 ^ i
    19.        
    20.         If size > div Then
    21.             Dim val As Double
    22.             val = size / div
    23.  
    24.             FormatSizeWithSuffix = Format(val, "0.##") + " " + denoms(i)
    25.             Exit Function
    26.         End If
    27.  
    28.     Next
    29.        
    30. End Function

    You can alter your code slightly to use like this:-

    vb Code:
    1. '
    2. Private Function FormatByteSize(ByVal size As Currency) As String
    3.     'Size is really a UI8 here:
    4.    
    5.     FormatByteSize = FormatSizeWithSuffix(size)
    6. End Function

    Not that your code is wrong or anything but I've always felt its better to avoid APIs if you can find a native implementation. That's just my personal opinion. Feel free to disregard.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    Re: [VB6] Shell Drive Free Space

    Forgot to mention, I never knew that Shell32.dll was also a COM server. I knew shell was COM based but never figured simply to try referencing Shell32.dll. Very interesting.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,655

    Re: [VB6] Shell Drive Free Space

    True enough, that works fine. The only caveat is that when your disk drive is so big it uses bit 63 you'll have a problem.

    People can always use alternatives, and the sample you gave may help them understand how to do other similar things. I was just trying to duplicate how Explorer does things: it really reports values in kibibytes, mebibytes, etc.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,655

    Re: [VB6] Shell Drive Free Space

    Quote Originally Posted by Niya View Post
    Forgot to mention, I never knew that Shell32.dll was also a COM server. I knew shell was COM based but never figured simply to try referencing Shell32.dll. Very interesting.
    Yeah, it's only been able to do this since 1997 or so. It's covered in the MSDN Library docs that came with VB6.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    Re: [VB6] Shell Drive Free Space

    Quote Originally Posted by dilettante View Post
    True enough, that works fine. The only caveat is that when your disk drive is so big it uses bit 63 you'll have a problem.
    Yeah. I've always felt that the lack of unsigned types was a serious limitation of nearly all incarnations of BASIC. Still, one could probably do some kind of funky bit shifting and a little math to correct this potential problem.

    Quote Originally Posted by dilettante View Post
    I was just trying to duplicate how Explorer does things: it really reports values in kibibytes, mebibytes, etc.
    According to that article, Kibibytes and its ilk are based on powers of 1024 whereas Kilobytes and such are based on 1000. I used 1024 in my implementation to match the output of the Windows shell.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,655

    Re: [VB6] Shell Drive Free Space

    Just tried your version and I saw that. Sorry to be so dull today.

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
  •  



Featured


Click Here to Expand Forum to Full Width