source: Include/Classes/System/Environment.ab@ 258

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

Prompt.sbp内を名前空間に入れた。EnvironmentのMachineName, UserName, GetFolderPathを実装。

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