如何利用Windows提供的API函数,编写程序,实现进程的创建和终止(如创建写字板进程及终止该进程)
输入1创建进程,输入2关闭进程,输入0退出程序源代码谢谢大家了我用的visualC++6.0...
输入1创建进程,输入2关闭进程,输入0退出程序
源代码谢谢大家了
我用的visual C++ 6.0 展开
源代码谢谢大家了
我用的visual C++ 6.0 展开
3个回答
展开全部
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <cstdio>
#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"<<endl;
break;
case '2':
close();
cout<<"command confirmed"<<endl;
break;
case '3':
exit(0);
break;
default:
cout<<"wrong command"<<endl;
}
}
}
#include <windows.h>
#include <iostream>
#include <cstdio>
#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"<<endl;
break;
case '2':
close();
cout<<"command confirmed"<<endl;
break;
case '3':
exit(0);
break;
default:
cout<<"wrong command"<<endl;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream.h>
#include <wtypes.h>
HANDLE hThread1 = NULL; //线程句柄定义为全局变量
DWORD ThreadID = NULL; //线程ID定义为全局变量
void open()
{
cout << "\nopen doning!"<<endl;
}
DWORD thread1()
{
if (hThread1)
{
cout << "\n线程[ "<<ThreadID<<" ]已经存在!"<<endl;
}
else
{
hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)open, NULL, 0, &ThreadID);
if (!hThread1)
{
cout<<"\n线程创建失败!"<<endl;
}
else
{
cout << "\n线程[ "<<ThreadID<<" ]已创建!"<<endl;
}
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线程[ "<<ThreadID<<" ]已关闭!"<<endl;
}
else
{
cout << "\n线程不存在!"<<endl;
}
}
//主函数
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"<<endl;
}
}
return;
}
#include <wtypes.h>
HANDLE hThread1 = NULL; //线程句柄定义为全局变量
DWORD ThreadID = NULL; //线程ID定义为全局变量
void open()
{
cout << "\nopen doning!"<<endl;
}
DWORD thread1()
{
if (hThread1)
{
cout << "\n线程[ "<<ThreadID<<" ]已经存在!"<<endl;
}
else
{
hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)open, NULL, 0, &ThreadID);
if (!hThread1)
{
cout<<"\n线程创建失败!"<<endl;
}
else
{
cout << "\n线程[ "<<ThreadID<<" ]已创建!"<<endl;
}
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线程[ "<<ThreadID<<" ]已关闭!"<<endl;
}
else
{
cout << "\n线程不存在!"<<endl;
}
}
//主函数
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"<<endl;
}
}
return;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-03-11
展开全部
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <cstdio>
#include "Tlhelp32.h"
using namespace std;
#include <windows.h>
#include <iostream>
#include <cstdio>
#include "Tlhelp32.h"
using namespace std;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询