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