source: trunk/dll/killproc.c@ 145

Last change on this file since 145 was 145, checked in by root, 20 years ago

Rework Win_Error usage

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