Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now when the window loads, we can make the window topmost this way:

Private Sub Form_Load()

    retVal = SetWindowPos(Me.hwnd, HWND_TOPMOST, 100, 100, _
        100, 100, SWP_DRAWFRAME Or SWP_SHOWWINDOW)

End Sub

That’s all it takes. Now the window will stay on top of other windows, as you see in Figure 23.11. Our topmost program is a success.


Figure 23.11  Making a window topmost.

Determining Free And Total Disk Space

The Testing Department is calling again. Your program, SuperDuperDataCrunch, is overflowing users’ disks. Can’t you check how much disk space is available before writing out your 800MB database? Hmm, you think, how would that work?

You can use the GetDiskFreeSpace function to check how much space there is on a disk:

Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
    GetDiskFreeSpaceA (ByVal     lpRootPathName As String, _
    lpSectorsPerCluster As Long,   lpBytesPerSector As Long, _
    lpNumberOfFreeClusters As Long,       lpTotalNumberOfClusters As Long) _
    As Long

Here’s what this function’s arguments mean:

  lpRootPathName—The disk you want to check (for example, “c:\”)
  lpSectorsPerCluster—The number of sectors per cluster on the disk
  lpBytesPerSector—The number of bytes per sector
  lpNumberOfFreeClusters—The number of free clusters
  lpTotalNumberOfClusters—The total number of clusters

To find the total free space, you multiply SectorsPerCluster * BytesPerSector * NumberOfFreeClusters. To find the total space on the drive, you multiply SectorsPerCluster * BytesPerSector * TotalNumberOfClusters.

Let’s see an example. Here, we’ll determine the free space on the c: drive. We start by declaring GetDiskFreeSpace:

Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
    "GetDiskFreeSpaceA" (ByVal lpRootPathName As String,_
    lngSectorsPerCluster As Long, lngBytesPerSector As Long, _
    lngNumberOfFreeClusters As Long, lngTotalNumberOfClusters As Long)_
    As Long

Next, when the form loads, we fill the variables lngSectorsPerCluster, lngBytesPerSector, lngNumberOfFreeClusters, and lngTotalNumberOfClusters with GetDiskFeeSpace for drive C:

Private Sub Form_Load()
    Dim lngSectorsPerCluster As Long
    Dim lngBytesPerSector As Long
    Dim lngNumberOfFreeClusters As Long
    Dim lngTotalNumberOfClusters As Long

    retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
    lngBytesPerSector, lngNumberOfFreeClusters, lngTotalNumberOfClusters)
...

Finally, we can display the total number of free bytes in a label, Label1, this way:

Private Sub Form_Load()
    Dim lngSectorsPerCluster As Long
    Dim lngBytesPerSector As Long
    Dim lngNumberOfFreeClusters As Long
    Dim lngTotalNumberOfClusters As Long

    retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
        lngBytesPerSector, lngNumberOfFreeClusters, _
        lngTotalNumberOfClusters)

    Label1.Caption = "Free space on drive c: " & _
        Str(lngSectorsPerCluster * lngBytesPerSector * _
        lngNumberOfFreeClusters) & " bytes"

End Sub

When you run the program, you’ll see the number of free bytes on the computer’s C: drive, something like the display in Figure 23.12.


Figure 23.12  Determining free disk space.

Determining The Windows Directory

Sometimes you want to determine where Windows is installed on a system to be able to install files such as initialization files or DLLs. You can use the GetWindowsDirectory function to find the Windows directory:

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
    "GetWindowsDirectoryA" (ByVal      lpBuffer As String, ByVal  nSize As _
    Long) As Long

Here are the arguments for the GetWindowsDirectory function:

  lpBuffer—Pointer to a buffer to store the Windows directory
  nSize—Size of the buffer in bytes.

Let’s see an example. Here, we’ll add some code to the example we started in the previous topic that displays free disk space. We add the declaration we’ll need for GetWindowsDirectory:

Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
    "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _
    lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
    lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long)
    As Long

 Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
    "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As _
    Long) As Long

Next, we set up a text buffer, get the Windows directory with GetWindowsDirectory, and display the Windows directory in a new label, Label2:

Private Sub Form_Load()
    Dim lngSectorsPerCluster As Long
    Dim lngBytesPerSector As Long
    Dim lngNumberOfFreeClusters As Long
    Dim lngTotalNumberOfClusters As Long
    Dim strBuffer As String * 1024

    retVal = GetDiskFreeSpace("c:\", lngSectorsPerCluster, _
    lngBytesPerSector, lngNumberOfFreeClusters,     lngTotalNumberOfClusters)

    Label1.Caption = "Free space on drive c: " & Str(lngSectorsPerCluster_
        * lngBytesPerSector * lngNumberOfFreeClusters) & "bytes"

    retVal = GetWindowsDirectory(strBuffer, 1024)
    Label2.Caption = "Windows directory: " & strBuffer

End Sub

That’s it—the result appears in Figure 23.13. As you can see, we’re now able to find the directory in which Windows itself is installed.


Figure 23.13  Determining the Windows directory.

The code for this example is located in the windisk folder on this book’s accompanying CD-ROM.


TIP:  If you just want to find the current, default directory, use the Visual Basic command CurDir. To set the current drive, use ChDrive, and to change directories, use ChDir.

Connecting To Visual C++

The Testing Department is calling again. Another company programmer has written a great routine for alphabetizing spreadsheets that would be great in your program, SuperDuperDataCrunch. Terrific, you say. They say, the only problem is that those routines are written in Visual C++, and you’ll have to interface them to Visual Basic. Visual C++. You say, what’s that?


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.