1、函數法
函數原型
HWND hwnd = FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
使用
//IPTip_Main_Window 爲Windows軟鍵盤
HWND MyHandle = FindWindow(L"IPTip_Main_Window",NULL);
if(!IsWindowEnabled(MyHandle))//不可用
{
}
else//可用
{
}
注意:如果要查找的是外部安裝的程序,安裝之後可能需要重啓操作系統才能正確找到
2、遍歷法
// testlog.cpp : 定義控制檯應用程序的入口點。
#include "stdafx.h"
#include<iostream>
#include<Windows.h>
#include<TlHelp32.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR c[]={L"cmd.exe"}; //定義字符串並初始化,c爲8長度,最後結尾有'\0',定義一個字符爲'x',
HANDLE handle; //定義CreateToolhelp32Snapshot系統快照句柄
HANDLE handle1; //定義要結束進程句柄
handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//獲得系統快照句柄
PROCESSENTRY32 *info; //定義PROCESSENTRY32結構字指
//PROCESSENTRY32 結構的 dwSize 成員設置成 sizeof(PROCESSENTRY32)
info=new PROCESSENTRY32;
info->dwSize=sizeof(PROCESSENTRY32);
//調用一次 Process32First 函數,從快照中獲取進程列表
Process32First(handle,info);
//重複調用 Process32Next,直到函數返回 FALSE 爲止
while(Process32Next(handle,info)!=FALSE)
{
info->szExeFile; //指向進程名字
//strcmp字符串比較函數同要結束相同
if( wcscmp(c,info->szExeFile) == 0 )
{
//MessageBox (NULL, info->szExeFile, TEXT ("HelloMsg"), 0);
//PROCESS_TERMINATE表示爲結束操作打開,FALSE=可繼承,info->th32ProcessID=進程ID
handle1=OpenProcess(PROCESS_TERMINATE,FALSE,info->th32ProcessID);
//結束進程
TerminateProcess(handle1,0);
}
}
CloseHandle(handle);
return 0;
}