今天写省电宝~需要关闭音乐进程,发现EnumProcessModules 这类函数竟然找不到,可能是定制Wince删掉这个了,也没有其他的lib,所以试验了几个方法,发现下面的这个最好用
DWORD FindProcess(TCHAR *ProcessName)
{
HANDLE hProcessSnap; ;
DWORD ProcessID=0x00;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap==INVALID_HANDLE_VALUE)
{
//printf("列举进程失败/n");
return -1;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
BOOL bProcess = Process32First(hProcessSnap, &pe32);
TCHAR buff[1024];
while(bProcess)
{
if(wcscmp(ProcessName,pe32.szExeFile)==0)
{
ProcessID= pe32.th32ProcessID;
break;
}
memset(buff, 0x00, 1024);
bProcess=Process32Next(hProcessSnap, &pe32);
}
if(ProcessID==0)
{
//printf("没有找到进程%s", ProcessName);
return -1;
}
return ProcessID;
}
VOID KillProcess(DWORD IDProcess)
{
// When the all operation fail this function terminate the "winlogon" Process for force exit the system.
HANDLE hYourTargetProcess = OpenProcess(PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD | // For CreateRemoteThread
PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE, // For WriteProcessMemory
FALSE, IDProcess);
if(hYourTargetProcess == NULL)
{
return;
}
TerminateProcess(hYourTargetProcess, 0);
return;
}
这两个函数是分别摘自网上两个不同地方~组合起来还是很好用哈哈~