首页 新闻 会员 周边 捐助

c#为甚么遍历当前进程内的线程,获取到的句柄都是一样的?

0
悬赏园豆:10 [待解决问题]

c#代码如下

******************************************************************

//遍历线程

System.Diagnostics.Process process =   System.Diagnostics.Process.GetCurrentProcess();

uint snapshot_handle =   CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); THREADENTRY32 threadEntry =   new THREADENTRY32();

threadEntry.dwSize =   (uint)Marshal.SizeOf(threadEntry);

//获取主线程

IntPtr hCurrentHandle =   IntPtr.Zero;

DuplicateHandle(GetCurrentProcess(),    GetCurrentThread(), GetCurrentProcess(),    out hCurrentHandle, 0, false,    DUPLICATE_SAME_ACCESS);               

bool ret = Thread32First(snapshot_handle,   ref threadEntry);  

while (ret) {    

//确认线程属于当前进程    

if (process.Id == threadEntry.th32OwnerProcessID)     {        

   IntPtr hPseudoThread = OpenThread(ThreadAccess.PROCESS_ALL_ACCESS,            true, threadEntry.th32ThreadID);                   

   if (hPseudoThread != hCurrentHandle)        

   {            

       SetThreadToken(ref hPseudoThread, newtoken)            

       CloseHandle(hPseudoThread);         

   }        

   else        

   {             MessageBox.Show("主线程忽略!");         }    

}    

ret = Thread32Next(snapshot_handle,           ref threadEntry);

}

******************************************************************

存在两个问题:

1.为甚么同一个进程内的多个线程,获取的句柄却是一样的?

2.为甚么DuplicateHandle(GetCurrentProcess(),    GetCurrentThread(), GetCurrentProcess(),    out hCurrentHandle, 0, false,    DUPLICATE_SAME_ACCESS); 获取到的当前线程句柄号却没有在枚举循环中找到?     

Johnye的主页 Johnye | 初学一级 | 园豆:87
提问于:2016-04-29 10:26
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册