JapyDooge
asked on
[C++] Kill a process
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.c pp In function `LRESULT WndProc(HWND__*, UINT, WPARAM, LPARAM)':
353 C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects \T2\main.c pp `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\Makefi le.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...
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
353 C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Documents and Settings\2wjd\My Documents\Dev-C++\Projects
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...
// 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
// *************************************************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the comments, but EndTask was my last try after i found it somewhere on the net.
EndTask() is also in winuser.h and i include that so it should work.
Removing EndTask() does'nt work either...
EndTask() is also in winuser.h and i include that so it should work.
Removing EndTask() does'nt work either...
Um, there is no 'EndTask()' in winuser.h, neither elsewhere...
ASKER
So what is this?
endtask.jpg
endtask.jpg
Windows 2000 0x0500
Windows Server 2003, Windows XP 0x0501
...
if you realy need this API, please define this macro
_WIN32_WINNT 0x50 accordingly in your stdafx.h
Windows Server 2003, Windows XP 0x0501
...
if you realy need this API, please define this macro
_WIN32_WINNT 0x50 accordingly in your stdafx.h
ASKER
I don't need that API but i need a way to kill a process from C++ (one i also started in C++)
It can't be that hard, does no one know how to do it? I tried a lot of different ways...
It can't be that hard, does no one know how to do it? I tried a lot of different ways...
Well there is one way to do it, but its not the best way. you could include windows.h and then use the system () call to exit the application. ex. system ("taskkill /im Program.exe");
Tend to object - in the above, there's a link to a fully-fledged MSDN article on how to do that, complete source code included.
void EndTask(Process, int, int);
something like that.