[142] | 1 | ' System/Environment.ab
|
---|
| 2 |
|
---|
[237] | 3 | #require <api_psapi.sbp>
|
---|
[142] | 4 |
|
---|
[268] | 5 | Declare Function _System_SetEnvironmentVariable Lib "kernel32" Alias _FuncName_SetEnvironmentVariable (lpName As LPCTSTR, lpValue As LPTSTR) As BOOL
|
---|
| 6 | Declare Function _System_GetEnvironmentVariable Lib "kernel32" Alias _FuncName_GetEnvironmentVariable (lpName As PCTSTR, lpBuffer As PTSTR, nSize As DWord) As DWord
|
---|
| 7 |
|
---|
[258] | 8 | Namespace System
|
---|
| 9 |
|
---|
| 10 | Namespace Detail
|
---|
| 11 | TypeDef PFNGetProcessMemoryInfo = *Function(Process As HANDLE, ByRef mc As PROCESS_MEMORY_COUNTERS, cb As DWord) As BOOL
|
---|
[268] | 12 |
|
---|
| 13 | Dim hasShutdownStarted As Boolean
|
---|
[258] | 14 | End Namespace
|
---|
| 15 |
|
---|
[142] | 16 | Class Environment
|
---|
| 17 | Public
|
---|
| 18 | ' Properties
|
---|
| 19 |
|
---|
| 20 | Static Function CommandLine() As String
|
---|
[237] | 21 | If Object.ReferenceEquals(cmdLine, Nothing) Then
|
---|
[497] | 22 | #ifdef UNICODE
|
---|
| 23 | cmdLine = New String(GetCommandLineW())
|
---|
| 24 | #else
|
---|
[142] | 25 | cmdLine = New String(GetCommandLineA())
|
---|
| 26 | #endif
|
---|
| 27 | End If
|
---|
| 28 | Return cmdLine
|
---|
| 29 | End Function
|
---|
| 30 |
|
---|
| 31 | Static Function CurrentDirectory() As String
|
---|
| 32 | Dim size = GetCurrentDirectory(0, 0)
|
---|
[388] | 33 | Dim p = GC_malloc_atomic(SizeOf (TCHAR) * size) As PCTSTR
|
---|
[208] | 34 | Dim len = GetCurrentDirectory(size, p)
|
---|
| 35 | If len < size Then
|
---|
[427] | 36 | CurrentDirectory = New String(p, len As Long)
|
---|
[208] | 37 | End If
|
---|
[142] | 38 | End Function
|
---|
| 39 |
|
---|
| 40 | Static Sub CurrentDirectory(cd As String)
|
---|
| 41 | SetCurrentDirectory(ToTCStr(cd))
|
---|
| 42 | End Sub
|
---|
| 43 |
|
---|
| 44 | Static Function ExitCode() As Long
|
---|
| 45 | Return exitCode
|
---|
| 46 | End Function
|
---|
| 47 |
|
---|
| 48 | Static Sub ExitCode(code As Long)
|
---|
| 49 | exitCode = code
|
---|
| 50 | End Sub
|
---|
| 51 |
|
---|
[173] | 52 | Static Function HasShutdownStarted() As Boolean
|
---|
[268] | 53 | Return Detail.hasShutdownStarted
|
---|
[173] | 54 | End Function
|
---|
[142] | 55 |
|
---|
[258] | 56 | Static Function MachineName() As String
|
---|
| 57 | If Object.ReferenceEquals(machineName, Nothing) Then
|
---|
| 58 | Dim buf[MAX_COMPUTERNAME_LENGTH] As TCHAR
|
---|
| 59 | Dim len = (MAX_COMPUTERNAME_LENGTH + 1) As DWord
|
---|
| 60 | GetComputerName(buf, len)
|
---|
| 61 | machineName = New String(buf, len As Long)
|
---|
| 62 | End If
|
---|
| 63 | Return machineName
|
---|
[268] | 64 | End Function
|
---|
[142] | 65 |
|
---|
| 66 | Static Function NewLine() As String
|
---|
| 67 | Return Ex"\r\n"
|
---|
| 68 | End Function
|
---|
| 69 |
|
---|
| 70 | Static Function OSVersion() As OperatingSystem
|
---|
[237] | 71 | If Object.ReferenceEquals(osVer, Nothing) Then
|
---|
[142] | 72 | Dim vi As OSVERSIONINFO
|
---|
[233] | 73 | GetVersionEx(vi)
|
---|
[142] | 74 | osVer = New OperatingSystem(vi)
|
---|
| 75 | End If
|
---|
| 76 | Return osVer
|
---|
| 77 | End Function
|
---|
| 78 |
|
---|
| 79 | Static Function ProcessorCount() As Long
|
---|
| 80 | If processorCount = 0 Then
|
---|
| 81 | Dim si As SYSTEM_INFO
|
---|
| 82 | GetSystemInfo(si)
|
---|
| 83 | processorCount = si.dwNumberOfProcessors
|
---|
| 84 | End If
|
---|
| 85 | Return processorCount
|
---|
| 86 | End Function
|
---|
| 87 |
|
---|
| 88 | ' StackTrace
|
---|
| 89 |
|
---|
| 90 | Static Function SystemDirectory() As String
|
---|
[237] | 91 | If Object.ReferenceEquals(sysDir, Nothing) Then
|
---|
[142] | 92 | Dim size = GetSystemDirectory(0, 0)
|
---|
[388] | 93 | Dim p = GC_malloc_atomic(SizeOf (TCHAR) * size) As PTSTR
|
---|
[237] | 94 | Dim len = GetSystemDirectory(p, size)
|
---|
[208] | 95 | sysDir = New String(p, len As Long)
|
---|
[268] | 96 | End If
|
---|
[142] | 97 | Return sysDir
|
---|
| 98 | End Function
|
---|
| 99 |
|
---|
| 100 | Static Function TickCount() As Long
|
---|
| 101 | Return GetTickCount() As Long
|
---|
| 102 | End Function
|
---|
| 103 |
|
---|
| 104 | ' UserDomainName
|
---|
| 105 |
|
---|
| 106 | ' UserInteractive
|
---|
| 107 |
|
---|
[258] | 108 | Static Function UserName() As String
|
---|
| 109 | If Object.ReferenceEquals(userName, Nothing) Then
|
---|
| 110 | Dim buf[UNLEN] As TCHAR
|
---|
| 111 | Dim len = (UNLEN + 1) As DWord
|
---|
| 112 | GetUserName(buf, len)
|
---|
| 113 | userName = New String(buf, len As Long)
|
---|
| 114 | End If
|
---|
| 115 | Return userName
|
---|
[268] | 116 | End Function
|
---|
[142] | 117 |
|
---|
| 118 | ' Version
|
---|
| 119 |
|
---|
[258] | 120 | Public
|
---|
| 121 | 'NTでしか使用できない仕様
|
---|
[497] | 122 | Static Function WorkingSet() As SIZE_T
|
---|
[142] | 123 | Dim hmodPSAPI = LoadLibrary("PSAPI.DLL")
|
---|
| 124 | If hmodPSAPI = 0 Then Return 0
|
---|
[258] | 125 | Dim pGetProcessMemoryInfo = GetProcAddress(hmodPSAPI, ToMBStr("GetProcessMemoryInfo")) As Detail.PFNGetProcessMemoryInfo
|
---|
[142] | 126 | If pGetProcessMemoryInfo <> 0 Then
|
---|
[237] | 127 | Dim mc As PROCESS_MEMORY_COUNTERS
|
---|
[142] | 128 | If pGetProcessMemoryInfo(GetCurrentProcess(), mc, Len (mc)) <> FALSE Then
|
---|
| 129 | WorkingSet = mc.WorkingSetSize
|
---|
| 130 | End If
|
---|
| 131 | End If
|
---|
| 132 | FreeLibrary(hmodPSAPI)
|
---|
| 133 | End Function
|
---|
| 134 |
|
---|
| 135 | ' Methods
|
---|
| 136 |
|
---|
| 137 | Static Sub Exit(exitCode As Long)
|
---|
| 138 | Environment.exitCode = exitCode
|
---|
| 139 | End
|
---|
| 140 | End Sub
|
---|
| 141 |
|
---|
| 142 | Static Function ExpandEnvironmentVariables(s As String) As String
|
---|
[497] | 143 | If ActiveBasic.IsNothing(s) Then
|
---|
| 144 | Throw New ArgumentNullException("s")
|
---|
| 145 | End If
|
---|
[142] | 146 | Dim src = ToTCStr(s)
|
---|
| 147 | Dim size = ExpandEnvironmentStrings(src, 0, 0)
|
---|
[497] | 148 | Dim dst = New Text.StringBuilder
|
---|
| 149 | dst.Length = size As Long
|
---|
| 150 | ExpandEnvironmentStrings(src, StrPtr(dst), size)
|
---|
| 151 | dst.Length = (size - 1) As Long
|
---|
| 152 | ExpandEnvironmentVariables = dst.ToString
|
---|
[142] | 153 | End Function
|
---|
| 154 |
|
---|
| 155 | Static Sub FailFast(message As String)
|
---|
[208] | 156 | FatalAppExit(0, ToTCStr(message))
|
---|
[142] | 157 | End Sub
|
---|
| 158 |
|
---|
| 159 | ' GetCommandLineArgs
|
---|
| 160 |
|
---|
[268] | 161 | Static Function GetEnvironmentVariable(variable As String) As String
|
---|
[497] | 162 | If ActiveBasic.IsNothing(variable) Then
|
---|
| 163 | Throw New ArgumentNullException("variable")
|
---|
| 164 | End If
|
---|
[268] | 165 | Dim tcsVariable = ToTCStr(variable)
|
---|
| 166 | Dim size = _System_GetEnvironmentVariable(tcsVariable, 0, 0)
|
---|
[497] | 167 | Dim buf = New Text.StringBuilder
|
---|
| 168 | buf.Length = size As Long
|
---|
| 169 | buf.Length = _System_GetEnvironmentVariable(tcsVariable, StrPtr(buf), size)
|
---|
| 170 | GetEnvironmentVariable = buf.ToString
|
---|
[268] | 171 | End Function
|
---|
[142] | 172 |
|
---|
| 173 | ' GetEnvironmentVariables
|
---|
| 174 |
|
---|
[258] | 175 | Static Function GetFolderPath(f As Environment_SpecialFolder) As String
|
---|
| 176 | ' If ... Then
|
---|
| 177 | ' Throw New ArgumentException
|
---|
| 178 | ' End If
|
---|
| 179 | Dim x As Long
|
---|
| 180 | x = f
|
---|
| 181 | Return ActiveBasic.Windows.GetFolderPath(x)
|
---|
| 182 | End Function
|
---|
| 183 |
|
---|
[142] | 184 | ' GetLogicalDrives
|
---|
| 185 |
|
---|
[268] | 186 | Static Sub SetEnvironmentVariable(variable As String, value As String)
|
---|
[497] | 187 | If ActiveBasic.IsNothing(variable) Then
|
---|
| 188 | Throw New ArgumentNullException("variable")
|
---|
| 189 | End If
|
---|
[268] | 190 | _System_SetEnvironmentVariable(ToTCStr(variable), ToTCStr(value))
|
---|
| 191 | End Sub
|
---|
[142] | 192 |
|
---|
| 193 | Private
|
---|
[152] | 194 | Static cmdLine = Nothing As String
|
---|
[142] | 195 | Static exitCode = 0 As Long
|
---|
[258] | 196 | Static machineName = Nothing As String
|
---|
[142] | 197 | Static osVer = Nothing As OperatingSystem
|
---|
| 198 | Static processorCount = 0 As Long
|
---|
| 199 | Static sysDir = Nothing As String
|
---|
[258] | 200 | Static userName = Nothing As String
|
---|
[142] | 201 | End Class
|
---|
[258] | 202 |
|
---|
| 203 | Enum Environment_SpecialFolder
|
---|
| 204 | Desktop = CSIDL_DESKTOP
|
---|
| 205 | Programs = CSIDL_PROGRAMS
|
---|
| 206 | Personal = CSIDL_PERSONAL
|
---|
| 207 | MyDocuments = CSIDL_PERSONAL
|
---|
| 208 | Favorites = CSIDL_FAVORITES
|
---|
| 209 | Startup = CSIDL_STARTUP
|
---|
| 210 | Recent = CSIDL_RECENT
|
---|
| 211 | SendTo = CSIDL_SENDTO
|
---|
| 212 | StartMenu = CSIDL_STARTMENU
|
---|
| 213 | MyMusic = CSIDL_MYMUSIC
|
---|
| 214 | DesktopDirectory = CSIDL_DESKTOPDIRECTORY
|
---|
| 215 | MyComputer = CSIDL_DRIVES
|
---|
| 216 | Templates = CSIDL_TEMPLATES
|
---|
| 217 | ApplicationData = CSIDL_APPDATA '4.71
|
---|
| 218 | LocalApplicationData = CSIDL_LOCAL_APPDATA
|
---|
| 219 | InternetCache = CSIDL_INTERNET_CACHE
|
---|
| 220 | Cookies = CSIDL_COOKIES
|
---|
| 221 | History = CSIDL_HISTORY
|
---|
| 222 | CommonApplicationData = CSIDL_COMMON_APPDATA '5.0
|
---|
| 223 | System = CSIDL_SYSTEM
|
---|
| 224 | CommonProgramFiles = CSIDL_PROGRAM_FILES
|
---|
| 225 | ProgramFiles = CSIDL_PROGRAM_FILES
|
---|
| 226 | MyPictures = CSIDL_MYPICTURES
|
---|
| 227 | End Enum
|
---|
| 228 |
|
---|
| 229 | End Namespace 'System
|
---|