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