public static ThreadEntry[] GetThreads(uint processID) { ArrayList threadList = new ArrayList(); IntPtr handle = ToolhelpAPI.CreateToolhelp32Snapshot(ToolhelpAPI.TH32CS_SNAPTHREAD, processID); if ((int)handle > 0) { try { THREADENTRY32 teCurrent; THREADENTRY32 te32 = new THREADENTRY32(); //Get byte array to pass to the API calls byte[] teBytes = te32.ToByteArray(); //Get the first process int retval = ToolhelpAPI.Thread32First(handle, teBytes); while (retval == 1) { //Convert bytes to the class teCurrent = new THREADENTRY32(teBytes); if ((processID == 0) || (teCurrent.th32OwnerProcessID == processID)) { //New instance ThreadEntry tentry = new ThreadEntry(teCurrent); threadList.Add(tentry); } retval = ToolhelpAPI.Thread32Next(handle, teBytes); } } catch (Exception ex) { throw new Exception("Exception: " + ex.Message); } //Close handle ToolhelpAPI.CloseToolhelp32Snapshot(handle); return((ThreadEntry[])threadList.ToArray(typeof(ThreadEntry))); } else { throw new Exception("Unable to create snapshot"); } }
private ThreadEntry(THREADENTRY32 te) { m_te = te; }