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