#include "stdafx.h"
#include
#include
#include
#include "Tlhelp32.h"
using namespace std;
#define PROCESS_PATH L"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"
void open()
{
WCHAR proPath[MAX_PATH];
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
wcscpy(proPath,PROCESS_PATH);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
BOOL bRet = ::CreateProcess(NULL, proPath, NULL,NULL,FALSE, CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi);
return;
}
void close()
{
PROCESSENTRY32 pe;
HANDLE hProcess;
pe.dwSize = sizeof (PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
Process32First(hSnapshot,& pe);
do
{
if ( ! _tcsicmp(pe.szExeFile,_T( "wordpad.exe" )))
{
break;
}
pe.dwSize = sizeof(PROCESSENTRY32);
} while(Process32Next(hSnapshot, & pe));
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
hProcess = ::OpenProcess(PROCESS_TERMINATE,FALSE,pe.th32ProcessID); //根据进程ID返回对象句柄
::TerminateProcess(hProcess,0); //根据对象句柄结束进程
CloseHandle(hSnapshot);
CloseHandle(hProcess);
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
//return 0;
char a;
while(TRUE)
{
cout<<"please input the command:";
cin>>a;
switch (a)
{
case '1':
open();
cout<<"command confirmed"<
case '2':
close();
cout<<"command confirmed"<
case '3':
exit(0);
break;
default:
cout<<"wrong command"<
}
}
#include
#include
HANDLE hThread1 = NULL; //线程句柄定义为全局变量
DWORD ThreadID = NULL; //线程ID定义为全局变量
void open()
{
cout << "\nopen doning!"<
DWORD thread1()
{
if (hThread1)
{
cout << "\n线程[ "<
else
{
hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)open, NULL, 0, &ThreadID);
if (!hThread1)
{
cout<<"\n线程创建失败!"<
else
{
cout << "\n线程[ "<
int iR = SetThreadPriority(hThread1, THREAD_PRIORITY_NORMAL);
}
return ThreadID;
}
void close( HANDLE handle)
{
if (handle) //如果线程句柄不为空
{
WaitForSingleObject((HANDLE)handle, INFINITE) ; //等待线程handle处理完毕
CloseHandle((HANDLE)handle) ; //关闭线程
cout << "\n线程[ "<
else
{
cout << "\n线程不存在!"<
}
//主函数
void main()
{
char ch;
while(1)
{
cout<<"Your option(1.创建线程;2.关闭线程;3.退出程序.):";
cin>>ch;
switch (ch)
{
case '1':
thread1();
break;
case '2':
close(hThread1);
hThread1 = NULL;
ThreadID = NULL;
break;
case '3':
exit(0);
break;
default:
cout<<"\nwrong option"<
}
return;
}
#include "stdafx.h"
#include
#include
#include
#include "Tlhelp32.h"
using namespace std;