// 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 ;
}
杀死进程(win32 ,wince等平台)
最新推荐文章于 2021-12-22 17:41:31 发布