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

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

修正ミス(MachineNameSystemDirectory)

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