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

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

[601][602]でのコミットし忘れ分と細かい修正

File size: 5.8 KB
RevLine 
[142]1' System/Environment.ab
2
[237]3#require <api_psapi.sbp>
[142]4
[268]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
[258]8Namespace System
9
10Namespace Detail
11 TypeDef PFNGetProcessMemoryInfo = *Function(Process As HANDLE, ByRef mc As PROCESS_MEMORY_COUNTERS, cb As DWord) As BOOL
[268]12
13 Dim hasShutdownStarted As Boolean
[258]14End Namespace
15
[142]16Class Environment
17Public
18 ' Properties
19
20 Static Function CommandLine() As String
[497]21#ifdef UNICODE
[583]22 CommandLine = New String(GetCommandLineW())
[497]23#else
[583]24 CommandLine = New String(GetCommandLineA())
[142]25#endif
26 End Function
27
28 Static Function CurrentDirectory() As String
29 Dim size = GetCurrentDirectory(0, 0)
[583]30 Dim buf = New Text.StringBuilder
31 buf.Length = size As Long
32 Dim len = GetCurrentDirectory(size, StrPtr(buf))
[208]33 If len < size Then
[583]34 CurrentDirectory = buf.ToString
[208]35 End If
[142]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
[173]50 Static Function HasShutdownStarted() As Boolean
[268]51 Return Detail.hasShutdownStarted
[173]52 End Function
[142]53
[258]54 Static Function MachineName() As String
[583]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
[268]63 End Function
[142]64
65 Static Function NewLine() As String
66 Return Ex"\r\n"
67 End Function
68
69 Static Function OSVersion() As OperatingSystem
[583]70 Dim vi As OSVERSIONINFO
[603]71 vi.dwOSVersionInfoSize = Len(vi)
[583]72 GetVersionEx(vi)
73 OSVersion = New OperatingSystem(vi)
[142]74 End Function
75
76 Static Function ProcessorCount() As Long
[583]77 Dim si As SYSTEM_INFO
78 GetSystemInfo(si)
79 ProcessorCount = si.dwNumberOfProcessors
[142]80 End Function
81
82 ' StackTrace
83
84 Static Function SystemDirectory() As String
[583]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
[586]91 SystemDirectory = .ToString
[583]92 End With
[142]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
[258]103 Static Function UserName() As String
[583]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
[268]112 End Function
[142]113
114 ' Version
115
[258]116Public
117 'NTでしか使用できない仕様
[497]118 Static Function WorkingSet() As SIZE_T
[142]119 Dim hmodPSAPI = LoadLibrary("PSAPI.DLL")
120 If hmodPSAPI = 0 Then Return 0
[258]121 Dim pGetProcessMemoryInfo = GetProcAddress(hmodPSAPI, ToMBStr("GetProcessMemoryInfo")) As Detail.PFNGetProcessMemoryInfo
[142]122 If pGetProcessMemoryInfo <> 0 Then
[237]123 Dim mc As PROCESS_MEMORY_COUNTERS
[142]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
[497]139 If ActiveBasic.IsNothing(s) Then
140 Throw New ArgumentNullException("s")
141 End If
[142]142 Dim src = ToTCStr(s)
143 Dim size = ExpandEnvironmentStrings(src, 0, 0)
[497]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
[142]149 End Function
150
151 Static Sub FailFast(message As String)
[208]152 FatalAppExit(0, ToTCStr(message))
[142]153 End Sub
154
155 ' GetCommandLineArgs
156
[268]157 Static Function GetEnvironmentVariable(variable As String) As String
[497]158 If ActiveBasic.IsNothing(variable) Then
159 Throw New ArgumentNullException("variable")
160 End If
[268]161 Dim tcsVariable = ToTCStr(variable)
162 Dim size = _System_GetEnvironmentVariable(tcsVariable, 0, 0)
[497]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
[268]167 End Function
[142]168
169 ' GetEnvironmentVariables
170
[258]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
[142]180 ' GetLogicalDrives
181
[268]182 Static Sub SetEnvironmentVariable(variable As String, value As String)
[497]183 If ActiveBasic.IsNothing(variable) Then
184 Throw New ArgumentNullException("variable")
185 End If
[268]186 _System_SetEnvironmentVariable(ToTCStr(variable), ToTCStr(value))
187 End Sub
[142]188
189Private
190 Static exitCode = 0 As Long
191End Class
[258]192
193Enum 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
217End Enum
218
219End Namespace 'System
Note: See TracBrowser for help on using the repository browser.