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