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); 获取到的当前线程句柄号却没有在枚举循环中找到?