Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows NT "Desktop" reference

Status
Not open for further replies.

pmessana

Programmer
Jan 24, 2003
19
0
0
US
Does anyone know how to reference the "desktop" while coding. It Windows 98 and prior it was statis 'c:\windows\desktop' but in NT and newer it is based on the user that is logged on. I am looking for a way to reference it regardless of user. Any clue?

Thanks in advance!
 
Try these functions and see if one of the environment variables will give you the information you need.

The first function based on an API call came from someone on the Tek-Tips board but I don't remember who so I can't give him the credit he deserves.

The second one has been adapted from Access97 help on the Environ$ function. It will return a 2 dimensional array of environment variables. The 0th element will always be empty because Environ$ is 1 based and I wanted to maintain a direct relationship between the array subscript and environment index.

Hope this helps and Good Luck!

PS. Sorry for the ugly formatting. If you copy and paste it into a text editor it should hopefully revert to the original formatting.

***************************** Begin Code *****************
Option Compare Database
Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

Public Function GetUserLoginName(Optional StripNullTerminator As Boolean = True) As String
Dim strName As String
Dim strResult As String
strName = Space(255)
Call WNetGetUser("", strName, Len(strName))
strResult = Trim$(strName)
'Not sure we want this - should this be optional true or false
If StripNullTerminator Then
If Asc(Right$(strResult, 1)) = 0 Then
strResult = Left$(strResult, Len(strResult) - 1)
End If
End If
GetUserLoginName = strResult
End Function 'GetUserLoginName

Public Function GetEnvironmentSettings() As Variant
Dim strEnviron As String
Dim lngIndex As Long
Dim strMessage As String
Dim lngPathLength As Long
Dim avarEnviron As Variant
lngIndex = 1
ReDim avarEnviron(1, lngIndex)
Do Until Len(Environ$(lngIndex)) = 0
strEnviron = Environ$(lngIndex) 'Get environment variables
ReDim Preserve avarEnviron(1, lngIndex)
avarEnviron(0, lngIndex) = Left$(strEnviron, InStr(strEnviron, "=") - 1)
avarEnviron(1, lngIndex) = Mid$(strEnviron, InStr(strEnviron, "=") + 1)
' MsgBox "Environ$(" & lngIndex & "): " & strEnviron
If Left$(strEnviron, 5) = "PATH=" Then
lngPathLength = Len(Environ$("PATH")) ' Get length.
strMessage = "PATH entry = " & lngIndex & " and length = " & lngPathLength
' Exit Do
End If
lngIndex = lngIndex + 1
Loop
strEnviron = ListEnvironmentArray(avarEnviron)
MsgBox "Environment variables for this PC:" & vbCrLf & vbCrLf & strEnviron
GetEnvironmentSettings = avarEnviron
End Function 'GetEnvironmentSettings

Private Function ListEnvironmentArray(ByRef ravarEnviron As Variant) As String
Dim ialng As Long
Dim str As String
For ialng = 1 To UBound(ravarEnviron, 2) 'Skip 0 because will always be empty
str = str & ravarEnviron(0, ialng) & ": " & ravarEnviron(1, ialng) & vbCrLf
Next ialng
ListEnvironmentArray = str
End Function
***************************** End Code *******************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top