杀死进程(win32 ,wince等平台)

// killProcess.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <locale.h>
#include <stdarg.h>
#include <Windows.h>
#include <tchar.h>

#include <tlhelp32.h>

void CloseExist(_TCHAR pName[]);
void wscprintf(wchar_t * str,...);
void c2w(wchar_t *pwstr,size_t len,const char *str);
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len);

int _tmain(int argc, _TCHAR* argv[])
{
	//char e;
	if(argc==1)
	{
		//printf("no process");
		//scanf("%c",&e
		wchar_t pCurDir[120]={0};
		GetModuleFileName(NULL,pCurDir,120);
		for(int i=119;i>=0;i--){
			if(pCurDir[i]!='\\')
				pCurDir[i]=0;
			else
			{
				//pCurDir[i]=0;
				wcscat(pCurDir,_T("pconfig.txt"));
				break;
			}
		}
		char config_file[500]={0};
		w2c(config_file,pCurDir,wcslen(pCurDir)*2+1);
		printf(config_file);
		FILE *handle=fopen(config_file,"r");
		if(!handle){
			wscprintf(_T("pconfig.txt not found or error"));
			return 0;
		}
		char buf[100]={0};

		//wchar_t  pcm[30]=_T("NK.EXE");

		while(fgets(buf,99,handle)!=NULL){
			wchar_t cmd[30]={0};
			for(int i=0;i<97;i++){
				if((buf[i]=='e'&&buf[i+1]=='x'&& buf[i+2]=='e')||(buf[i]=='E'&&buf[i+1]=='X'&& buf[i+2]=='E')){
					buf[i+3]=0;
				}
			}
			c2w(cmd,30,buf);
			wscprintf(_T("%s to %s"),buf,cmd);
			//printf(buf);
			CloseExist(cmd);

			memset(buf,0,sizeof(buf));
		}
		fclose(handle);
		return 0;
	}

	CloseExist(argv[1]);

	//scanf("%c",&e);
	//system("pause");
	return 0;
}


void CloseExist(_TCHAR pName[])
{
	HANDLE hProcessSnap;
	HANDLE hProcess;
	PROCESSENTRY32 pe32;
	DWORD dwPriorityClass;

	bool bFind = false;
	do
	{
		bFind = false;

		wscprintf(_T("CreateToolhelp32Snapshot..."));
		// Take a snapshot of all processes in the system.
		hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (hProcessSnap == INVALID_HANDLE_VALUE)
		{
			wscprintf(_T("无效的操作,errono=%d"),GetLastError());
			//MessageBox(0,LPCTSTR(buf),L"info",0);
			return;
		}

		// Set the size of the structure before using it.
		pe32.dwSize = sizeof(PROCESSENTRY32);

		// Retrieve information about the first process,
		// and exit if unsuccessful
		if (!Process32First(hProcessSnap, &pe32))
		{
			wscprintf(_T("clean the snapshot objet,errono=%d"),GetLastError());
			CloseHandle(hProcessSnap);          // clean the snapshot object
			return;
		}

		// Now walk the snapshot of processes, and
		// display information about each process in turn

		wscprintf(_T("searching the process..."));

		//int len=strlen(pName);
		do
		{
			// Retrieve the priority class.
			dwPriorityClass = 0;
			//if (::strstr(pe32.szExeFile, pName) != NULL)
			//if(isTcharEqualToChar(pe32.szExeFile,pName,len))
			if(wcscmp(pe32.szExeFile,pName)==0)
			{
				bFind = true;
				hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
				::TerminateProcess(hProcess, 0);
				wscprintf(_T("Terminated one process %s\n"), pName);
				Sleep(1000);
				CloseHandle(hProcess);
			}
		} while (Process32Next(hProcessSnap, &pe32));

		if(!bFind)
			wscprintf(_T("not find %s\n"),pName);

		CloseHandle(hProcessSnap);
	} while (bFind);
}

void wscprintf(wchar_t * format,...)
{
  va_list va;
  va_start(va,format);
  TCHAR sz[1024];
  _vstprintf_s(sz,_countof(sz),format,va);
  va_end(va);
  //_wsetlocale(LC_ALL,_T("Chinese-simplified"));
  _tprintf(_T("%s\n"),sz);
}

void c2w(wchar_t *pwstr,size_t len,const char *str)
{
	if(str)
    {
      size_t nu = strlen(str);
      size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);
      if(n>=len)n=len-1;
      MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);
	  pwstr[n]=0;

    }

}

//将wchar_t* 转成char*的实现函数如下:

char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
	int nlength=wcslen(pwstr);
	//获取转换后的长度
	int nbytes = WideCharToMultiByte( 0,0,pwstr,nlength,NULL,0,NULL,NULL );
	if(nbytes>len)   
		nbytes=len;
	// 通过以上得到的结果,转换unicode 字符为ascii 字符
	WideCharToMultiByte( 0,0,pwstr,nlength,pcstr,nbytes,NULL,NULL );
	return pcstr ;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值