Getting special paths using VB6 (temp, profile, system)

Visual Basic Topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Getting special paths using VB6 (temp, profile, system)

Post by Neo » Fri Feb 26, 2010 12:38 pm

Temp path

Declarations:

Code: Select all

Const MAX_PATH = 255

Private Declare Function GetTempPath Lib "kernel32" _ 
Alias "GetTempPathA" (ByVal nBufferLength As Long, _
ByVal lpBuConst MAX_PATH = 255
Code:

Code: Select all

Public Function GetTempDir() As String
    Dim sRet As String, lngLen As Long
    
    'create buffer
    sRet = String(MAX_PATH, 0)

    lngLen = GetTempPath(MAX_PATH, sRet)
    If lngLen = 0 Then err.Raise err.LastDllError
    GetTempDir = Left$(sRet, lngLen)
End Function

User's profile path
Declarations:

Code: Select all

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Code:

Code: Select all

' Return the user's profile path.
Private Function ProfilePath() As String
Dim win_dir As String
Dim user_name As String
Dim ret_len As Long

    ' Get the windows directory.
    win_dir = Space$(256)
    ret_len = GetWindowsDirectory(win_dir, Len(win_dir))
    win_dir = Left$(win_dir, ret_len)

    ' Get the user's name.
    user_name = Space$(256)
    GetUserName user_name, ret_len
    user_name = Left$(user_name, ret_len)

    ProfilePath = win_dir & "\" & user_name
End Function
Get the Windows and System directories
Declarations:

Code: Select all

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Code:

Code: Select all

Private Sub Form_Load()
Dim buf As String * 256
Dim return_len As Long
Dim wid1 As Single
Dim wid2 As Single

    return_len = GetSystemDirectory(buf, Len(buf))
    lblSystemDirectory.Caption = Left$(buf, return_len)

    return_len = GetWindowsDirectory(buf, Len(buf))
    lblWindowsDirectory.Caption = Left$(buf, return_len)

    wid1 = lblWindowsDirectory.Left + _
        lblWindowsDirectory.Width + Label1(0).Left
    wid2 = lblSystemDirectory.Left + _
        lblSystemDirectory.Width + Label1(0).Left
    If wid1 > wid2 Then
        Width = Width - ScaleWidth + wid1
    Else
        Width = Width - ScaleWidth + wid2
    End If
End Sub
Post Reply

Return to “Visual Basic Programming”