xlang v4.0 Release
程序设计语言基础库文档
载入中...
搜索中...
未找到
Runtime.xcs
浏览该文件的文档.
1//xlang
2
3package Runtime {
4
5 class rtlp{
6 public static int rtl_osid = _system_.getPlatformId();
7 };
8
9 class UnixlikeApi : Library{
10 static bool loaded = false;
11
12 public static bool load_linux() {
13 try {
14 loadLibrary("libc.so.6");
15 return true;
16 } catch(Exception e) {
18
19 try {
20 loadLibrary("libc.so");
21 return true;
22 } catch(Exception e) {
24 }
25 }
26
27 return false;
28 }
29
30 public static bool load() {
31 if (loaded == false) {
32 if (rtlp.rtl_osid == 1) {
33 loaded = load_linux();
34
35 if (loaded == false) {
36 _system_.output("can not load libc");
37 }
38 } else {
39 try {
40 loadLibrary("libc.dylib");
41 loaded = true;
42 } catch(Exception e) {
44 }
45 }
46 }
47
48 return loaded;
49 }
50
51 public import{
52 Pointer cdecl readlink (String __path,
53 ObjectPtr __buf, Pointer __len);
54 int cdecl kill(int pid, int sig);
55 };
56 };
57 class Win32Api{
58
59 public static class Psapi32 : Library{
60
61 public static bool loaded = false;
62
63 public static bool load() {
64 if (loaded == false) {
65 try {
66 loadLibrary("psapi.dll");
67 loaded = true;
68 } catch(Exception e) {
69
70 }
71 }
72
73 return loaded;
74 }
75
76 public import {
77 int
78 stdcall
79 GetProcessImageFileNameA(
80 Pointer hProcess,
81 ObjectPtr lpExeName,
82 ObjectPtr lpdwSize
83 );
84
85 bool stdcall EnumProcessModules(
86 Pointer hProcess,
87 ObjectPtr lphModule,
88 int cb,
89 ObjectPtr lpcbNeeded
90 );
91
93 Pointer hProcess,
94 Pointer hModule,
95 ObjectPtr lpFilename,
96 int nSize
97 );
98 };
99 };
100
101 public static class Kernel32 : Library{
102
103 public static bool loaded = false;
104
105 public static bool load() {
106 if (loaded == false) {
107 try {
108 loadLibrary("kernel32.dll");
109 loaded = true;
110 } catch(Exception e) {
111
112 }
113 }
114
115 return loaded;
116 }
117
118 public static const int MAX_PATH = 260,
119 TH32CS_SNAPHEAPLIST = 1,
120 TH32CS_SNAPPROCESS = 2,
121 TH32CS_SNAPTHREAD = 4,
122 TH32CS_SNAPMODULE = 8,
123 TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE;
124
125 public import {
126 Pointer stdcall CreateToolhelp32Snapshot(
127 int dwFlags,
128 int th32ProcessID
129 );
130
131 //结构体356个字节
132 bool stdcall Process32First(
133 Pointer hSnapshot,
134 ObjectPtr lppe
135 );
136
137 bool stdcall Process32Next(
138 Pointer hSnapshot,
139 ObjectPtr lppe
140 );
141
142 int stdcall lstrlenA(
143 ObjectPtr lpString
144 );
145
146 bool stdcall CloseHandle(
147 Pointer hObject
148 );
149
151 ObjectPtr hProcess,
152 int dwFlags,
153 ObjectPtr lpExeName,
154 ObjectPtr lpdwSize
155 );
156
157 Pointer stdcall OpenProcess(
158 int dwDesiredAccess,
159 bool bInheritHandle,
160 int dwProcessId);
161
162 bool stdcall TerminateProcess(
163 Pointer hProcess,
164 int uExitCode
165 );
166 };
167 };
168 };
169
170
171 public class OSProcess{
172 public OSProcess() {
173 }
174 public OSProcess(int Id) {
175 processId = Id;
176 }
178 for (int i = 0; i < 4; i ++) {
179 processId = (processId << 8) | data[8 + 3 - i];
180 }
181
182 byte[] paths = new byte[Win32Api.Kernel32.MAX_PATH];
183
184 _system_.arrayCopy(data, _system_.getOSBit() == 32 ? 36 : 44, paths, 0, Win32Api.Kernel32.MAX_PATH);
185 int len = Win32Api.Kernel32.lstrlenA(paths);
186 name = new String(paths, 0, len);
187 }
188
192
193 public int getId() {
194 return processId;
195 }
196
197 public String getName() {
198 return name;
199 }
200
201 public String getPath() {
202 //PROCESS_QUERY_INFORMATION
203 if (exepath == nilptr) {
204
205 if (rtlp.rtl_osid == 0) {
206 if (false == Win32Api.Psapi32.load()) {
207 return nilptr;
208 }
209
210 long handle = open();
211
212 if (handle > 0) {
213 byte [] path = new byte[Win32Api.Kernel32.MAX_PATH];
214 int [] pl = new int[1];
215 long [] hm = new long[1];
216
217 pl[0] = Win32Api.Kernel32.MAX_PATH;
218 int up = 0;
219
220 if (Win32Api.Psapi32.EnumProcessModules(handle, hm, 8, up)) {
221 int len = Win32Api.Psapi32.GetModuleFileNameExA(handle, hm[0], path, Win32Api.Kernel32.MAX_PATH);
222
223 if (len > 0) {
224 try {
225 exepath = new String(path, 0, len);
226 } catch(Exception e) {
227
228 }
229 }
230 }
231
232 closeHandle(handle);
233 }
234 }
235 }
236
237 return exepath;
238 }
239
240 public long open() {
241 return Win32Api.Kernel32.OpenProcess(0x001fffff, false, processId);
242 }
243
244 public void closeHandle(long handle) {
245 Win32Api.Kernel32.CloseHandle(handle);
246 }
247 public long open(int flag, bool bi) {
248 return Win32Api.Kernel32.OpenProcess(flag, bi, processId);
249 }
250
251 public bool kill(int code) {
252 bool r = false;
253
254 if (rtlp.rtl_osid == 0) {
255
256 long h = open();
257
258 if (h > 0) {
259 r = Win32Api.Kernel32.TerminateProcess(h, code);
260 Win32Api.Kernel32.CloseHandle(h);
261 }
262 } else {
263 r = 0 == UnixlikeApi.kill(processId, 15);
264 }
265
266 return r;
267 }
268
269 public static
271 int osid = _system_.getPlatformId();
272
273 if (osid == 0) {
274 return getProcessListWin32();
275 } else if (osid == 1 || osid == 2) {
276 return getProcessListUnix();
277 }
278
279 return nilptr;
280 }
281
282 static
284 List<OSProcess> plist = nilptr;
285
286 if (false == UnixlikeApi.load()) {
287 return nilptr;
288 }
289
290 plist = new List<OSProcess>();
291
292 try {
293 File file = new File ("/proc");
294 long hfind = file.open();
295
296 if (hfind != 0) {
297
298 File pf = new File();
299
300 while (file.find(hfind, pf)) {
301 String pname = pf.getName();
302
303 if (pname != nilptr && pname.isDigit()) {
304 OSProcess process = new OSProcess();
305 String fppath = pf.getPath();
306
307 if (fppath != nilptr) {
308 String namepath = fppath.appendPath("status");
309
310 try {
312
313 String txtstatus = "";
314
315 byte []status = new byte[1024];
316 int rd = 0;
317
318 while ((rd = fis.read(status, 0, 1024)) > 0) {
319 txtstatus = txtstatus + new String(status, 0, rd);
320 }
321
322 fis.close();
323
324 int pos = txtstatus.indexOf("Name:");
325
326 if (pos != -1) {
327 int le = txtstatus.indexOf("\n", pos);
328
329 if (le == -1) {
330 le = txtstatus.length();
331 }
332
333 process.name = txtstatus.substring(pos + 5, le).trim(true);
334 }
335 } catch(Exception e) {
336
337 }
338
339 String expath = fppath.appendPath("exe");
340
341 byte [] fullpath = new byte[4096];
342 long length = UnixlikeApi.readlink(expath, fullpath, (long)4096);
343
344 if (length != -1) {
345 process.exepath = new String (fullpath, 0, (int)length);
346 }
347
348 process.processId = pname.parseInt();
349 plist.add(process);
350 }
351 }
352 }
353
354 file.close(hfind);
355 }
356 } catch(Exception e) {
357
358 }
359
360 return plist;
361 }
362
363 static
365 List<OSProcess> plist = nilptr;
366
367 if (false == Win32Api.Kernel32.load()) {
368 return nilptr;
369 }
370
371 byte[] struct = new byte[356];
372 struct[0] = 08;
373 struct[1] = 12;
374 plist = new List<OSProcess>();
375
376 long snopt = Win32Api.Kernel32.CreateToolhelp32Snapshot(
377 Win32Api.Kernel32.TH32CS_SNAPALL,
378 0);
379
380 try {
381 if (snopt > 0) {
382 bool succ = Win32Api.Kernel32.Process32First(snopt, struct);
383
384 while (succ) {
385 plist.add(new OSProcess(struct));
386 succ = Win32Api.Kernel32.Process32Next(snopt, struct);
387 }
388
389 Win32Api.Kernel32.CloseHandle(snopt);
390 }
391 } catch(Exception e) {
392
393 }
394
395 return plist;
396 }
397 };
398};
399
400/*
401package System{
402 public class out{
403 public static int println(String text){
404 return _system_.consoleWrite(text + "\n");
405 }
406 public static int print(String text){
407 return _system_.consoleWrite(text);
408 }
409 };
410
411 public class Console{
412 public Console(){
413 _system_.createConsole();
414 }
415 };
416};
417
418
419using { System; };
420
421
422int main(String [] args){
423
424 new Console();
425
426 List<Runtime.OSProcess> lists = Runtime.OSProcess.listProcesses();
427
428 List.Iterator<Runtime.OSProcess> iter = lists.iterator();
429
430 while (iter.hasNext()){
431 Runtime.OSProcess p = iter.next();
432 System.out.println(p.getName() + "[" + p.getId() +"] " + p.getPath());
433 }
434
435 Thread.sleep(-1);
436
437 return 0;
438}
439*/
系统和IO相关
static final int getPlatformId()
static final void output(String)
static final void arrayCopy(Object, long, Object, long, long)
static final int getOSBit()
数组储存异常
异常类
final String getMessage()
String getPath()
String getName()
long open()
bool find(long, File)
void close(long)
void close() override
Definition FileStream.x:119
static final void loadLibrary(String)
map容器
Definition List.xcs:6
void add(T)
void closeHandle(long handle)
Definition Runtime.xcs:244
static List<OSProcess> getProcessListUnix()
Definition Runtime.xcs:283
OSProcess(byte[] data)
Definition Runtime.xcs:177
String getName()
Definition Runtime.xcs:197
static List<OSProcess> getProcessListWin32()
Definition Runtime.xcs:364
static List<OSProcess> listProcesses()
Definition Runtime.xcs:270
String getPath()
Definition Runtime.xcs:201
OSProcess(int Id)
Definition Runtime.xcs:174
bool kill(int code)
Definition Runtime.xcs:251
long open(int flag, bool bi)
Definition Runtime.xcs:247
static bool load_linux()
Definition Runtime.xcs:12
static bool load()
Definition Runtime.xcs:30
bool stdcall CloseHandle( Pointer hObject )
Pointer stdcall OpenProcess( int dwDesiredAccess, bool bInheritHandle, int dwProcessId)
int stdcall lstrlenA( ObjectPtr lpString )
bool stdcall QueryFullProcessImageNameA( ObjectPtr hProcess, int dwFlags, ObjectPtr lpExeName, ObjectPtr lpdwSize )
static bool load()
Definition Runtime.xcs:105
bool stdcall TerminateProcess( Pointer hProcess, int uExitCode )
bool stdcall Process32Next( Pointer hSnapshot, ObjectPtr lppe )
bool stdcall Process32First( Pointer hSnapshot, ObjectPtr lppe )
bool stdcall EnumProcessModules( Pointer hProcess, ObjectPtr lphModule, int cb, ObjectPtr lpcbNeeded )
int stdcall GetModuleFileNameExA( Pointer hProcess, Pointer hModule, ObjectPtr lpFilename, int nSize )
static bool load()
Definition Runtime.xcs:63
字符串类
bool isDigit()
String appendPath(String)
int parseInt()