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

Last change on this file since 667 was 667, checked in by NoWest, 15 years ago

Environment.GetLogicalDrivesメソッド内でPCWSTRとPCSTRを分けていましたが、PCTSTRに統一しました。

File size: 6.6 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#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
116Public
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 /*!
181 @brief コンピュータの論理ドライブの名前を格納している文字列の配列を取得
182 @author NoWest
183 @date 2008/12/28
184 */
185 Static Function GetLogicalDrives() As System.Collections.Generic.IList<String>
186 Dim ret = New System.Collections.Generic.List<String>
187
188 Dim buf As *Char
189 buf = GC_malloc( 256*SizeOf(Char) )
190 Dim length = GetLogicalDriveStrings( 256, buf )
191 If length > 256 Then
192 buf = realloc(buf, length * SizeOf(Char) )
193 length = GetLogicalDriveStrings( length, buf )
194 ElseIf length = 0 Then
195 Return ret
196 End If
197 Dim strbuf = New System.String( buf As PCTSTR, length As Long )
198 Dim startpos = 0 As Long
199 length = 3
200 While True
201 ret.Add( strbuf.Substring(startpos,length) )
202 startpos += (length+1)
203 If startpos > strbuf.Length()-1 Then Return ret
204 Wend
205 End Function
206
207 Static Sub SetEnvironmentVariable(variable As String, value As String)
208 If ActiveBasic.IsNothing(variable) Then
209 Throw New ArgumentNullException("variable")
210 End If
211 _System_SetEnvironmentVariable(ToTCStr(variable), ToTCStr(value))
212 End Sub
213
214Private
215 Static exitCode = 0 As Long
216End Class
217
218Enum Environment_SpecialFolder
219 Desktop = CSIDL_DESKTOP
220 Programs = CSIDL_PROGRAMS
221 Personal = CSIDL_PERSONAL
222 MyDocuments = CSIDL_PERSONAL
223 Favorites = CSIDL_FAVORITES
224 Startup = CSIDL_STARTUP
225 Recent = CSIDL_RECENT
226 SendTo = CSIDL_SENDTO
227 StartMenu = CSIDL_STARTMENU
228 MyMusic = CSIDL_MYMUSIC
229 DesktopDirectory = CSIDL_DESKTOPDIRECTORY
230 MyComputer = CSIDL_DRIVES
231 Templates = CSIDL_TEMPLATES
232 ApplicationData = CSIDL_APPDATA '4.71
233 LocalApplicationData = CSIDL_LOCAL_APPDATA
234 InternetCache = CSIDL_INTERNET_CACHE
235 Cookies = CSIDL_COOKIES
236 History = CSIDL_HISTORY
237 CommonApplicationData = CSIDL_COMMON_APPDATA '5.0
238 System = CSIDL_SYSTEM
239 CommonProgramFiles = CSIDL_PROGRAM_FILES
240 ProgramFiles = CSIDL_PROGRAM_FILES
241 MyPictures = CSIDL_MYPICTURES
242End Enum
243
244End Namespace 'System
Note: See TracBrowser for help on using the repository browser.