How to Get the Windows System Directory
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 'System & API - How to Get the Windows System Directory Option Explicit Global Const gstrSEP_DIR$ = "\" Global Const gstrSEP_URLDIR$ = "/" Global Const gintMAX_SIZE% = 255 Declare Function GetSystemDirectory Lib "Kernel32" Alias "GetSystemDirectoryA" _ (ByVal lpBuffer As String, ByVal nSize As Long) As Long Public Function GetWindowsSysDir() As String Dim strBuf As String strBuf = Space$(gintMAX_SIZE) 'Get the system directory and then trim the buffer to the exact length 'returned and add a dir sep (backslash) if the API didn't return one If GetSystemDirectory(strBuf, gintMAX_SIZE) > 0 Then strBuf = StripTerminator(strBuf) AddDirSep strBuf GetWindowsSysDir = strBuf Else GetWindowsSysDir = vbNullString End If End Function Private Function StripTerminator(ByVal strString As String) As String Dim intZeroPos As Integer intZeroPos = InStr(strString, Chr$(0)) If intZeroPos > 0 Then StripTerminator = Left$(strString, intZeroPos - 1) Else StripTerminator = strString End If End Function Private Sub AddDirSep(ByVal strPathName As String) If Right$(Trim$(strPathName), Len(gstrSEP_URLDIR)) <> gstrSEP_URLDIR And _ Right$(Trim$(strPathName), Len(gstrSEP_DIR)) <> gstrSEP_DIR Then strPathName = RTrim$(strPathName) & gstrSEP_DIR End If End Sub |