#include <tlhelp32.h>
HWND FindWindowHandle;
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam);
void CFindHandleDlg::OnButton1() 
{
// TODO: Add your control notification handler code here
UpdateData();

PROCESSENTRY32 pe[50];
HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (!hSnapshot)
{
//printf("Unable to create snapshot!\n");
::MessageBoxA(NULL, "CreateToolhelp32Snapshot() : Fail!", NULL,NULL);
}


// prepare to enumerate the processes....
//PROCESSENTRY32 pe[50];
ULONG count=0;

// remember to init the structure...
pe[0].dwSize=sizeof(PROCESSENTRY32);
BOOL retval=Process32First(hSnapshot,&pe[0]);
while(retval)
{
// increment the process count
count++;
pe[count].dwSize=sizeof(PROCESSENTRY32);
// display the process name and ID
retval=Process32Next(hSnapshot,&pe[count]);
}

// since we are done, remember to close the snapshot handle
CloseHandle(hSnapshot);

for(int i=0; i<50; i++)
{
char sss[100];
//::wcstombs(sss, (const unsigned short *)pe[i].szExeFile, 100);
if(strcmp(pe[i].szExeFile, (LPSTR)(LPCTSTR)m_strProcessImageName)==0)
{
if(::EnumWindows(&EnumWindowsProc,(LPARAM)pe[i].th32ProcessID)==false)
{
sprintf(sss, "%x", FindWindowHandle);
AfxMessageBox(sss);
}
}
}
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
DWORD pid;
    GetWindowThreadProcessId(hwnd,&pid);
    if(lParam==pid && IsWindowVisible(hwnd))
    {
#if defined(_DEBUG)
        TCHAR name[255]={0,};
        GetWindowText(hwnd,name,255);
        TRACE("%s 0x%08x(%d) 0x%08x(%d)\n",name, hwnd,hwnd,pid,pid);
#endif
        FindWindowHandle = hwnd;
        return false;
    }
    return true;
}
Posted by 띠깜
,