| 1 | ' System/Environment.ab
|
|---|
| 2 |
|
|---|
| 3 | #require <api_psapi.sbp>
|
|---|
| 4 |
|
|---|
| 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 |
|
|---|
| 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
|
|---|
| 12 |
|
|---|
| 13 | Dim hasShutdownStarted As Boolean
|
|---|
| 14 | End Namespace
|
|---|
| 15 |
|
|---|
| 16 | Class Environment
|
|---|
| 17 | Public
|
|---|
| 18 | ' Properties
|
|---|
| 19 |
|
|---|
| 20 | Static Function CommandLine() As String
|
|---|
| 21 | CommandLine = New String(GetCommandLine())
|
|---|
| 22 | End Function
|
|---|
| 23 |
|
|---|
| 24 | Static Function CurrentDirectory() As String
|
|---|
| 25 | Dim size = GetCurrentDirectory(0, 0)
|
|---|
| 26 | Dim buf = New Text.StringBuilder
|
|---|
| 27 | buf.Length = size As Long
|
|---|
| 28 | Dim len = GetCurrentDirectory(size, StrPtr(buf))
|
|---|
| 29 | If len < size Then
|
|---|
| 30 | CurrentDirectory = buf.ToString
|
|---|
| 31 | End If
|
|---|
| 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 |
|
|---|
| 46 | Static Function HasShutdownStarted() As Boolean
|
|---|
| 47 | Return Detail.hasShutdownStarted
|
|---|
| 48 | End Function
|
|---|
| 49 |
|
|---|
| 50 | Static Function MachineName() As String
|
|---|
| 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
|
|---|
| 59 | End Function
|
|---|
| 60 |
|
|---|
| 61 | Static Function NewLine() As String
|
|---|
| 62 | Return Ex"\r\n"
|
|---|
| 63 | End Function
|
|---|
| 64 |
|
|---|
| 65 | Static Function OSVersion() As OperatingSystem
|
|---|
| 66 | OSVersion = New OperatingSystem(ActiveBasic.Windows.Version.Detail.vi)
|
|---|
| 67 | End Function
|
|---|
| 68 |
|
|---|
| 69 | Static Function ProcessorCount() As Long
|
|---|
| 70 | Dim si As SYSTEM_INFO
|
|---|
| 71 | GetSystemInfo(si)
|
|---|
| 72 | ProcessorCount = si.dwNumberOfProcessors
|
|---|
| 73 | End Function
|
|---|
| 74 |
|
|---|
| 75 | ' StackTrace
|
|---|
| 76 |
|
|---|
| 77 | Static Function SystemDirectory() As String
|
|---|
| 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
|
|---|
| 84 | SystemDirectory = .ToString
|
|---|
| 85 | End With
|
|---|
| 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 |
|
|---|
| 96 | Static Function UserName() As String
|
|---|
| 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
|
|---|
| 105 | End Function
|
|---|
| 106 |
|
|---|
| 107 | ' Version
|
|---|
| 108 |
|
|---|
| 109 | Public
|
|---|
| 110 | 'NTでしか使用できない仕様
|
|---|
| 111 | Static Function WorkingSet() As SIZE_T
|
|---|
| 112 | Dim hmodPSAPI = LoadLibrary("PSAPI.DLL")
|
|---|
| 113 | If hmodPSAPI = 0 Then Return 0
|
|---|
| 114 | Dim pGetProcessMemoryInfo = GetProcAddress(hmodPSAPI, ToMBStr("GetProcessMemoryInfo")) As Detail.PFNGetProcessMemoryInfo
|
|---|
| 115 | If pGetProcessMemoryInfo <> 0 Then
|
|---|
| 116 | Dim mc As PROCESS_MEMORY_COUNTERS
|
|---|
| 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
|
|---|
| 132 | If ActiveBasic.IsNothing(s) Then
|
|---|
| 133 | Throw New ArgumentNullException("s")
|
|---|
| 134 | End If
|
|---|
| 135 | Dim src = ToTCStr(s)
|
|---|
| 136 | Dim size = ExpandEnvironmentStrings(src, 0, 0)
|
|---|
| 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 | End Function
|
|---|
| 143 |
|
|---|
| 144 | Static Sub FailFast(message As String)
|
|---|
| 145 | FatalAppExit(0, ToTCStr(message))
|
|---|
| 146 | End Sub
|
|---|
| 147 |
|
|---|
| 148 | ' GetCommandLineArgs
|
|---|
| 149 |
|
|---|
| 150 | Static Function GetEnvironmentVariable(variable As String) As String
|
|---|
| 151 | If ActiveBasic.IsNothing(variable) Then
|
|---|
| 152 | Throw New ArgumentNullException("variable")
|
|---|
| 153 | End If
|
|---|
| 154 | Dim tcsVariable = ToTCStr(variable)
|
|---|
| 155 | Dim size = _System_GetEnvironmentVariable(tcsVariable, 0, 0)
|
|---|
| 156 | Dim buf = New Text.StringBuilder
|
|---|
| 157 | buf.Length = size As Long
|
|---|
| 158 | buf.Length = _System_GetEnvironmentVariable(tcsVariable, StrPtr(buf), size)
|
|---|
| 159 | GetEnvironmentVariable = buf.ToString
|
|---|
| 160 | End Function
|
|---|
| 161 |
|
|---|
| 162 | ' GetEnvironmentVariables
|
|---|
| 163 |
|
|---|
| 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 |
|
|---|
| 173 | /*!
|
|---|
| 174 | @brief コンピュータの論理ドライブの名前を格納している文字列の配列を取得
|
|---|
| 175 | @author NoWest
|
|---|
| 176 | @date 2008/12/28
|
|---|
| 177 | */
|
|---|
| 178 | Static Function GetLogicalDrives() As System.Collections.Generic.IList<String>
|
|---|
| 179 | Dim ret = New System.Collections.Generic.List<String>
|
|---|
| 180 |
|
|---|
| 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
|
|---|
| 190 | Dim strbuf = New System.String( buf As PCTSTR, length As Long )
|
|---|
| 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 |
|
|---|
| 200 | Static Sub SetEnvironmentVariable(variable As String, value As String)
|
|---|
| 201 | If ActiveBasic.IsNothing(variable) Then
|
|---|
| 202 | Throw New ArgumentNullException("variable")
|
|---|
| 203 | End If
|
|---|
| 204 | _System_SetEnvironmentVariable(ToTCStr(variable), ToTCStr(value))
|
|---|
| 205 | End Sub
|
|---|
| 206 |
|
|---|
| 207 | Private
|
|---|
| 208 | Static exitCode = 0 As Long
|
|---|
| 209 | End Class
|
|---|
| 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
|
|---|