Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Windows directory
Message
From
14/03/2001 18:41:04
 
General information
Forum:
Visual Basic
Category:
Windows API functions
Miscellaneous
Thread ID:
00483995
Message ID:
00485097
Views:
9
>>>>Besides the API call suggestions, GetEnv("WinDir") works well for the windows
>>>>directory. The system directory however still must be read through the API
>>>>
>>>>>Can anybody tell me how in VB I can get the wondowsdirectory ??
>>>
>>>Ed,
>>>
>>>Piet wants VB code for this, not VFP. It could be done either through API, either through WHS.
>>
>>Then Piet can use the VB function Environ instead of GetEnv(). The concept is still valid.
>
>Great, thanks. Not sure, I knew this function. I start forgetting VB, since I don't use it in my work :(

Piet, here is what you need and more. Create a module and add it to your
project. I have named this WindowsGen but you can name it what ever. It
includes other groovy functions as well. In VB, BTW. :) You will find
it to be complete and very straight forward. I believe others will love
my GetDesktop code as it works in 9x as well as NT/2000. :)
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type

' **********************************
' **********************************
Public Function getWinVersion() As String
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)


    With osinfo


        Select Case .dwPlatformId
            Case 1
                If .dwMinorVersion = 0 Then
                    getWinVersion = "Windows 95"
                ElseIf .dwMinorVersion = 10 Then
                    getWinVersion = "Windows 98"
                End If
            Case 2
                If .dwMajorVersion = 3 Then
                    getWinVersion = "Windows NT 3.51"
                ElseIf .dwMajorVersion = 4 Then
                    getWinVersion = "Windows NT 4.0"
                ElseIf .dwMajorVersion = 5 Then
                    getWinVersion = "Windows 2000"
                End If
            Case Else
                getWinVersion = "Failed " & "Platform Id " & Str(dwPlatformId) & "Maj " & Str(dwMajorVersion)
        End Select
    End With
End Function


' Returns the window title text
Public Function TopWindow() As String
    Dim hWnd As Long, llen As Long
    Dim strTitle As String
    strTitle = Space(255) & vbNullChar

    hWnd = GetForegroundWindow()
    llen = GetWindowText(hWnd, strTitle, 255&)

    If llen > 0 Then
        strTitle = Left$(strTitle, llen)
        TopWindow = strTitle
    Else
        TopWindow = "Error retrieving title"
    End If

End Function

Public Function GetWinDir() As String
    Dim strPath As String * 255
    ReturnLength = GetWindowsDirectory(strPath, Len(strPath))
    GetWinDir = AlphaNumericsOnly(strPath)
End Function

Public Function GetWinSysDir() As String
    Dim strPath As String * 255
    ReturnLength = GetSystemDirectory(strPath, Len(strPath))
    GetWinSysDir = AlphaNumericsOnly(strPath) ' a must have.
End Function

Public Function GetSysDir() As String
    GetSysDir = GetWinSysDir
End Function

Public Function GetDesktop() As String
      Dim strDesktop As String
      Dim strOS As String
      Dim strWinDir As String
      Dim strUserName As String
      Dim lngNameLen As Long
      
      strUserName = Space(64)
      lngNameLen = Len(strUserName)
      strOS = getWinVersion
      strWinDir = GetWinDir
      GetUserName strUserName, lngNameLen
      
      strDesktop = ""
             
      If Left$(strOS, 9) = "Windows 9" Then
        strDesktop = strWinDir + "\Desktop"
      Else
        strDesktop = strWinDir & _
                     "\Profiles\" & _
                     Left$(Trim$(strUserName), Len(Trim$(strUserName)) - 1) & _
                     "\Desktop\"
      End If
      
      If Dir$(strDesktop, vbDirectory) <> "" Then
         GetDesktop = strDesktop
      End If
End Function
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Reply
Map
View

Click here to load this message in the networking platform