📄 environment.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Environment"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2004 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: Environment
'
''
' The Environment class provides functions for retrieving information about the local machine
'
' @remarks This class cannot be directly instantiated. In order to access the methods, use
' the class name directly.
' <pre>
' Debug.Print Environment.MachineName
' Debug.Print Environment.UserName
' </pre>
'
Option Explicit
Private Const ERR_PATHNOTFOUND As Long = 76
Private Const MAX_COMPUTERNAME_LENGTH As Long = 31
Private Const UNLEN As Long = 256
Private Const UOI_FLAGS As Long = 1
Private Const WSF_VISIBLE As Long = 1
Private Const WM_SETTINGCHANGE As Long = &H1A
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const SMTO_NORMAL As Long = &H0
Private Const ERROR_ENVVAR_NOT_FOUND As Long = 203&
''
' This enum represents a list of folders accessible using <i>GetFolderPath</i>.
' These definitions are from the Microsoft Dot NET 1.1 MSDN Library.
'
' @param ApplicationData The directory that serves as a common repository for application-specific data for the current roaming user.
' A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.
' @param CommonApplicationData The directory that serves as a common repository for application-specific data that is used by all users.
' @param CommonProgramFiles The directory for components that are shared across applications.
' @param Cookies The directory that serves as a common repository for Internet cookies.
' @param Desktop The logical Desktop rather than the physical file system location.
' @param DesktopDirectory The directory used to physically store file objects on the desktop.
' Do not confuse this directory with the desktop folder itself, which is a virtual folder.
' @param Favorites The directory that serves as a common repository for the user's favorite items.
' @param History The directory that serves as a common repository for Internet history items.
' @param InternetCache The directory that serves as a common repository for temporary Internet files.
' @param LocalApplicationData The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
' @param MyComputer The "My Computer" folder.
' @param MyMusic The "My Music" folder.
' @param MyPictures The "My Pictures" folder.
' @param Personal The directory that serves as a common repository for documents (My Documents).
' @param ProgramFiles The program files folder.
' @param Programs The directory that contains the user's program groups.
' @param Recent The directory that contains the user's most recently used documents.
' @param SendTo The directory that contains the Send To menu items.
' @param StartMenu The directory that contains the Start menu items.
' @param Startup The directory that corresponds to the user's Startup program group.
' The system starts these programs whenever a user logs on or starts Windows NT or later, or starts Windows 98.
' @param SystemDir The system directory.
' @param Templates The directory that serves as a common repository for document templates.
' @param Windows The windows directory.
'
Public Enum SpecialFolder
ApplicationData = &H1A
CommonApplicationData = &H23
CommonProgramFiles = &H2B
Cookies = &H21
Desktop = &H0
DesktopDirectory = &H10
Favorites = &H6
History = &H22
InternetCache = &H20
LocalApplicationData = &H1C
MyComputer = &H11
MyMusic = &HD
MyPictures = &H27
Personal = &H5
ProgramFiles = &H26
Programs = &H2
Recent = &H8
SendTo = &H9
StartMenu = &HB
Startup = &H7
SystemDir = &H25
Templates = &H15
Windows = &H24
End Enum
''
' Used to indicate the source or destination of the
' environment variables when read from or written to the system.
'
' @param Machine The source of the variable is machine wide. These
' are variables that are defined for the system and all users.
' @param Process The source of the variable is for the current process.
' This combines both machine and user level variables, giving priority
' to the user level variables.
' @param User The source of the variable is of the current user.
' @remarks <b>Machine</b> and <b>User</b> are not supported on Windows 95, 98 or ME.
'
Public Enum EnvironmentVariableTarget
Machine = 0
Process = 1
User = 2
End Enum
Private mOSVersion As New OperatingSystem
''
' Returns the command specified when executing the current application,
' including any arguments typed after the application name.
'
' @return The command line typed in at a command prompt.
'
Public Property Get CommandLine() As String
CommandLine = SysAllocString(GetCommandLineW)
End Property
''
' Returns the current directy of the executing application.
'
' @return The current directory.
'
Public Property Get CurrentDirectory() As String
CurrentDirectory = Directory.GetCurrentDirectory
End Property
''
' Sets the current directory for the executing application.
'
' @param RHS The new directory to change to.
'
Public Property Let CurrentDirectory(ByVal RHS As String)
Call Directory.SetCurrentDirectory(RHS)
End Property
''
' Returns the name of the local computer.
'
' @return The name of the local computer.
'
Public Property Get MachineName() As String
Dim Size As Long
Size = MAX_COMPUTERNAME_LENGTH + 1
Dim Ret As String
Ret = String$(Size, 0)
If API.GetComputerName(Ret, Size) = BOOL_FALSE Then _
Throw Cor.NewInvalidOperationException(GetErrorMessage(Err.LastDllError))
MachineName = Left$(Ret, Size)
End Property
''
' Returns string of a newline character for the local computer.
'
' @return The newline characters.
' @remarks Being this always going to be in VB, the return is just vbCrLf.
'
Public Property Get NewLine() As String
NewLine = vbCrLf
End Property
''
' Returns an object representing the current operating system.
'
' @return The OperatingSystem object that represents the current OS.
'
Public Property Get OSVersion() As OperatingSystem
Set OSVersion = mOSVersion
End Property
''
' Returns the System directory for the current OS.
'
' @return The system directory.
' @remarks Generally this will be C:\Windows\System32 or C:\Winnt\System32 for NT4 system.
'
Public Property Get SystemDirectory() As String
Dim Ret As String
Ret = String$(MAX_PATH + 1, 0)
Dim Size As Long
Size = API.GetSystemDirectory(Ret, Len(Ret))
If Size > 0 Then SystemDirectory = Left$(Ret, Size)
End Property
''
' Returns the number of milliseconds the system has been running since it started.
'
' @return Number of milliseconds since the system booted up.
' @remarks Once the maximum value is reached, it will wrap around to be negative,
' at which point negative values will be returned until 0 is reached.
' Wrapping to negative will take place in approximatively 24.85 days.
'
Public Property Get TickCount() As Long
TickCount = timeGetTime
End Property
''
' Returns the name of the local machine within a domain
'
' @return The computer name within a domain.
' @remarks Typically the domain name is the name of the machine, but
' can change depending on the application being deployed.<br><br>
' <b>UserDomainName</b> is not supported on Windows 95, 98, ME platforms
' and will throw a <b>PlatformNotSupportedException</b> exception.
'
Public Property Get UserDomainName() As String
Call VerifyNTMachine
Dim Size As Long
Size = 128
Dim Sid As String
Sid = String$(Size, 0)
Dim Buf As String
Do
Buf = String$(Size, 0)
If LookupAccountName(0, StrPtr(UserName), StrPtr(Sid), Len(Sid), StrPtr(Buf), Size, 0&) = BOOL_FALSE Then IOError Err.LastDllError
Loop While Size > Len(Buf)
UserDomainName = SysAllocString(StrPtr(Buf))
End Property
''
' Determines if the current application is allowed to have user interaction.
'
' @return Indication if user interaction is allowed.
' @remarks This is used primarily for when the application is a service. It can
' be used to supress message dialogs and user entry when not allowed.
'
Public Property Get UserInteractive() As Boolean
Dim hObj As Handle
Dim Info As USEROBJECTFLAGS
If OSVersion.Platform = Win32NT Then
hObj = GetProcessWindowStation
If GetUserObjectInformation(hObj, UOI_FLAGS, Info, LenB(Info), 0&) Then
UserInteractive = (Info.dwFlags And WSF_VISIBLE)
End If
Else
UserInteractive = True
End If
End Property
''
' Returns the name of the currently logged in user of the local computer.
'
' @return The currently logged in user's name.
'
Public Property Get UserName() As String
Dim Size As Long
Size = UNLEN + 1
Dim Ret As String
Ret = String$(Size, 0)
If GetUserNameA(Ret, Size) <> BOOL_FALSE Then UserName = Left$(Ret, Size - 1)
End Property
''
' Returns the amount of physical memory the current application is allocated.
'
' @return The amount of memory allocated to the application.
' @remarks This method only works for NT installations. 95, 98, and ME will
' receive a 0 when this method is called.
'
Public Property Get WorkingSet() As Long
If OSVersion.Platform <> Win32NT Then Exit Property
Dim h As Handle
h = GetCurrentProcess
Dim Info As PROCESS_MEMORY_COUNTERS
If GetProcessMemoryInfo(h, Info, LenB(Info)) Then
WorkingSet = Info.WorkingSetSize
End If
End Property
''
' Replaces environment tags within a string with the actual values.
'
' @param Name A string containing environment variable tags to be replaced.
' @return A string containing actual environment variable values in place of tags.
' @remarks The environment variables are tagged using %.
' A string such as 'My OS is %OS%.' would become 'My OS is Windows_NT.' for NT machines.
'
Public Function ExpandEnvironmentVariables(ByVal Name As String) As String
Const DEF_SIZE As Long = 128
If Len(Name) = 0 Then Exit Function
Dim Buf As String
Buf = SysAllocStringLen(0, DEF_SIZE)
Dim Size As Long
Size = API.ExpandEnvironmentStrings(Name, Buf, DEF_SIZE)
If Size > DEF_SIZE Then
Buf = SysAllocStringLen(0, Size)
Size = API.ExpandEnvironmentStrings(Name, Buf, Size)
End If
ExpandEnvironmentVariables = Left$(Buf, Size - 1)
End Function
''
' Returns an array of parsed Arguments from the command line.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -