进程看门狗

2014年3月21日18:17:21

维护进程定期(10s)检查工作进程是否存在。不存在,则创建工作进程。同时杀死多余的工作进程。


1、使用VS2008创建一个带预编译头的控制台项目。


2、源文件内容:

#include "stdafx.h"  

#include <windows.h>  
#include <psapi.h>  

#include<iostream>  
using namespace std;  

#pragma  once  
#pragma  message("Psapi.h --> Linking with Psapi.lib")  
#pragma  comment(lib,"Psapi.lib")  

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS  
// and compile with -DPSAPI_VERSION=1 

bool cmpName_processID(TCHAR * pStrProcessName,DWORD processID)
{
	TCHAR szProcessName[MAX_PATH] = TEXT("");

	// Get a handle to the process.  
	HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |  
		PROCESS_VM_READ,  
		FALSE, processID ); 

	// Get the process name.  
	if (NULL != hProcess )  
	{  
		HMODULE hMod;  
		DWORD cbNeeded;  

		if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),   
			&cbNeeded) )  
		{  
			GetModuleBaseName( hProcess, hMod, szProcessName,   
				sizeof(szProcessName)/sizeof(TCHAR) );  
		}  
	} 

	CloseHandle( hProcess ); 

	if(0 == _tcscmp(pStrProcessName,szProcessName))
	{
		return true;
	}

	return false;
}

void create_process(TCHAR * pStrProcessName)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	if( !CreateProcess(pStrProcessName,   //module name
		NULL,        // Command line
		NULL,           // Process handle not inheritable
		NULL,           // Thread handle not inheritable
		FALSE,          // Set handle inheritance to FALSE
		0,              // No creation flags
		NULL,           // Use parent's environment block
		NULL,           // Use parent's starting directory 
		&si,            // Pointer to STARTUPINFO structure
		&pi )           // Pointer to PROCESS_INFORMATION structure
		) 
	{
		printf( "CreateProcess failed (%d).\n", GetLastError() );
		return;
	}

	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );

	return;
}

void check_create_process(TCHAR * pStrProcessName)
{
	bool bExist = false;

	DWORD aProcesses[1024], nByteNeed, nProcesses;  
	unsigned int i;  

	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &nByteNeed ) )  
	{  
		printf("获取所有进程失败\n");
		return ;  
	}  

	// Calculate how many process identifiers were returned.  
	nProcesses = nByteNeed / sizeof(DWORD);

	for ( i = 0; i < nProcesses; i++ )  
	{  
		if( aProcesses[i] != 0 )  
		{  
			if(cmpName_processID( pStrProcessName,aProcesses[i] ))
			{
				if(bExist)
				{
					HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |   // Required by Alpha
						PROCESS_TERMINATE,             
						FALSE, aProcesses[i] ); 

					if (NULL != hProcess )  
					{  
						TerminateProcess(hProcess, 0);
						printf("杀死进程\n");
					}  
				}
				else
				{
					bExist = true;
				}
			}
		}  
	}  

	if(bExist)return;

	create_process(pStrProcessName);
}

int main( void )  
{  
	char ch;  
	//cin>>ch;  

	while(1)
	{
		Sleep(10*1000);

		check_create_process(_T("tt.exe"));
	}


	cin>>ch;  

	return 0;  
}  






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值