source: trunk/dll/killproc.c@ 350

Last change on this file since 350 was 350, checked in by root, 19 years ago

Use Runtime_Error

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1
2/***********************************************************************
3
4 $Id: killproc.c 350 2006-07-26 19:01:20Z root $
5
6 Kill a process
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2005, 2006 Steven H. Levine
10
11 24 May 05 SHL Rework Win_Error usage
12 14 Jul 06 SHL Use Runtime_Error
13
14***********************************************************************/
15
16#define INCL_DOSERRORS
17#define INCL_DOS
18#define INCL_WIN
19#include <os2.h>
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <time.h>
25#include <ctype.h>
26#include <process.h>
27#include <limits.h>
28
29#include "procstat.h"
30#include "fm3dll.h"
31#include "fm3dlg.h"
32#include "fm3str.h"
33
34#pragma data_seg(DATA2)
35
36static PSZ pszSrcFile = __FILE__;
37
38#pragma alloc_text(KILLPROC,FillKillList,FillKillList2,GetDosPgmName,KillDlgProc)
39
40CHAR *GetDosPgmName (PID pid,CHAR *string)
41{
42 HSWITCH hs;
43 SWCNTRL swctl;
44 PCH pch;
45
46 *string = 0;
47 hs = WinQuerySwitchHandle(0,pid);
48 if(hs) {
49 WinQuerySwitchEntry(hs,&swctl);
50 pch = swctl.szSwtitle;
51 while(*pch) {
52 if(*pch < 0x10)
53 if(pch != swctl.szSwtitle && *(pch - 1) == 0x20)
54 memmove(pch,pch + 1,strlen(pch));
55 else {
56 *pch = 0x20;
57 pch++;
58 }
59 else
60 pch++;
61 }
62 strcpy(string,swctl.szSwtitle);
63 }
64 if(!*string)
65 strcpy(string,GetPString(IDS_UNKNOWNDOSPROCTEXT));
66 return string;
67}
68
69
70VOID FillKillList2 (VOID *arg)
71{
72 HWND hwnd = *(HWND *)arg;
73 CHAR s[1036];
74 HAB thab;
75 HMQ thmq;
76 INT rc;
77 PROCESSINFO *ppi;
78 BUFFHEADER *pbh;
79 MODINFO *pmi;
80
81 thab = WinInitialize(0);
82 thmq = WinCreateMsgQueue(thab,0);
83 WinCancelShutdown(thmq,TRUE);
84
85 WinSendDlgItemMsg(hwnd,KILL_LISTBOX,LM_DELETEALL,MPVOID,MPVOID);
86 rc = DosAllocMem((PVOID)&pbh,USHRT_MAX + 4096,
87 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
88 if (rc)
89 Dos_Error(MB_CANCEL,rc,HWND_DESKTOP,pszSrcFile,__LINE__,GetPString(IDS_OUTOFMEMORY));
90 else {
91 rc = DosQProcStatus(pbh,USHRT_MAX);
92 if (!rc) {
93 ppi = pbh->ppi;
94 while(ppi->ulEndIndicator != PROCESS_END_INDICATOR ) {
95 if(ppi->pid != mypid) {
96 pmi = pbh->pmi;
97 while(pmi && ppi->hModRef != pmi->hMod)
98 pmi = pmi->pNext;
99 if(pmi) {
100 sprintf(s,"%04x ",ppi->pid);
101 if(!stricmp(pmi->szModName,"SYSINIT"))
102 GetDosPgmName(ppi->pid,s + strlen(s));
103 else {
104 if(*pmi->szModName)
105 strcat(s,pmi->szModName);
106 else
107 strcat(s,GetPString(IDS_UNKNOWNPROCTEXT));
108 }
109 if (WinIsWindow(thab,hwnd)) {
110 WinSendDlgItemMsg(hwnd,KILL_LISTBOX,LM_INSERTITEM,
111 MPFROM2SHORT(LIT_SORTASCENDING,0),
112 MPFROMP(s));
113 }
114 else
115 break;
116 }
117 }
118 ppi = (PPROCESSINFO)(ppi->ptiFirst + ppi->usThreadCount);
119 } // while
120 }
121 DosFreeMem(pbh);
122 }
123
124 if(WinIsWindow(thab,hwnd))
125 PostMsg(hwnd,UM_CONTAINER_FILLED,MPVOID,MPVOID);
126 WinDestroyMsgQueue(thmq);
127 WinTerminate(thab);
128}
129
130
131VOID FillKillList (VOID *arg)
132{
133 HWND hwnd = *(HWND *)arg;
134 CHAR s[1036],progname[1027],*p;
135 HAB thab;
136 HMQ thmq;
137 FILE *fp;
138 BOOL foundstart = FALSE;
139 INT rc;
140 CHAR *startstring = "Process and Thread Information";
141 CHAR *endstring = "System Semaphore Information";
142 PID pid;
143 HFILE oldstdout,newstdout;
144
145 DosError(FERR_DISABLEHARDERR);
146
147 thab = WinInitialize(0);
148 thmq = WinCreateMsgQueue(thab,0);
149 WinCancelShutdown(thmq,TRUE);
150
151 WinSendDlgItemMsg(hwnd,
152 KILL_LISTBOX,
153 LM_DELETEALL,
154 MPVOID,
155 MPVOID);
156 strcpy(s,"$PSTAT#$.#$#");
157 unlinkf("%s",s);
158 fp = fopen(s,"w");
159 if(!fp) {
160 Win_Error(NULLHANDLE,HWND_DESKTOP,__FILE__,__LINE__,
161 GetPString(IDS_REDIRECTERRORTEXT));
162 goto Abort;
163 }
164 else {
165 newstdout = -1;
166 rc = DosDupHandle(fileno(stdout),&newstdout);
167 if (rc)
168 Dos_Error(MB_CANCEL,rc,hwnd,__FILE__,__LINE__,"DosDupHandle");
169 oldstdout = fileno(stdout);
170 DosDupHandle(fileno(fp),&oldstdout);
171 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
172 hwnd,
173 NULL,
174 NULL,
175 "%s",
176 "PSTAT.EXE /C");
177 oldstdout = fileno(stdout);
178 DosDupHandle(newstdout,&oldstdout);
179 DosClose(newstdout);
180 fclose(fp);
181 if(rc == -1) {
182 saymsg(MB_CANCEL,
183 hwnd,
184 GetPString(IDS_ARGHTEXT),
185 GetPString(IDS_CANTRUNPSTATTEXT));
186 goto Abort;
187 }
188 }
189 fp = fopen(s,"r");
190 if(fp) {
191 while(!feof(fp)) {
192 strset(s,0);
193 if(!fgets(s,1025,fp))
194 break;
195 if(!foundstart) {
196 if(*s == ' ' && strstr(s,startstring))
197 foundstart = TRUE;
198 }
199 else {
200 if(*s == ' ' && strstr(s,endstring))
201 break;
202 if(*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
203 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
204 p = &s[1];
205 pid = strtol(&s[1],&p,16);
206 if(pid && pid != mypid) {
207 strcpy(progname,&s[30]);
208 p = strchr(progname,' ');
209 if(p)
210 *p = 0;
211 if(!stristr(progname,"\\PSTAT.EXE")) {
212 sprintf(s,"%04x %s",pid,progname);
213 WinSendDlgItemMsg(hwnd,
214 KILL_LISTBOX,
215 LM_INSERTITEM,
216 MPFROM2SHORT(LIT_SORTASCENDING,0),
217 MPFROMP(s));
218 }
219 }
220 }
221 }
222 }
223 fclose(fp);
224 }
225Abort:
226 DosForceDelete("$PSTAT#$.#$#");
227 PostMsg(hwnd,
228 UM_CONTAINER_FILLED,
229 MPVOID,
230 MPVOID);
231 WinDestroyMsgQueue(thmq);
232 WinTerminate(thab);
233}
234
235
236MRESULT EXPENTRY KillDlgProc (HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
237{
238 SHORT sSelect;
239 PID pid;
240 static BOOL listdone;
241 static HPOINTER hptrIcon = (HPOINTER)0;
242
243 switch(msg) {
244 case WM_INITDLG:
245 hptrIcon = WinLoadPointer(HWND_DESKTOP,
246 FM3ModHandle,
247 KILL_FRAME);
248 WinDefDlgProc(hwnd,
249 WM_SETICON,
250 MPFROMLONG(hptrIcon),
251 MPVOID);
252 WinCheckButton(hwnd,
253 KILL_CHECKBOX,
254 fUseQProcStat);
255 PostMsg(hwnd,
256 WM_COMMAND,
257 MPFROM2SHORT(KILL_RESCAN,0),
258 MPVOID);
259 break;
260
261 case UM_CONTAINER_FILLED:
262 listdone = TRUE;
263 if((SHORT)WinSendDlgItemMsg(hwnd,
264 KILL_LISTBOX,
265 LM_QUERYITEMCOUNT,
266 MPVOID,
267 MPVOID) == 0) {
268 if(!fUseQProcStat)
269 saymsg(MB_CANCEL,
270 hwnd,
271 GetPString(IDS_ICHOKEDTEXT),
272 GetPString(IDS_ISPSTATTHERETEXT));
273 else
274 saymsg(MB_CANCEL,
275 hwnd,
276 GetPString(IDS_ICHOKEDTEXT),
277 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
278 }
279 return 0;
280
281 case WM_CONTROL:
282 switch(SHORT1FROMMP(mp1)) {
283 case KILL_CHECKBOX:
284 fUseQProcStat = WinQueryButtonCheckstate(hwnd,
285 KILL_CHECKBOX);
286 PrfWriteProfileData(fmprof,
287 FM3Str,
288 "UseQProcStat",
289 &fUseQProcStat,
290 sizeof(BOOL));
291 PostMsg(hwnd,
292 WM_COMMAND,
293 MPFROM2SHORT(KILL_RESCAN,0),
294 MPVOID);
295 break;
296
297 case KILL_LISTBOX:
298 switch(SHORT2FROMMP(mp2)) {
299 case LN_ENTER:
300 WinSendDlgItemMsg(hwnd,
301 DID_OK,
302 BM_CLICK,
303 MPFROMSHORT(TRUE),
304 MPVOID);
305 break;
306 }
307 break;
308
309 default:
310 break;
311 }
312 return 0;
313
314 case WM_ADJUSTWINDOWPOS:
315 PostMsg(hwnd,
316 UM_STRETCH,
317 MPVOID,
318 MPVOID);
319 break;
320
321 case UM_STRETCH:
322 {
323 SWP swpC,swp,swpH;
324
325 WinQueryWindowPos(hwnd,&swp);
326 if(!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
327 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),&swpC);
328 WinQueryWindowPos(WinWindowFromID(hwnd,KILL_HDR),&swpH);
329 WinSetWindowPos(WinWindowFromID(hwnd,KILL_LISTBOX),HWND_TOP,
330 SysVal(SV_CXSIZEBORDER),
331 swpC.y,
332 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
333 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
334 SysVal(SV_CYSIZEBORDER)) -
335 (swpH.cy + 8)),
336 SWP_MOVE | SWP_SIZE);
337 WinSetWindowPos(WinWindowFromID(hwnd,KILL_HDR),HWND_TOP,
338 SysVal(SV_CXSIZEBORDER) + 4,
339 swpC.y + ((swp.cy - swpC.y) -
340 (SysVal(SV_CYTITLEBAR) +
341 SysVal(SV_CYSIZEBORDER)) -
342 (swpH.cy + 4)),
343 swpH.cx,
344 swpH.cy,
345 SWP_MOVE);
346 }
347 }
348 return 0;
349
350 case WM_COMMAND:
351 switch(SHORT1FROMMP(mp1)) {
352 case KILL_RESCAN:
353 listdone = FALSE;
354 if(fUseQProcStat) {
355 if(_beginthread(FillKillList2,
356 NULL,
357 65536,
358 (PVOID)&hwnd) != -1)
359 DosSleep(250L);
360 else
361 WinDismissDlg(hwnd,0);
362 }
363 else {
364 if(_beginthread(FillKillList,
365 NULL,
366 65536,
367 (PVOID)&hwnd) != -1)
368 DosSleep(250L);
369 else
370 WinDismissDlg(hwnd,0);
371 }
372 break;
373
374 case KILL_SHOW:
375 case DID_OK:
376 sSelect = (USHORT)WinSendDlgItemMsg(hwnd,
377 KILL_LISTBOX,
378 LM_QUERYSELECTION,
379 MPFROMSHORT(LIT_FIRST),
380 MPVOID);
381 if(sSelect >= 0) {
382
383 CHAR s[31],*p;
384 APIRET error;
385
386 *s = 0;
387 WinSendDlgItemMsg(hwnd,
388 KILL_LISTBOX,
389 LM_QUERYITEMTEXT,
390 MPFROM2SHORT(sSelect,30),
391 MPFROMP(s));
392 if(*s) {
393 p = s;
394 pid = strtol(s,&p,16);
395 if(pid) {
396 if(SHORT1FROMMP(mp1) == DID_OK) {
397 error = DosKillProcess(DKP_PROCESS,pid);
398 if(error && error != ERROR_INVALID_PROCID) {
399 Dos_Error(MB_CANCEL,
400 error,
401 hwnd,
402 __FILE__,
403 __LINE__,
404 GetPString(IDS_DOSKILLFAILEDTEXT));
405 }
406 else
407 WinSendDlgItemMsg(hwnd,
408 KILL_LISTBOX,
409 LM_DELETEITEM,
410 MPFROM2SHORT(sSelect,0),
411 MPVOID);
412 }
413 else if(!ShowSession(hwnd,pid))
414 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
415 }
416 }
417 }
418 break;
419
420 case DID_CANCEL:
421 if(!listdone)
422 Runtime_Error(pszSrcFile, __LINE__, "busy");
423 else
424 WinDismissDlg(hwnd,0);
425 break;
426
427 case IDM_HELP:
428 saymsg(MB_ENTER | MB_ICONASTERISK,
429 hwnd,
430 GetPString(IDS_KILLPROCHELPTITLETEXT),
431 GetPString(IDS_KILLPROCHELPTEXT));
432 break;
433 }
434 return 0;
435
436 case WM_CLOSE:
437 if(!listdone) {
438 Runtime_Error(pszSrcFile, __LINE__, "busy");
439 return 0;
440 }
441 break;
442
443 case WM_DESTROY:
444 if(hptrIcon)
445 WinDestroyPointer(hptrIcon);
446 hptrIcon = (HPOINTER)0;
447 break;
448 }
449 return WinDefDlgProc(hwnd,msg,mp1,mp2);
450}
451
Note: See TracBrowser for help on using the repository browser.