PDA

View Full Version : Solved: Operating System Inquiry Function Results



kv2e
12-12-2010, 01:51 PM
In a thread from 07-24-2008
Mavyak offered the following (brilliant) code to provide OS identity
'// it's a much more accurate result than Environ("OS")



Function getOS() As String
Dim OS
For Each OS In GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
getOS = OS.Caption
Next OS
Set OS = Nothing
End Function


Can anyone point me to a site page that provides an accurate list of possible results. I use XP, and the code above returns "Microsoft Windows XP Professional" - but need to know what the result would be for the others especially Vista and Windows 7.

Right now, I only need it to specify the path for "My Documents" or "Documents" via Select Case, but I suppose it could prove helpful in other situations - yes?

Thnx,
Bill

hansup
12-12-2010, 05:20 PM
Can anyone point me to a site page that provides an accurate list of possible results. I use XP, and the code above returns "Microsoft Windows XP Professional" - but need to know what the result would be for the others especially Vista and Windows 7. I can show you this one.
? getOS
Microsoft Windows 7 Ultimate

Right now, I only need it to specify the path for "My Documents" or "Documents" via Select Case, but I suppose it could prove helpful in other situations - yes?If you use the Windows API to determine the location of the personal folder, your code can still work if those folders are mapped to a non-standard location, and you needn't worry about which Windows version is in use.

This code is from MSDN:Identify the Location of Special Folders with API Calls
http://msdn.microsoft.com/en-us/library/aa140088%28office.10%29.aspx

Option Compare Database
Option Explicit

Public Declare Function SHGetSpecialFolderLocation _
Lib "shell32" (ByVal hWnd As Long, _
ByVal nFolder As Long, ppidl As Long) As Long

Public Declare Function SHGetPathFromIDList _
Lib "shell32" Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, ByVal pszPath As String) As Long

Public Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)

Public Const CSIDL_PERSONAL = &H5
Public Const CSIDL_DESKTOPDIRECTORY = &H10
Public Const MAX_PATH = 260
Public Const NOERROR = 0

Public Function SpecFolder(ByVal lngFolder As Long) As String
Dim lngPidlFound As Long
Dim lngFolderFound As Long
Dim lngPidl As Long
Dim strPath As String

strPath = Space(MAX_PATH)
lngPidlFound = SHGetSpecialFolderLocation(0, lngFolder, lngPidl)
If lngPidlFound = NOERROR Then
lngFolderFound = SHGetPathFromIDList(lngPidl, strPath)
If lngFolderFound Then
SpecFolder = Left$(strPath, _
InStr(1, strPath, vbNullChar) - 1)
End If
End If
CoTaskMemFree lngPidl
End FunctionThen to determine the location of my personal folder:
? SpecFolder(CSIDL_PERSONAL)
C:\Users\Hans\Documents

kv2e
12-15-2010, 10:57 AM
Thanks Hans, your suggestion worked perfectly with one exception: I had to remove the first line (Option Compare Database).

Genereally, I don't like to use code that I do not *completely* understand, but after reading the article at the link you provided, I believe I understand most of it. It will be a valuable addition to my personal library of code.

Best wishes for the Holidays,
Bill