VBA code to locate windows\system32 on any PC

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All

I am trying to write some code in a VB/Access application that will permit
me to create & write to a text file on any windows 98/nt/2000/xp/2003
install.

I must first off all be able to locate the windir.
"%windir%\system32" does not work in VBA code as I discovered

Cheers all
 
There's code to do this at the following URL ...

http://www.mvps.org/access/api/api0010.htm

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Hi 'All'
It seems you need to know the name of the Windows\System32 folder on a PC
that might have any version of Windows.

The code can be called from apiGetSpecialFolderTest in the following
(lengthy code), which can be added to a module or class module.

Adrian


Option Compare Database
Option Explicit




'For SpecialFolderLocation:
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, ppidl As Long) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias
"SHBrowseForFolderA" _
(lpbi As BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias
"SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function GetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long 'Windows folder
Private Declare Function GetSystemDirectory Lib "kernel32" _
Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, _
ByVal nSize As Long) As Long 'Windows system folder
Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" _
(ByVal nSize As Long, _
ByVal lpBuffer As String) As Long 'Windows temp folder


Private Type BROWSEINFO
hwndOwner As Long
pIDlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type



'Parameter list for GetSpecialFolder adapted from
'http://www.mvps.org/vbnet/index.html?code/browse/shpathidlist.htm
Public Enum apSpecialFolderName
cDesktop = &H0 'W2K: C:\Documents and
Settings\Adrian\Desktop
cInternet = &H1 'Internet Explorer (icon on desktop)
cPrograms = &H2 'Start Menu\Programs
cControlPanel = &H3 'My Computer\Control Panel
cPrinters = &H4 'My Computer\Printers
cMyDocuments = &H5 'My Documents
cUserFavorites = &H6 '{user}\Favourites
cStartUp = &H7 'Start Menu\Programs\Startup
cUserRecent = &H8 '{user}\Recent
cUserSendTo = &H9 '{user}\SendTo
cRecycleBin = &HA '{desktop}\Recycle Bin
cUserStartMenu = &HB '{user}\Start Menu
cUserDesktop = &H10 '{user}\Desktop
cMyComputer = &H11 'My Computer
cNetworkNeighbourhood = &H12 'Network Neighbourhood
cUserNethood = &H13 '{user}\nethood
cFonts = &H14 'windows\fonts
cUserTemplates = &H15 'User's templates
cAllUsersStartMenu = &H16 'All Users\Start Menu
cAllUsersPrograms = &H17 'All Users\Programs
cAllUsersStartup = &H18 'All Users\Startup
cAllUsersDesktop = &H19 'W2K: C:\Documents and Settings\All
Users\Desktop
cUserAppData = &H1A '{user}\Application Data
cUserPrintHood = &H1B '{user}\PrintHood
cLOCAL_APPDATA = &H1C '{user}\Local Settings\Application Data
(non roaming)
cALTSTARTUP = &H1D 'non localized startup
cCOMMON_ALTSTARTUP = &H1E 'non localized common startup
cCOMMON_FAVORITES = &H1F
cINTERNET_CACHE = &H20
cCOOKIES = &H21
cHISTORY = &H22
cAllUsersAppData = &H23 'All Users\Application Data
cWindows = &H24 'GetWindowsDirectory()
csystem = &H25 'GetSystemDirectory()
cProgramFiles = &H26 'C:\Program Files
cMyPictures = &H27 'D:\My Documents\My Pictures
cPROFILE = &H28 'USERPROFILE
cSYSTEMX86 = &H29 'x86 system directory on RISC
cPROGRAM_FILESX86 = &H2A 'x86 C:\Program Files on RISC
cProgramFilesCommon = &H2B 'C:\Program Files\Common
cPROGRAM_FILES_COMMONX86 = &H2C 'x86 Program Files\Common on RISC
cAllUsersTemplatesLocal = &H2D 'W2K: C:\Documents and Settings\All
Users\Templates
cAllUsersDocuments = &H2E 'All Users\Documents
cCOMMON_ADMINTOOLS = &H2F 'All Users\Start
Menu\Programs\Administrative Tools
cADMINTOOLS = &H30 '{user}\Start
Menu\Programs\Administrative Tools

'Added by me
cTemp = &HF0 'Windows temp folder

End Enum






'19 Nov 2003
Function apiGetSpecialFolder(nFolder As apSpecialFolderName) As String
' Returns path to Special Folder in list
' Parameters will pop-up when programnming

Dim retval As Long
Dim lPidl As Long
Dim sPath As String


Select Case nFolder 'Some cases different for different OS

Case apSpecialFolderName.cWindows
apiGetSpecialFolder = GetWinFolder

Case apSpecialFolderName.csystem
apiGetSpecialFolder = GetSystemDir

Case apSpecialFolderName.cTemp
apiGetSpecialFolder = GetTempDir

Case Else

sPath = Space(255)
retval = SHGetSpecialFolderLocation(0, nFolder, lPidl)
If retval = 0 Then
retval = SHGetPathFromIDList(lPidl, sPath)
If retval = 1 Then
apiGetSpecialFolder = Left(sPath, InStr(sPath, Chr(0)) -
1)
End If
End If

End Select

End Function

Private Sub apiGetSpecialFolderTest()
Dim temp
temp = apiGetSpecialFolder(csystem)
End Sub




'10 May 2002
'Modules can use temp = fs.GetSpecialFolder
Function GetWinFolder() As String
' Returns the path to Windows folder

Dim strBuffer As String, lngLen As Long

strBuffer = Space$(255)
lngLen = GetWindowsDirectory(strBuffer, 255)
GetWinFolder = Left$(strBuffer, lngLen)

End Function





'20 Sep 2003
Private Function GetSystemDir() As String
'Return the path to Windows system or system32 folder depending on OS

Dim nSize As Long
Dim tmp As String

tmp = Space$(256)
nSize = Len(tmp)
Call GetSystemDirectory(tmp, nSize)

GetSystemDir = apTrimNull(tmp)

End Function





'20 Sep 2003
Private Function GetTempDir() As String

Dim nSize As Long
Dim tmp As String

tmp = Space$(256)
nSize = Len(tmp)
Call GetTempPath(nSize, tmp)

GetTempDir = apTrimNull(tmp)

End Function




'20 Sep 2003
Function apTrimNull(ByVal sItem As String) As String
'Trims the trailing null returned from API calls


Dim nPos As Integer

nPos = InStr(sItem, vbNullChar)

If nPos > 0 Then
apTrimNull = Left$(sItem, nPos - 1)
Else
apTrimNull = sItem
End If

End Function
 
Thank Man,

That work a treat

Cheers All


Brendan Reynolds said:
There's code to do this at the following URL ...

http://www.mvps.org/access/api/api0010.htm

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top