首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

妖哥,进来帮忙看看,多谢了

2012-05-16 
妖哥,各位高手,进来帮忙看看,谢谢了!C/C++ codeDWORD GetThreadID(HANDLE hThread,DWORD dwProcessId){HAN

妖哥,各位高手,进来帮忙看看,谢谢了!

C/C++ code
DWORD GetThreadID(HANDLE hThread,DWORD dwProcessId){    HANDLE hThreadSnap=INVALID_HANDLE_VALUE;    THREADENTRY32 te32;        #ifdef __GNUC__         typedef HANDLE (*OPENTHREAD)(DWORD,BOOL,DWORD);        OPENTHREAD OpenThread=::GetProcAddress(::GetModuleHandle("kernel32.dll"),"OpenThread");         #endif        hThreadSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);//获取进程列表快照    if(hThreadSnap==INVALID_HANDLE_VALUE){        return -1;    }    te32.dwSize = sizeof(THREADENTRY32);     if(::Thread32First(hThreadSnap,&te32)==FALSE){        return 1;    }    while(::Thread32Next(hThreadSnap,&te32)!=FALSE){//如果获取下一个线程成功        if(te32.th32OwnerProcessID==dwProcessId){//如果获取的进程ID与指定的进程ID相同            HANDLE hTempThread=OpenThread(THREAD_ALL_ACCESS,FALSE,te32.th32ThreadID);//根据结构体内的线程ID,打开线程得到句柄    if(hTempThread==hThread){//如果指定线程句柄与打开句柄相同//问题出在这里,我有2台4核电脑,一台双核,一台单核;单核机和双核机运行正常,两台四核电脑上运行找不到相同的句柄//一台四核装的win7,其他3台电脑装的都是完全相同的XP系统,在四核电脑上跟踪调试hTempThread都是3-4位数的句柄,//hThread都是5位数的句柄,为什么会这样呢?该怎么修改啊?                ::CloseHandle(hTempThread);                ::CloseHandle(hThreadSnap);                return te32.th32ThreadID;//返回句柄    }else{      ::CloseHandle(hTempThread);         }        }    }    return 0;}


主调程序:
C/C++ code
HWND hwndHandle = FindWindow("TForm_Client","辅助工具");if(hwndHandle){    unsigned long PID;    GetWindowThreadProcessId(hwndHandle,&PID);        HWND hdwWnd = FindWindowEx(hwndHandle, NULL,"TPageControl",NULL);    hdwWnd = FindWindowEx(hdwWnd, NULL,"TTabSheet","登录");    hdwWnd = FindWindowEx(hdwWnd, NULL,"TButton","登录.L");         PID=GetThreadID(hdwWnd,PID);         ……}


[解决办法]
不曾注意过双核或4核CPU的区别,至于枚举进程这里,楼主你试试EnumProcess

热点排行