xlang v5.1 Release
程序设计语言基础库文档
载入中...
搜索中...
未找到
Runtime.xcs
浏览该文件的文档.
1//xlang
2@SuppressWarnings
3@Version(value = "1.0.0.11")
4@Compiler(version = ">=4.0") // 要求编译器版本大于等于4.0
5package Runtime {
6
7 public class rtlp{
8 public static int rtl_osid = _system_.getPlatformId();
9 };
10
11 public class UnixlikeApi : Library{
12 static bool loaded = false;
13
14 public static bool load_linux() {
15 try {
16 loadLibrary("libc.so.6");
17 return true;
18 } catch(Exception e) {
20
21 try {
22 loadLibrary("libc.so");
23 return true;
24 } catch(Exception e) {
26 }
27 }
28
29 return false;
30 }
31
32 public static bool load() {
33 if (loaded == false) {
34 if (rtlp.rtl_osid == 1) {
35 loaded = load_linux();
36
37 if (loaded == false) {
38 _system_.output("can not load libc");
39 }
40 } else {
41 try {
42 loadLibrary("libc.dylib");
43 loaded = true;
44 } catch(Exception e) {
46 }
47 }
48 }
49
50 return loaded;
51 }
52
53 public import{
54 Pointer cdecl readlink (String __path,
55 ObjectPtr __buf, Pointer __len);
56 int cdecl kill(int pid, int sig);
57 };
58 };
59 public class Win32Api{
60
61 public static class Psapi32 : Library{
62
63 public static bool loaded = false;
64
65 public static bool load() {
66 if (loaded == false) {
67 try {
68 loadLibrary("psapi.dll");
69 loaded = true;
70 } catch(Exception e) {
71
72 }
73 }
74
75 return loaded;
76 }
77
78 public import {
79 int
80 stdcall
81 GetProcessImageFileNameA(
82 Pointer hProcess,
83 ObjectPtr lpExeName,
84 ObjectPtr lpdwSize
85 );
86
87 bool stdcall EnumProcessModules(
88 Pointer hProcess,
89 ObjectPtr lphModule,
90 int cb,
91 ObjectPtr lpcbNeeded
92 );
93
95 Pointer hProcess,
96 Pointer hModule,
97 ObjectPtr lpFilename,
98 int nSize
99 );
100 };
101 };
102
103 public static class Kernel32 : Library{
104 public static bool loaded = false;
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 @Native
171 static class PROCESSENTRY32
172 {
173 @Native public int dwSize = sizeof(this);
174 @Native public int cntUsage;
175 @Native public int th32ProcessID; // this process
176 @Native public Pointer th32DefaultHeapID;
177 @Native public int th32ModuleID; // associated exe
178 @Native public int cntThreads;
179 @Native public int th32ParentProcessID; // this process's parent process
180 @Native public int pcPriClassBase; // Base priority of process's threads
181 @Native public int dwFlags;
182 @Native public byte [] szExeFile = new byte [Win32Api.Kernel32.MAX_PATH] ; // Path
183 @Native int n;
184 };
185
186 public class OSProcess{
187 public OSProcess() {
188 }
189 public OSProcess(int Id) {
190 processId = Id;
191 }
193 processId = entry.th32ProcessID;
194 int len = Win32Api.Kernel32.lstrlenA(entry.szExeFile);
195 if (len > 0){
196 name = new String(entry.szExeFile, 0, len);
197 }
198 }
199
203
204 public int getId() {
205 return processId;
206 }
207
208 public String getName() {
209 return name;
210 }
211
212 public String getPath() {
213 //PROCESS_QUERY_INFORMATION
214 if (exepath == nilptr) {
215
216 if (rtlp.rtl_osid == 0) {
217 if (false == Win32Api.Psapi32.load()) {
218 return nilptr;
219 }
220
221 long handle = open();
222
223 if (handle > 0) {
224 byte [] path = new byte[Win32Api.Kernel32.MAX_PATH];
225 int [] pl = new int[1];
226 long [] hm = new long[1];
227
228 pl[0] = Win32Api.Kernel32.MAX_PATH;
229 int up = 0;
230
231 if (Win32Api.Psapi32.EnumProcessModules(handle, hm, 8, up)) {
232 int len = Win32Api.Psapi32.GetModuleFileNameExA(handle, hm[0], path, Win32Api.Kernel32.MAX_PATH);
233
234 if (len > 0) {
235 try {
236 exepath = new String(path, 0, len);
237 } catch(Exception e) {
238
239 }
240 }
241 }
242
243 closeHandle(handle);
244 }
245 }
246 }
247
248 return exepath;
249 }
250
251 public long open() {
252 return Win32Api.Kernel32.OpenProcess(0x001fffff, false, processId);
253 }
254
255 public void closeHandle(long handle) {
256 Win32Api.Kernel32.CloseHandle(handle);
257 }
258 public long open(int flag, bool bi) {
259 return Win32Api.Kernel32.OpenProcess(flag, bi, processId);
260 }
261
262 public bool kill(int code) {
263 bool r = false;
264
265 if (rtlp.rtl_osid == 0) {
266
267 long h = open();
268
269 if (h > 0) {
270 r = Win32Api.Kernel32.TerminateProcess(h, code);
271 Win32Api.Kernel32.CloseHandle(h);
272 }
273 } else {
274 r = 0 == UnixlikeApi.kill(processId, 15);
275 }
276
277 return r;
278 }
279
280 public static
281 List<OSProcess> listProcesses() {
282 int osid = _system_.getPlatformId();
283
284 if (osid == 0) {
285 return getProcessListWin32();
286 } else if (osid == 1 || osid == 2) {
287 return getProcessListUnix();
288 }
289
290 return nilptr;
291 }
292
293 static
294 List<OSProcess> getProcessListUnix() {
295 List<OSProcess> plist = nilptr;
296
297 if (false == UnixlikeApi.load()) {
298 return nilptr;
299 }
300
301 plist = new List<OSProcess>();
302
303 try {
304 File file = new File ("/proc");
305 long hfind = file.open();
306
307 if (hfind != 0) {
308 File pf = new File();
309
310 while (file.find(hfind, pf)) {
311 String pname = pf.getName();
312
313 if (pname != nilptr && pname.isDigit()) {
314 OSProcess process = new OSProcess();
315 String fppath = pf.getPath();
316
317 if (fppath != nilptr) {
318 String namepath = fppath.appendPath("status");
319
320 long fis = _system_.open(namepath, _system_.READ);
321
322 if (fis > 0) {
323 String txtstatus = "";
324 byte []status = new byte[1024];
325 int rd = 0;
326
327 while ((rd = _system_.read(fis, status, 0, 1024)) > 0) {
328 txtstatus = txtstatus + new String(status, 0, rd);
329 }
330
331 _system_.close(fis);
332 int pos = txtstatus.indexOf("Name:");
333
334 if (pos != -1) {
335 int le = txtstatus.indexOf("\n", pos);
336 if (le == -1) {
337 le = txtstatus.length();
338 }
339 process.name = txtstatus.substring(pos + 5, le).trim(true);
340 }
341 }
342
343 String expath = fppath.appendPath("exe");
344 byte [] fullpath = new byte[4096];
345 long length = UnixlikeApi.readlink(expath, fullpath, (long)4096);
346
347 if (length != -1) {
348 process.exepath = new String (fullpath, 0, (int)length);
349 }
350
351 process.processId = pname.parseInt();
352 plist.add(process);
353 }
354 }
355 }
356
357 file.close(hfind);
358 }
359 } catch(Exception e) {
360
361 }
362
363 return plist;
364 }
365
366 static
367 List<OSProcess> getProcessListWin32() {
368 List<OSProcess> plist = nilptr;
369
370 if (false == Win32Api.Kernel32.load()) {
371 return nilptr;
372 }
373
374 PROCESSENTRY32 struct = new PROCESSENTRY32();
375 plist = new List<OSProcess>();
376
377 long snopt = Win32Api.Kernel32.CreateToolhelp32Snapshot(
378 Win32Api.Kernel32.TH32CS_SNAPALL,
379 0);
380
381 try {
382 if (snopt > 0) {
383 bool succ = Win32Api.Kernel32.Process32First(snopt, struct);
384
385 while (succ) {
386 plist.add(new OSProcess(struct));
387 succ = Win32Api.Kernel32.Process32Next(snopt, struct);
388 }
389
390 Win32Api.Kernel32.CloseHandle(snopt);
391 }
392 } catch(Exception e) {
393
394 }
395
396 return plist;
397 }
398 };
399};
400
401/*
402package System {
403 public class out{
404 public static int println(String text) {
405 return _system_.consoleWrite(text + "\n");
406 }
407 public static int print(String text) {
408 return _system_.consoleWrite(text);
409 }
410 };
411
412 public class Console{
413 public Console() {
414 _system_.createConsole();
415 }
416 };
417};
418
419
420using { System; };
421
422
423int main(String [] args) {
424
425 List<Runtime.OSProcess> lists = Runtime.OSProcess.listProcesses();
426
427 List.Iterator<Runtime.OSProcess> iter = lists.iterator();
428
429 while (iter.hasNext()) {
430 Runtime.OSProcess p = iter.next();
431 System.out.println(p.getName() + "[" + p.getId() + "] " + p.getPath());
432 }
433
434 Thread.sleep(-1);
435
436 return 0;
437}
438*/
static final long read(long, byte[], long, long)
static final int getPlatformId()
static const int READ
Definition _system_.x:232
static final void output(String)
static final long open(String, int)
static final bool close(long)
final String getMessage()
Definition File.x:4
String getPath()
String getName()
long open()
bool find(long, File)
void close(long)
static final void loadLibrary(String)
void closeHandle(long handle)
Definition Runtime.xcs:255
OSProcess(PROCESSENTRY32 entry)
Definition Runtime.xcs:192
static List<OSProcess> getProcessListUnix()
Definition Runtime.xcs:294
String getName()
Definition Runtime.xcs:208
static List<OSProcess> getProcessListWin32()
Definition Runtime.xcs:367
static List<OSProcess> listProcesses()
Definition Runtime.xcs:281
String getPath()
Definition Runtime.xcs:212
OSProcess(int Id)
Definition Runtime.xcs:189
bool kill(int code)
Definition Runtime.xcs:262
long open(int flag, bool bi)
Definition Runtime.xcs:258
static bool load_linux()
Definition Runtime.xcs:14
static bool load()
Definition Runtime.xcs:32
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:65
Definition String.x:5
int length()
String substring(int, int)
bool isDigit()
String appendPath(String)
String trim(bool)
int indexOf(String, int)
int parseInt()