source: trunk/ab5.0/ablib/src/Classes/System/Environment.ab@ 531

Last change on this file since 531 was 497, checked in by イグトランス (egtra), 16 years ago

インクルードガードとその他不要な前処理定義などの削除

File size: 6.3 KB
Line 
1' System/Environment.ab
2
3#require <api_psapi.sbp>
4
5Declare Function _System_SetEnvironmentVariable Lib "kernel32" Alias _FuncName_SetEnvironmentVariable (lpName As LPCTSTR, lpValue As LPTSTR) As BOOL
6Declare Function _System_GetEnvironmentVariable Lib "kernel32" Alias _FuncName_GetEnvironmentVariable (lpName As PCTSTR, lpBuffer As PTSTR, nSize As DWord) As DWord
7
8Namespace System
9
10Namespace 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
14End Namespace
15
16Class Environment
17Public
18 ' Properties
19
20 Static Function CommandLine() As String
21 If Object.ReferenceEquals(cmdLine, Nothing) Then
22#ifdef UNICODE
23 cmdLine = New String(GetCommandLineW())
24#else
25 cmdLine = New String(GetCommandLineA())
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 = GC_malloc_atomic(SizeOf (TCHAR) * size) As PCTSTR
34 Dim len = GetCurrentDirectory(size, p)
35 If len < size Then
36 CurrentDirectory = New String(p, len As Long)
37 End If
38 End Function
39
40 Static Sub CurrentDirectory(cd As String)
41 SetCurrentDirectory(ToTCStr(cd))
42 End Sub
43
44 Static Function ExitCode() As Long
45 Return exitCode
46 End Function
47
48 Static Sub ExitCode(code As Long)
49 exitCode = code
50 End Sub
51
52 Static Function HasShutdownStarted() As Boolean
53 Return Detail.hasShutdownStarted
54 End Function
55
56 Static Function MachineName() As String
57 If Object.ReferenceEquals(machineName, Nothing) Then
58 Dim buf[MAX_COMPUTERNAME_LENGTH] As TCHAR
59 Dim len = (MAX_COMPUTERNAME_LENGTH + 1) As DWord
60 GetComputerName(buf, len)
61 machineName = New String(buf, len As Long)
62 End If
63 Return machineName
64 End Function
65
66 Static Function NewLine() As String
67 Return Ex"\r\n"
68 End Function
69
70 Static Function OSVersion() As OperatingSystem
71 If Object.ReferenceEquals(osVer, Nothing) Then
72 Dim vi As OSVERSIONINFO
73 GetVersionEx(vi)
74 osVer = New OperatingSystem(vi)
75 End If
76 Return osVer
77 End Function
78
79 Static Function ProcessorCount() As Long
80 If processorCount = 0 Then
81 Dim si As SYSTEM_INFO
82 GetSystemInfo(si)
83 processorCount = si.dwNumberOfProcessors
84 End If
85 Return processorCount
86 End Function
87
88 ' StackTrace
89
90 Static Function SystemDirectory() As String
91 If Object.ReferenceEquals(sysDir, Nothing) Then
92 Dim size = GetSystemDirectory(0, 0)
93 Dim p = GC_malloc_atomic(SizeOf (TCHAR) * size) As PTSTR
94 Dim len = GetSystemDirectory(p, size)
95 sysDir = New String(p, len As Long)
96 End If
97 Return sysDir
98 End Function
99
100 Static Function TickCount() As Long
101 Return GetTickCount() As Long
102 End Function
103
104 ' UserDomainName
105
106 ' UserInteractive
107
108 Static Function UserName() As String
109 If Object.ReferenceEquals(userName, Nothing) Then
110 Dim buf[UNLEN] As TCHAR
111 Dim len = (UNLEN + 1) As DWord
112 GetUserName(buf, len)
113 userName = New String(buf, len As Long)
114 End If
115 Return userName
116 End Function
117
118 ' Version
119
120Public
121 'NTでしか使用できない仕様
122 Static Function WorkingSet() As SIZE_T
123 Dim hmodPSAPI = LoadLibrary("PSAPI.DLL")
124 If hmodPSAPI = 0 Then Return 0
125 Dim pGetProcessMemoryInfo = GetProcAddress(hmodPSAPI, ToMBStr("GetProcessMemoryInfo")) As Detail.PFNGetProcessMemoryInfo
126 If pGetProcessMemoryInfo <> 0 Then
127 Dim mc As PROCESS_MEMORY_COUNTERS
128 If pGetProcessMemoryInfo(GetCurrentProcess(), mc, Len (mc)) <> FALSE Then
129 WorkingSet = mc.WorkingSetSize
130 End If
131 End If
132 FreeLibrary(hmodPSAPI)
133 End Function
134
135 ' Methods
136
137 Static Sub Exit(exitCode As Long)
138 Environment.exitCode = exitCode
139 End
140 End Sub
141
142 Static Function ExpandEnvironmentVariables(s As String) As String
143 If ActiveBasic.IsNothing(s) Then
144 Throw New ArgumentNullException("s")
145 End If
146 Dim src = ToTCStr(s)
147 Dim size = ExpandEnvironmentStrings(src, 0, 0)
148 Dim dst = New Text.StringBuilder
149 dst.Length = size As Long
150 ExpandEnvironmentStrings(src, StrPtr(dst), size)
151 dst.Length = (size - 1) As Long
152 ExpandEnvironmentVariables = dst.ToString
153 End Function
154
155 Static Sub FailFast(message As String)
156 FatalAppExit(0, ToTCStr(message))
157 End Sub
158
159 ' GetCommandLineArgs
160
161 Static Function GetEnvironmentVariable(variable As String) As String
162 If ActiveBasic.IsNothing(variable) Then
163 Throw New ArgumentNullException("variable")
164 End If
165 Dim tcsVariable = ToTCStr(variable)
166 Dim size = _System_GetEnvironmentVariable(tcsVariable, 0, 0)
167 Dim buf = New Text.StringBuilder
168 buf.Length = size As Long
169 buf.Length = _System_GetEnvironmentVariable(tcsVariable, StrPtr(buf), size)
170 GetEnvironmentVariable = buf.ToString
171 End Function
172
173 ' GetEnvironmentVariables
174
175 Static Function GetFolderPath(f As Environment_SpecialFolder) As String
176' If ... Then
177' Throw New ArgumentException
178' End If
179 Dim x As Long
180 x = f
181 Return ActiveBasic.Windows.GetFolderPath(x)
182 End Function
183
184 ' GetLogicalDrives
185
186 Static Sub SetEnvironmentVariable(variable As String, value As String)
187 If ActiveBasic.IsNothing(variable) Then
188 Throw New ArgumentNullException("variable")
189 End If
190 _System_SetEnvironmentVariable(ToTCStr(variable), ToTCStr(value))
191 End Sub
192
193Private
194 Static cmdLine = Nothing As String
195 Static exitCode = 0 As Long
196 Static machineName = Nothing As String
197 Static osVer = Nothing As OperatingSystem
198 Static processorCount = 0 As Long
199 Static sysDir = Nothing As String
200 Static userName = Nothing As String
201End Class
202
203Enum Environment_SpecialFolder
204 Desktop = CSIDL_DESKTOP
205 Programs = CSIDL_PROGRAMS
206 Personal = CSIDL_PERSONAL
207 MyDocuments = CSIDL_PERSONAL
208 Favorites = CSIDL_FAVORITES
209 Startup = CSIDL_STARTUP
210 Recent = CSIDL_RECENT
211 SendTo = CSIDL_SENDTO
212 StartMenu = CSIDL_STARTMENU
213 MyMusic = CSIDL_MYMUSIC
214 DesktopDirectory = CSIDL_DESKTOPDIRECTORY
215 MyComputer = CSIDL_DRIVES
216 Templates = CSIDL_TEMPLATES
217 ApplicationData = CSIDL_APPDATA '4.71
218 LocalApplicationData = CSIDL_LOCAL_APPDATA
219 InternetCache = CSIDL_INTERNET_CACHE
220 Cookies = CSIDL_COOKIES
221 History = CSIDL_HISTORY
222 CommonApplicationData = CSIDL_COMMON_APPDATA '5.0
223 System = CSIDL_SYSTEM
224 CommonProgramFiles = CSIDL_PROGRAM_FILES
225 ProgramFiles = CSIDL_PROGRAM_FILES
226 MyPictures = CSIDL_MYPICTURES
227End Enum
228
229End Namespace 'System
Note: See TracBrowser for help on using the repository browser.