我在使用API枚举线程窗口时,停止响应了,代码如下:
if (::Process32First(m_hProcessSnap,&m_pe32))
{
do
{
m_hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,FALSE,m_pe32.th32ProcessID); if (::Thread32First(m_hThreadSnap,&m_te32))
{
do
{
if (m_pe32.th32ProcessID == m_te32.th32OwnerProcessID)
{
::EnumThreadWindows(m_te32.th32ThreadID,
(WNDENUMPROC)ThreadWindows,
(LPARAM)(LPCTSTR)cstrFileName);
}
}
while (::Thread32Next(m_hThreadSnap,&m_te32));
}
}
while (::Process32Next(m_hProcessSnap,&m_pe32));
}BOOL CALLBACK ThreadWindows(HWND hWnd,LPARAM lParam)
{ void* pStr = NULL;
pStr = new unsigned char[MAX_PATH];
if(!pStr)
return FALSE;
::GetWindowText(hWnd,(char*)pStr,MAX_PATH);
delete[] pStr; return TRUE;
}
if (::Process32First(m_hProcessSnap,&m_pe32))
{
do
{
m_hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,FALSE,m_pe32.th32ProcessID); if (::Thread32First(m_hThreadSnap,&m_te32))
{
do
{
if (m_pe32.th32ProcessID == m_te32.th32OwnerProcessID)
{
::EnumThreadWindows(m_te32.th32ThreadID,
(WNDENUMPROC)ThreadWindows,
(LPARAM)(LPCTSTR)cstrFileName);
}
}
while (::Thread32Next(m_hThreadSnap,&m_te32));
}
}
while (::Process32Next(m_hProcessSnap,&m_pe32));
}BOOL CALLBACK ThreadWindows(HWND hWnd,LPARAM lParam)
{ void* pStr = NULL;
pStr = new unsigned char[MAX_PATH];
if(!pStr)
return FALSE;
::GetWindowText(hWnd,(char*)pStr,MAX_PATH);
delete[] pStr; return TRUE;
}
解决方案 »
- 基于MFC dialog 的多条数据曲线绘制与屏蔽问题请教
- 如何让avtiveX控件固定大小?不能改变大小。容器运行不可见。
- CWinThread::ExitInstance
- 关于MapObjects for VC 的二次开发
- FlashGet是用什么工具开发,用什么方法开发的?
- 请问怎么在HTML中设置ActiveX控件的属性啊?
- bind()在TCP和UDP下的问题
- 急!答对现场给分,在vc阿中如何用ado分别得到连接查询两个表中相同字段的内容,
- 请问如何在CMyApp::Initinstance中得到命令行参数?
- 调查一下VC程序员都有什么爱好?
- 软件发布时,怎样将dll文件包含进去
- 用AnimateWindow函数要注意什么?
void CInstallUserDLLApp::EndProgram(CString strExeName)
{
CString strExeNameToFind = strExeName;
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(SnapShot==NULL)
{
//MessageBox("检测当前进程失败!");
return ;
}
SHFILEINFO shSmall;
CString str,strTemp;
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status=Process32First(SnapShot,&ProcessInfo);
int m_nProcess=0;
DWORD m_ProcessID=0;
while(Status)
{
m_nProcess++;
ZeroMemory(&shSmall,sizeof(shSmall));
//获取进程文件信息
SHGetFileInfo(ProcessInfo.szExeFile,0,&shSmall,
sizeof(shSmall),SHGFI_ICON|SHGFI_SMALLICON);
strTemp=ProcessInfo.szExeFile;
strTemp.MakeUpper();
strExeNameToFind.MakeUpper();
if(strTemp.Find(strExeNameToFind,0) >= 0)
{
m_ProcessID=ProcessInfo.th32ProcessID;
break;
}
//获取下一个进程的信息
Status=Process32Next(SnapShot,&ProcessInfo);
}
// TODO: Add extra validation here
if(m_ProcessID!=0)
{
HANDLE hProcess= OpenProcess( PROCESS_ALL_ACCESS, FALSE, m_ProcessID);
if(::TerminateProcess(hProcess,1))
{
//AfxMessageBox(" The process has been successfully killed",MB_OK);
;
}
}
}