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