Windows API Reference
Google
Web www.mahipalreddy.com

Get the computer name useing the GetComputerName function in VB.

The following code example shows how to use the GetComputerName function to return the computer name


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Private Sub Command1_Click()
    Dim Comp_Name_B As String * 255
    Dim Comp_Name As String
    GetComputerName Comp_Name_B, Len(Comp_Name_B)
    'but the string is always ended with a null terminated string so..
    '..we can use the Chr(0) function to find the end and return only the computer name
    Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
    MsgBox Comp_Name
End Sub

Top

Get the currently logged on user name with VB code

The following code will return the currently logged on user name.


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
             (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub Command1_Click()
    Dim lpBuff As String * 25
    Dim ret As Long, UserName As String
    ret = GetUserName(lpBuff, 25)
    UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
    MsgBox UserName
End Sub

Top

Retrieve the current screen video resolution using VB code

The following code example returns the current screen video resolution


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
'Constants for GetSystemMetrics
 Const SM_CXSCREEN = 0        ' Width of screen
 Const SM_CYSCREEN = 1        ' Height of screen

Private Sub Command1_Click()
    Dim XVal As Long, YVal As Long
    YVal = GetSystemMetrics(SM_CYSCREEN)
    XVal = GetSystemMetrics(SM_CXSCREEN)
    MsgBox "Your Screen Resolution is " & XVal & " by " & YVal
End Sub

Top

Returns the windows Short File name for a given Long File Name with VB

The following code takes a Long File Name and returns the Short File name


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetShortPathName Lib "KERNEL32.DLL" Alias "GetShortPathNameA" _
  (ByVal lpctstrLongName As String, ByVal lptstrShortName As String, ByVal bufLen As Long) As Long

Private Sub Command1_Click()
    Dim LongStr As String, ShortStr As String
    Dim lStrLen As Long, lRet As Long
    
    'LongStr is any long file name or variable pointing to a file
    LongStr = "C:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB"
    lRet = GetShortPathName(LongStr, ShortStr, lStrLen)
    'This allows us to create a buffer the same length as the returned string, 
    'saving us the trouble of having to strip the left of the buffer to get the string
    ShortStr = String(lRet, " ")
    lRet = GetShortPathName(LongStr, ShortStr, lRet)
    MsgBox LongStr & "   was converted to   " & ShortStr
End Sub

Top

Get a list of all the Logical drives of a machine with VB code (logical drives are removable, fixed, CD drives and mapped network shares)

The following example uses the function GetLogicalDriveStrings to loop through all logical drives


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" _
  (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Sub Command1_Click()
    Dim DrvString As String
    Dim TotDrvs As Long
    Dim Counter As Integer
    
    'TotDrvs returns the total number of characters in return string
    TotDrvs = GetLogicalDriveStrings(0&, DrvString)
    'DrvString is the buffer created to hold the string
    DrvString = String(TotDrvs - 1, " ")
    'Calling GetLogicalDriveStrings a second time fills the string with valid data
    'example "a:\c:\d:\e:\"
    TotDrvs = GetLogicalDriveStrings(TotDrvs, DrvString)
    
    'Parse through the return string displaying each in a msgbox
    For Counter = 1 To TotDrvs Step 4
        MsgBox Mid(DrvString, Counter, 3)
    Next Counter
End Sub

Top

Find the free disk space available as well as the total hard drive size with VB

The following code example shows how to find the free space available as well as the total hard drive size.


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetDiskFreeSpace Lib "KERNEL32.DLL" Alias "GetDiskFreeSpaceA" (ByVal lpRoot As String, _
  dwSectors As Long, dwBytes As Long, dwFreeClusters As Long, dwTotalClusters As Long) As Long

Private Sub Command1_Click()
    Dim f As Long, iSectors As Long
    Dim iTotal As Long, rTotal As Long
    Dim iFree As Long, rFree As Long
    Dim iBytes As Long
    Dim sName As String, s As String

    sName = "C:\"
    f = GetDiskFreeSpace(sName, iSectors, iBytes, iFree, iTotal)
    rFree = iSectors * iBytes * CDbl(iFree)
    rTotal = iSectors * iBytes * CDbl(iTotal)
    If f Then
        s = sName
        s = s & " has " & Format(rFree, "#,###,###,##0")
        s = s & " bytes free from " & Format(rTotal, "#,###,##0") & " Total bytes"
    End If
    MsgBox s
End Sub

Top

Retrieve the system folder location with VB code

The following code example show how to retrieve the system folder location.


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Command1_Click()
    Dim Sys_Dir As String, Res As Long
    Res = GetSystemDirectory(Sys_Dir, 0&)
    Sys_Dir = String(Res - 1, " ")
    Res = GetSystemDirectory(Sys_Dir, Res)
    MsgBox Sys_Dir
End Sub

Top

Check to see if a computer can play WAV files (checks if there are any devices on a machine that are capable of playing WAV files)

 


'Add a Command button to your Form (Form1)
'Paste the following in to the code window of your form

'Next line declares the API function in the general declarations section
Private Declare Function waveOutGetNumDevs Lib "winmm" () As Long

Private Sub Command1_Click()
    Dim i As Long
    i = waveOutGetNumDevs()
    If i > 0 Then         ' There is at least one device
        MsgBox "You Can Play Wave Data"
    Else
        MsgBox "Cannot Play Wave Data"
    End If
End Sub

Top

Top

www.mahipalreddy.com
Terms and Conditions of Use
Copyright 2004 - 2006 Mahipal Padigela. All rights reserved.