我需要按名称,ID或进程句柄处理另一个正在运行的应用程序。我得到了ID和Process句柄,但我不知道如何处理窗口以更改外部程序标题。
有我的代码:
BOOL CFindProcess::OnInitDialog()
{
CDialogEx::OnInitDialog();
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_PROCESS);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 proc;
proc.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnap, &proc)){
pComboBox->AddString(proc.szExeFile);
while (Process32Next(hSnap, &proc)){
if (0!=wcscmp(proc.szExeFile, L"svchost.exe"))
pComboBox->AddString(proc.szExeFile);
if (wcscmp(proc.szExeFile, L"notepad.exe") == 0){
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc.th32ProcessID);
GetProcessId(hProcess);
AfxMessageBox(L"Handeled");
//SetWindowText(hProcess, L"Weather");
CloseHandle(hProcess);
}
}
}
CloseHandle(hSnap);
return TRUE;
}
我的问题是如何通过名称或ID获取notepad.exe的窗口句柄,使用MFC处理句柄?
答案 0 :(得分:2)
此代码演示了Jerry所指的内容:
struct MYFINDSTRUCT
{
DWORD dwPID;
HWND hWnd;
};
BOOL CALLBACK MyWndEnum(HWND hwnd,LPARAM lParam)
{
MYFINDSTRUCT* pP = (MYFINDSTRUCT*)lParam;
DWORD dwPID = 0;
GetWindowThreadProcessId(hwnd,&dwPID);
if(dwPID==pP->dwPID)
{
pP->hWnd = hwnd;
return 0;
}
return 1;
}
HWND GetProcessHWND(unsigned int nPID)
{
MYFINDSTRUCT p;
p.hWnd = 0;
p.dwPID = nPID;
EnumWindows(MyWndEnum,(LPARAM)&p);
return p.hWnd;
}