[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

08/05/2008 at 08:24AM PDT, ID: 23622548
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

[C++] Kill a process

Asked by JapyDooge in C++ Programming Language, Microsoft Visual C++.Net, Microsoft Windows Operating Systems

Tags: C++, n/a, n/a

I have a little problem here.
In C++ i create a process and after that i want to end (kill) it but for some reason i can't get this to work.
Check my code down here and see what i already tried (a LOT).

Most of this code is made of 'fount' pieces over the net.
I added 'EndTask' but it says:

 C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects\T2\main.cpp In function `LRESULT WndProc(HWND__*, UINT, WPARAM, LPARAM)':
353 C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects\T2\main.cpp `EndTask' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
 C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects\T2\Makefile.win [Build Error]  [main.o] Error 1

That's strange becouse i have al needed includes.

I'm using Dev-C++, the starting and running of my program works great, i only want to kill the process i create on exit...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
// test the listbox creation and selection
// modified from BCX generated C code for Dev-C++
// a Dev-C++ tested Windows Application by  vegaseat  04nov2004
 
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
 
using namespace std;
 
static HINSTANCE BCX_hInstance;
static int     BCX_ScaleX;
static int     BCX_ScaleY;
static char    BCX_ClassName[2048];  // default size
static char    text[2048];
static HWND    Form1;
static HWND    List1;
static HWND    Butn1;
static HWND    Butn2;
int     IntSucces;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
#define Show(Window)  RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
 
HWND    BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
HWND    BCX_Listbox(char*,HWND,int  ,int  ,int  ,int  ,int  ,int=0,int=-1);
HWND     BCX_Button(char*,HWND,int=0,int=0,int=0,int=0,int=0,int=0,int=-1);
int     BCX_Set_Text(HWND,char*);
void    Center (HWND,HWND=0,HWND=0);
char*   BCX_TmpStr(size_t);
 
void    FormLoad (void);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void    addLB (HWND, char *);
char    * getLB (HWND);
 
// sample data for the list box
static char names[2][10]=
{
  "WVUN002", "NVUN012"
};
 
 
// this is the standard windows main() function
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
{
 WNDCLASS Wc;
 MSG      Msg;
 // *****************************
 strcpy(BCX_ClassName,"ListBox1");
 // ***************************************
 // Programmer has selected to use pixels
 // ***************************************
 BCX_ScaleX       = 1;          // for generic scaling
 BCX_ScaleY       = 1;
 BCX_hInstance    =  hInst;
 // ******************************************************
 Wc.style         =  CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 Wc.lpfnWndProc   =  WndProc;
 Wc.cbClsExtra    =  0;
 Wc.cbWndExtra    =  0;
 Wc.hInstance     =  hInst;
 Wc.hIcon         =  LoadIcon(NULL,IDI_WINLOGO);
 Wc.hCursor       =  LoadCursor(NULL,IDC_ARROW);
 Wc.hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
 Wc.lpszMenuName  =  NULL;
 Wc.lpszClassName =  BCX_ClassName;
 RegisterClass(&Wc);
 
 FormLoad();
  // the event message loop
 while(GetMessage(&Msg,NULL,0,0))
   {
    HWND hActiveWindow = GetActiveWindow();
    if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
      {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
      }
    }
 return Msg.wParam;
}
 
 
// circular storage for strings
char *BCX_TmpStr (size_t Bites)
{
  static int   StrCnt;
  static char *StrFunc[2048];
  StrCnt=(StrCnt + 1) & 2047;
  if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
  return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
}
int BCX_Set_Text(HWND hWnd, char *Text){
      return SetWindowText(hWnd,Text);
}
 
// center the form in the screen (really optional, for looks)
void Center (HWND hwnd, HWND Xhwnd, HWND Yhwnd)
{
  RECT rect, rectP;
  int  x, y, width, height;
  int  screenwidth, screenheight;
  if(Xhwnd==0)
    {
      RECT  DesktopArea;
      RECT  rc;
      SystemParametersInfo(SPI_GETWORKAREA,0,&DesktopArea,0);
      GetWindowRect(hwnd,&rc);
      SetWindowPos(hwnd,HWND_TOP,
        ((DesktopArea.right-DesktopArea.left)-(rc.right-rc.left))/2+
          DesktopArea.left,((DesktopArea.bottom-DesktopArea.top)-
         (rc.bottom-rc.top))/2 + DesktopArea.top,0,0,SWP_NOSIZE);
      return;
    }
  GetWindowRect (hwnd,&rect);
  GetWindowRect (Xhwnd,&rectP);
  width = rect.right-rect.left;
  x = ((rectP.right-rectP.left)-width)/2 + rectP.left;
  if(Yhwnd==NULL)
    {
      height = rect.bottom-rect.top;
      y = ((rectP.bottom-rectP.top)-height)/2 + rectP.top;
    }
  else
    {
      GetWindowRect(Yhwnd,&rectP);
      height = rect.bottom-rect.top;
      y = ((rectP.bottom-rectP.top)-height)/2+rectP.top;
    }
  screenwidth = GetSystemMetrics(SM_CXSCREEN);
  screenheight = GetSystemMetrics(SM_CYSCREEN);
  if ((x<0)) x=0;
  if ((y<0)) y=0;
  if ((x+width>screenwidth))   x = screenwidth-width;
  if ((y+height>screenheight)) y = screenheight-height;
  MoveWindow (hwnd, x, y, width, height, FALSE);
}
 
 
// create the windows form
HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
{
   HWND  A;
   // assign a default style
   if (!Style)
   {
     Style=
//     WS_MINIMIZEBOX  |
//     WS_SIZEBOX      |
     WS_CAPTION      |
//     WS_MAXIMIZEBOX  |
     WS_POPUP        |
     WS_SYSMENU      ;
   }
   A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
     Style,
     X*BCX_ScaleX,
     Y*BCX_ScaleY,
     (4+W)*BCX_ScaleX,
     (12+H)*BCX_ScaleY,
     NULL,(HMENU)NULL,BCX_hInstance,NULL);
   SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
     (LPARAM)MAKELPARAM(FALSE,0));
   return A;
}
 
 
// create the list box with desired styles
HWND BCX_Listbox
(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
{ 
  HWND  A;
  // assign a default style if needed
  if (!Style)
  {
    Style=LBS_STANDARD | WS_CHILD | WS_VISIBLE |
    LBS_SORT | WS_VSCROLL | WS_TABSTOP;
  }
  if (Exstyle == -1)
  {
    Exstyle=WS_EX_CLIENTEDGE;
  }
  A = CreateWindowEx(Exstyle,"Listbox",NULL,Style,
        X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
        hWnd,(HMENU)id,BCX_hInstance,NULL);
  SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject
        (DEFAULT_GUI_FONT),(LPARAM)MAKELPARAM(FALSE,0));
  return A;
}
 
 
// create the various buttons
HWND BCX_Button
(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
{ 
HWND A;
// assign default style
if(!Style)
{
Style=WS_CHILD | WS_VISIBLE | BS_MULTILINE | BS_PUSHBUTTON | WS_TABSTOP;
}
if(Exstyle==-1)
{
Exstyle=WS_EX_STATICEDGE;
}
A = CreateWindowEx(Exstyle,"button",Text,Style,
X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
hWnd,(HMENU)id,BCX_hInstance,NULL);
SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
(LPARAM)MAKELPARAM(FALSE,0));
if (W==0)
{
HDC hdc=GetDC(A);
SIZE size;
GetTextExtentPoint32(hdc,Text,strlen(Text),&size);
ReleaseDC(A,hdc);
MoveWindow(A,X*BCX_ScaleX,Y*BCX_ScaleY,(int)(size.cx+(size.cx*0.5)),(int)(size.cy+(size.cy*0.32)),TRUE);
}
return A;
}
 
 
// the details for Form1 and List1 (corner,width,height)
void FormLoad (void)
{
  static char A[2048];
  memset(&A,0,sizeof(A));
  static int      k;
  memset(&k,0,sizeof(k));
  Form1=BCX_Form("Select connection:",0,0,235,195);
  List1=BCX_Listbox("",Form1,1009,10,15,100,150);
  Butn1=BCX_Button("Connect",Form1,1010,120,140,100,25);
  Butn2=BCX_Button("Exit",Form1,1011,120,15,100,25);
  for(k=0; k<=1; k+=1)
  {
    strcpy(A,(char*)names[k]);
    addLB(List1,A);
  }
  Center(Form1);   // optional
  Show(Form1);
}
 
void KillProcess(DWORD pid)
{
HANDLE tokenHandle = INVALID_HANDLE_VALUE;
TOKEN_PRIVILEGES newTokenPrivileges;
TOKEN_PRIVILEGES oldTokenPrivileges;
DWORD oldTokenPrivilegeSize = 0;
LUID newLuid;
 
// Get Debug Privileges for current process
if(!OpenProcessToken(GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), &tokenHandle))
{
printf ("Unable to get current process token - %d\n", GetLastError());
return;
}
 
// Get the LUID for SE_DEBUG_NAME
if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &newLuid))
{
printf ("Unable to lookup privilege value SE_DEBUG_NAME - %d\n", GetLastError());
return;
}
 
// Setup new token privileges
newTokenPrivileges.PrivilegeCount = 1; // We need only one privilege to change time
// Copy over privilege value to one which we will send to system
memcpy(&newTokenPrivileges.Privileges[0].Luid, &newLuid, sizeof(LUID)); // The privilege to change system time
newTokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Enable it !!
 
// Adjust the process token privileges
if(!AdjustTokenPrivileges(tokenHandle, FALSE, &newTokenPrivileges, sizeof(TOKEN_PRIVILEGES), &oldTokenPrivileges, &oldTokenPrivilegeSize))
{
printf ("Unable to adjust token privileges for current process - %d\n", GetLastError());
return;
}
 
HANDLE processHandle = OpenProcess (PROCESS_ALL_ACCESS, 0, pid);
 
if (!processHandle)
{
//printf ("Could not get process andle for %d... error = %d\n", pid, GetLastError));
//continue;
}
 
if (!TerminateProcess(processHandle, 0))
{
//printf ("Could not terminate process %d... error = %d\n", pid, GetLastError());
//continue;
}
 
CloseHandle(processHandle);
}
 
 
 
 
 
// standard windows message handling
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  while(1)
  {
    if (Msg==WM_COMMAND)
    {
      //  list box item clicked (selected) 
      if (LOWORD(wParam)==1009)
      {
        if (HIWORD(wParam)==LBN_SELCHANGE)
        {
          strcpy(text,(char*)getLB(List1));
          //  just for test 
          //Beep(400,500);
          //  selected item to form title 
//          SetWindowText(Form1,text);
          BCX_Set_Text(Butn1,text);
//          system("notepad");
//          Butn1=BCX_Button(text,Form1,1009,120,140,100,25);
        }
      }
      if (LOWORD(wParam)==1010)
      {
          char buffer [500];
//          int n;
          //system("winvnc4.exe -register");
          //system("winvnc4.exe -start");
	//I know its annoying that CreateProcess REQUIRES these
 
	memset(& si, 0, sizeof(si));
	memset(& pi, 0, sizeof(pi));
 
	IntSucces = CreateProcess(NULL, "winvnc4.exe", NULL, NULL, FALSE, 0, 0, NULL, & si, & pi);
 
 
          //system("winvnc4.exe");
          sprintf(buffer, "%s %s %s", "winvnc4.exe -noconsole -connect", text,"DisableWallpaper=1");
          system(buffer);
 
      }
      if (LOWORD(wParam)==1011)
      {
          CloseHandle(pi.hThread );
          CloseHandle(pi.hProcess );
 
 
 
 
 
          IntSucces = TerminateProcess(pi.hProcess,1);
          CloseHandle(pi.hProcess);
          KillProcess((DWORD)pi.hProcess);
          EndTask(pi.hProcess, 0, 1);
          system("winvnc4.exe -disconnect");      
          system("winvnc4.exe -stop");          
          system("winvnc4.exe -unregister");
          UnregisterClass(BCX_ClassName,BCX_hInstance);
          PostQuitMessage(0);                                        
      } 
    }
    break;
  }
  // exit from the window form
  if (Msg==WM_DESTROY)
  {
            KillProcess((DWORD)pi.hProcess);
          CloseHandle(pi.hThread );
          CloseHandle(pi.hProcess );
          EndTask(pi.hProcess, 0, 1);
          IntSucces = TerminateProcess((void*)pi.dwProcessId,1);
          system("winvnc4.exe -disconnect");      
          system("winvnc4.exe -stop");          
          system("winvnc4.exe -unregister");
    UnregisterClass(BCX_ClassName,BCX_hInstance);
    PostQuitMessage(0);
  }
  return DefWindowProc(hWnd,Msg,wParam,lParam);
}
 
 
//  add a string to the listbox 
void addLB (HWND idnr, char *ltext)
{
  SendMessage(idnr,(UINT)LB_ADDSTRING,(WPARAM)0,(LPARAM)ltext);
}
 
//  return selected listbox string
char * getLB (HWND idnr)
{
  static int      index;
  memset(&index,0,sizeof(index));
  static char buf[2048];
  memset(&buf,0,sizeof(buf));
  char *BCX_RetStr={0};
  index=SendMessage(idnr,(UINT)LB_GETCURSEL,(WPARAM)0,(LPARAM)0);
  SendMessage(idnr,(UINT)LB_GETTEXT,(WPARAM)index,(LPARAM)buf);
  BCX_RetStr=BCX_TmpStr(strlen(buf));
  strcpy(BCX_RetStr,buf);
  return BCX_RetStr;
}
 
// *************************************************************
//   Created with BCX -- The BASIC To C Translator (ver 5.02)
//  BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
// *************************************************************
 
Loading Advertisement...
 
[+][-]08/05/08 08:49 AM, ID: 22162139

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C++ Programming Language, Microsoft Visual C++.Net, Microsoft Windows Operating Systems
Tags: C++, n/a, n/a
Sign Up Now!
Solution Provided By: jkr
Participating Experts: 3
Solution Grade: A
 
 
[+][-]08/05/08 08:50 AM, ID: 22162153

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08/05/08 10:44 PM, ID: 22167377

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/06/08 01:15 AM, ID: 22168048

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/06/08 07:38 AM, ID: 22170714

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/06/08 07:52 AM, ID: 22170901

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/11/08 07:15 PM, ID: 22209688

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/18/08 08:52 AM, ID: 22252996

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/10/09 02:12 PM, ID: 23606112

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/25/09 01:33 PM, ID: 24468567

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]05/25/09 01:42 PM, ID: 24468604

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/25/09 06:43 PM, ID: 24469675

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]05/30/09 08:47 AM, ID: 24509373

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091028-EE-VQP-87 / EE_QW_2_20070628