MSDN のドキュメントによると、GetWindowThreadProcessId は、「指定されたウィンドウを作成したスレッドの識別子と、オプションで、ウィンドウを作成したプロセスの識別子を取得します」を返します。
ここで、次のプログラムでは、進行中のFirefoxのすべてのインスタンスをリストしようとしていました
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
class SpyDemo {
private static List<IntPtr> _result = new List<IntPtr>();
private delegate int EnumWindowCallbackFn(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowCallbackFn x, int y);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
private static StreamWriter writer = new StreamWriter("D:\\Log4.txt", false);
public static void Main() {
foreach (Process processInfo in Process.GetProcessesByName("firefox"))
{
//using (StreamWriter writer = new StreamWriter("D:\\Log2.txt", true)) {
writer.WriteLine("process {0}, {1}", processInfo.ProcessName, processInfo.Id); //writes in file
foreach (ProcessThread threadInfo in processInfo.Threads)
{
/*
* iterates through every thread of a process
*/
//Console.WriteLine("\t{0}", threadInfo.Id);
writer.WriteLine("Thread Id passes: " + threadInfo.Id);
foreach (IntPtr item in GetWindowHandleForThread(threadInfo.Id))
{
writer.WriteLine("\t"+item);
}
}
//}
}
writer.Dispose();
Console.ReadKey();
}
private static IntPtr[] GetWindowHandleForThread(int threadHandle) {
_result.Clear();
EnumWindowCallbackFn myEnumWindowCallbackFn = new EnumWindowCallbackFn(WindowEnum);
EnumWindows(myEnumWindowCallbackFn, threadHandle);
return _result.ToArray();
}
private static int WindowEnum(IntPtr hwnd, int lParam) {
int processId = 0;
int threadId = GetWindowThreadProcessId(hwnd, out processId);
//using (StreamWriter writer1= new StreamWriter("D:\\Log4.txt", true)) {
writer.WriteLine("\t hwnd: {3}, lParam: {0}, threadID: {1}, processId: {2}", lParam, threadId, processId, hwnd);
if (threadId == lParam)
{
_result.Add(hwnd);
writer.WriteLine("\t Matched");
}
//}
return 1;
}
}
しかし、私が得ているのはこのようなものです
process firefox, 648
Thread Id passes: 676
hwnd: 65700, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65674, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65678, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65682, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65684, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65654, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65698, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65676, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65646, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65640, lParam: 676, threadID: 4396, processId: 2728
hwnd: 197400, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 65714, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131294, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131266, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131286, lParam: 676, threadID: 4396, processId: 2728
hwnd: 196826, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131326, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131288, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131252, lParam: 676, threadID: 4396, processId: 2728
hwnd: 1114854, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131314, lParam: 676, threadID: 4396, processId: 2728
hwnd: 1443328, lParam: 676, threadID: 5212, processId: 2728
hwnd: 65680, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65662, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65658, lParam: 676, threadID: 4396, processId: 2728
hwnd: 327846, lParam: 676, threadID: 4396, processId: 2728
hwnd: 984070, lParam: 676, threadID: 6380, processId: 8112
hwnd: 132290, lParam: 676, threadID: 7276, processId: 6320
hwnd: 459784, lParam: 676, threadID: 7084, processId: 2728
hwnd: 66260, lParam: 676, threadID: 4916, processId: 4912
hwnd: 66254, lParam: 676, threadID: 4916, processId: 4912
hwnd: 263886, lParam: 676, threadID: 7396, processId: 2728
hwnd: 65834, lParam: 676, threadID: 2720, processId: 2728
hwnd: 132186, lParam: 676, threadID: 7276, processId: 6320
hwnd: 197734, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1310950, lParam: 676, threadID: 7276, processId: 6320
hwnd: 328868, lParam: 676, threadID: 7276, processId: 6320
hwnd: 525444, lParam: 676, threadID: 7276, processId: 6320
hwnd: 328800, lParam: 676, threadID: 7276, processId: 6320
hwnd: 66034, lParam: 676, threadID: 5412, processId: 4904
hwnd: 66014, lParam: 676, threadID: 5328, processId: 4828
hwnd: 131418, lParam: 676, threadID: 5112, processId: 2728
hwnd: 327852, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65622, lParam: 676, threadID: 4080, processId: 4048
hwnd: 65620, lParam: 676, threadID: 4080, processId: 4048
hwnd: 2688416, lParam: 676, threadID: 8180, processId: 3068
hwnd: 3474992, lParam: 676, threadID: 8180, processId: 3068
hwnd: 2426462, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1115184, lParam: 676, threadID: 7276, processId: 6320
hwnd: 132196, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1704778, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1378230, lParam: 676, threadID: 7964, processId: 3068
hwnd: 459528, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 722064, lParam: 676, threadID: 6380, processId: 8112
hwnd: 1967586, lParam: 676, threadID: 6380, processId: 8112
hwnd: 3867504, lParam: 676, threadID: 6380, processId: 8112
hwnd: 919188, lParam: 676, threadID: 6380, processId: 8112
hwnd: 2950808, lParam: 676, threadID: 6380, processId: 8112
hwnd: 853010, lParam: 676, threadID: 6380, processId: 8112
hwnd: 918728, lParam: 676, threadID: 6380, processId: 8112
hwnd: 1901550, lParam: 676, threadID: 6380, processId: 8112
hwnd: 2819496, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66086, lParam: 676, threadID: 5368, processId: 5364
hwnd: 262896, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66196, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66194, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66084, lParam: 676, threadID: 5368, processId: 5364
hwnd: 262950, lParam: 676, threadID: 6184, processId: 648
hwnd: 196812, lParam: 676, threadID: 4396, processId: 2728
hwnd: 131268, lParam: 676, threadID: 4396, processId: 2728
hwnd: 3343794, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 1574684, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 1771210, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 590606, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 3015940, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 984652, lParam: 676, threadID: 7084, processId: 2728
hwnd: 656370, lParam: 676, threadID: 7084, processId: 2728
hwnd: 918600, lParam: 676, threadID: 7084, processId: 2728
hwnd: 919140, lParam: 676, threadID: 7084, processId: 2728
hwnd: 1836296, lParam: 676, threadID: 7084, processId: 2728
hwnd: 1377222, lParam: 676, threadID: 7084, processId: 2728
hwnd: 3998764, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1639852, lParam: 676, threadID: 7276, processId: 6320
hwnd: 591316, lParam: 676, threadID: 7396, processId: 2728
hwnd: 460376, lParam: 676, threadID: 7396, processId: 2728
hwnd: 788020, lParam: 676, threadID: 7396, processId: 2728
hwnd: 525704, lParam: 676, threadID: 7396, processId: 2728
hwnd: 656886, lParam: 676, threadID: 7396, processId: 2728
hwnd: 3343412, lParam: 676, threadID: 7396, processId: 2728
hwnd: 721922, lParam: 676, threadID: 6380, processId: 8112
hwnd: 984124, lParam: 676, threadID: 6380, processId: 8112
hwnd: 1115058, lParam: 676, threadID: 6380, processId: 8112
hwnd: 853686, lParam: 676, threadID: 6380, processId: 8112
hwnd: 525344, lParam: 676, threadID: 7084, processId: 2728
hwnd: 14222208, lParam: 676, threadID: 7396, processId: 2728
hwnd: 918702, lParam: 676, threadID: 6696, processId: 2728
hwnd: 787714, lParam: 676, threadID: 6592, processId: 2728
hwnd: 1508200, lParam: 676, threadID: 7164, processId: 2728
hwnd: 460024, lParam: 676, threadID: 7448, processId: 6320
hwnd: 263336, lParam: 676, threadID: 7276, processId: 6320
hwnd: 3802290, lParam: 676, threadID: 7276, processId: 6320
hwnd: 525478, lParam: 676, threadID: 7276, processId: 6320
hwnd: 394078, lParam: 676, threadID: 7276, processId: 6320
hwnd: 394298, lParam: 676, threadID: 7276, processId: 6320
hwnd: 524584, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1311552, lParam: 676, threadID: 7276, processId: 6320
hwnd: 132178, lParam: 676, threadID: 220, processId: 6320
hwnd: 132244, lParam: 676, threadID: 220, processId: 6320
hwnd: 132184, lParam: 676, threadID: 4680, processId: 6320
hwnd: 590698, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1770282, lParam: 676, threadID: 7276, processId: 6320
hwnd: 852880, lParam: 676, threadID: 7276, processId: 6320
hwnd: 525188, lParam: 676, threadID: 7276, processId: 6320
hwnd: 852866, lParam: 676, threadID: 7276, processId: 6320
hwnd: 852834, lParam: 676, threadID: 7276, processId: 6320
hwnd: 196666, lParam: 676, threadID: 5960, processId: 2728
hwnd: 196958, lParam: 676, threadID: 5108, processId: 2728
hwnd: 66122, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66120, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66116, lParam: 676, threadID: 5368, processId: 5364
hwnd: 262732, lParam: 676, threadID: 6140, processId: 6116
hwnd: 66114, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66264, lParam: 676, threadID: 6012, processId: 4912
hwnd: 131742, lParam: 676, threadID: 2136, processId: 4600
hwnd: 852624, lParam: 676, threadID: 4916, processId: 4912
hwnd: 66176, lParam: 676, threadID: 3148, processId: 4940
hwnd: 66174, lParam: 676, threadID: 4944, processId: 4940
hwnd: 262696, lParam: 676, threadID: 4944, processId: 4940
hwnd: 459112, lParam: 676, threadID: 4944, processId: 4940
hwnd: 327986, lParam: 676, threadID: 4944, processId: 4940
hwnd: 66170, lParam: 676, threadID: 4944, processId: 4940
hwnd: 66166, lParam: 676, threadID: 3220, processId: 4940
hwnd: 66156, lParam: 676, threadID: 3932, processId: 5824
hwnd: 66154, lParam: 676, threadID: 4620, processId: 4616
hwnd: 66146, lParam: 676, threadID: 5828, processId: 5824
hwnd: 66142, lParam: 676, threadID: 3892, processId: 5824
hwnd: 66140, lParam: 676, threadID: 5640, processId: 5636
hwnd: 66138, lParam: 676, threadID: 5640, processId: 5636
hwnd: 66130, lParam: 676, threadID: 4300, processId: 5636
hwnd: 131644, lParam: 676, threadID: 4272, processId: 5824
hwnd: 66104, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66082, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66074, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66064, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66058, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66046, lParam: 676, threadID: 5100, processId: 2728
hwnd: 66030, lParam: 676, threadID: 5408, processId: 4904
hwnd: 66026, lParam: 676, threadID: 5404, processId: 4904
hwnd: 131558, lParam: 676, threadID: 5392, processId: 4904
hwnd: 131522, lParam: 676, threadID: 5112, processId: 2728
hwnd: 131420, lParam: 676, threadID: 5112, processId: 2728
hwnd: 131116, lParam: 676, threadID: 5108, processId: 2728
hwnd: 131106, lParam: 676, threadID: 5112, processId: 2728
hwnd: 131100, lParam: 676, threadID: 5100, processId: 2728
hwnd: 196880, lParam: 676, threadID: 5100, processId: 2728
hwnd: 131502, lParam: 676, threadID: 4880, processId: 4616
hwnd: 65962, lParam: 676, threadID: 4460, processId: 4456
hwnd: 65958, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65956, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65954, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65952, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65950, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65948, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65946, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65944, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65940, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65938, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65936, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65934, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65930, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65926, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65922, lParam: 676, threadID: 4504, processId: 4500
hwnd: 65912, lParam: 676, threadID: 4744, processId: 4532
hwnd: 65898, lParam: 676, threadID: 4536, processId: 4532
hwnd: 131426, lParam: 676, threadID: 4620, processId: 4616
hwnd: 590160, lParam: 676, threadID: 4544, processId: 4540
hwnd: 65870, lParam: 676, threadID: 4496, processId: 2728
hwnd: 65860, lParam: 676, threadID: 4468, processId: 2728
hwnd: 65838, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65690, lParam: 676, threadID: 4408, processId: 2728
hwnd: 65688, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65636, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65628, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65624, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65608, lParam: 676, threadID: 4160, processId: 3232
hwnd: 65604, lParam: 676, threadID: 4044, processId: 3232
hwnd: 131134, lParam: 676, threadID: 3844, processId: 4048
hwnd: 196656, lParam: 676, threadID: 4064, processId: 4048
hwnd: 65612, lParam: 676, threadID: 3140, processId: 3124
hwnd: 131150, lParam: 676, threadID: 4080, processId: 4048
hwnd: 66000, lParam: 676, threadID: 4832, processId: 4828
hwnd: 65994, lParam: 676, threadID: 4832, processId: 4828
hwnd: 65990, lParam: 676, threadID: 5312, processId: 4828
hwnd: 66134, lParam: 676, threadID: 5640, processId: 5636
hwnd: 329202, lParam: 676, threadID: 5328, processId: 4828
hwnd: 66008, lParam: 676, threadID: 5328, processId: 4828
hwnd: 66004, lParam: 676, threadID: 5328, processId: 4828
hwnd: 525060, lParam: 676, threadID: 2720, processId: 2728
hwnd: 787990, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65812, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65644, lParam: 676, threadID: 4396, processId: 2728
hwnd: 65638, lParam: 676, threadID: 4396, processId: 2728
hwnd: 395040, lParam: 676, threadID: 5212, processId: 2728
hwnd: 460500, lParam: 676, threadID: 4916, processId: 4912
hwnd: 197276, lParam: 676, threadID: 4916, processId: 4912
hwnd: 66036, lParam: 676, threadID: 5412, processId: 4904
hwnd: 328464, lParam: 676, threadID: 5124, processId: 4648
hwnd: 394012, lParam: 676, threadID: 5124, processId: 4648
hwnd: 2491404, lParam: 676, threadID: 8180, processId: 3068
hwnd: 2229134, lParam: 676, threadID: 8180, processId: 3068
hwnd: 525426, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1639246, lParam: 676, threadID: 7276, processId: 6320
hwnd: 1180626, lParam: 676, threadID: 7964, processId: 3068
hwnd: 1770510, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 590598, lParam: 676, threadID: 676, processId: 648
Matched
hwnd: 656434, lParam: 676, threadID: 6380, processId: 8112
hwnd: 787432, lParam: 676, threadID: 6380, processId: 8112
hwnd: 66102, lParam: 676, threadID: 5368, processId: 5364
hwnd: 66060, lParam: 676, threadID: 5368, processId: 5364
hwnd: 591536, lParam: 676, threadID: 7084, processId: 2728
hwnd: 1312154, lParam: 676, threadID: 7084, processId: 2728
hwnd: 65626, lParam: 676, threadID: 2720, processId: 2728
hwnd: 65848, lParam: 676, threadID: 2720, processId: 2728
hwnd: 656900, lParam: 676, threadID: 7396, processId: 2728
hwnd: 7079376, lParam: 676, threadID: 7396, processId: 2728
hwnd: 459588, lParam: 676, threadID: 6696, processId: 2728
hwnd: 525680, lParam: 676, threadID: 6592, processId: 2728
hwnd: 591206, lParam: 676, threadID: 7164, processId: 2728
hwnd: 132182, lParam: 676, threadID: 220, processId: 6320
hwnd: 132174, lParam: 676, threadID: 4680, processId: 6320
hwnd: 196668, lParam: 676, threadID: 5960, processId: 2728
hwnd: 459070, lParam: 676, threadID: 6140, processId: 6116
hwnd: 66266, lParam: 676, threadID: 6012, processId: 4912
hwnd: 66248, lParam: 676, threadID: 2136, processId: 4600
hwnd: 66212, lParam: 676, threadID: 2136, processId: 4600
hwnd: 66178, lParam: 676, threadID: 3148, processId: 4940
hwnd: 66172, lParam: 676, threadID: 4944, processId: 4940
hwnd: 66168, lParam: 676, threadID: 3220, processId: 4940
hwnd: 66160, lParam: 676, threadID: 3932, processId: 5824
hwnd: 66148, lParam: 676, threadID: 5828, processId: 5824
hwnd: 66144, lParam: 676, threadID: 3892, processId: 5824
hwnd: 66132, lParam: 676, threadID: 4300, processId: 5636
hwnd: 66128, lParam: 676, threadID: 4272, processId: 5824
hwnd: 66032, lParam: 676, threadID: 5408, processId: 4904
hwnd: 66028, lParam: 676, threadID: 5404, processId: 4904
hwnd: 131560, lParam: 676, threadID: 5392, processId: 4904
hwnd: 131110, lParam: 676, threadID: 5108, processId: 2728
hwnd: 131114, lParam: 676, threadID: 5112, processId: 2728
hwnd: 131094, lParam: 676, threadID: 5100, processId: 2728
hwnd: 65968, lParam: 676, threadID: 4880, processId: 4616
hwnd: 65964, lParam: 676, threadID: 4460, processId: 4456
hwnd: 65928, lParam: 676, threadID: 4476, processId: 4472
hwnd: 65924, lParam: 676, threadID: 4504, processId: 4500
hwnd: 65918, lParam: 676, threadID: 4536, processId: 4532
hwnd: 65914, lParam: 676, threadID: 4744, processId: 4532
hwnd: 65900, lParam: 676, threadID: 4536, processId: 4532
hwnd: 65892, lParam: 676, threadID: 4620, processId: 4616
hwnd: 65874, lParam: 676, threadID: 4544, processId: 4540
hwnd: 65610, lParam: 676, threadID: 4160, processId: 3232
hwnd: 65600, lParam: 676, threadID: 3844, processId: 4048
hwnd: 196658, lParam: 676, threadID: 4064, processId: 4048
hwnd: 65616, lParam: 676, threadID: 4080, processId: 4048
hwnd: 65996, lParam: 676, threadID: 4832, processId: 4828
hwnd: 65992, lParam: 676, threadID: 5312, processId: 4828
hwnd: 66136, lParam: 676, threadID: 5640, processId: 5636
hwnd: 66012, lParam: 676, threadID: 5328, processId: 4828
hwnd: 66006, lParam: 676, threadID: 5328, processId: 4828
197400
459528
3343794
1574684
1771210
590606
3015940
1770510
590598
Thread Id passes: 6784
hwnd: 65700, lParam: 6784, threadID: 4396, processId: 2728
hwnd: 65674, lParam: 6784, threadID: 4396, processId: 2728
hwnd: 65678, lParam: 6784, threadID: 4396, processId: 2728
hwnd: 65682, lParam: 6784, threadID: 4396, processId: 2728
hwnd: 65684, lParam: 6784, threadID: 4396, processId: 2728
hwnd: 65654, lParam: 6784, threadID: 4396, processId: 2728
出力には 11545 行が含まれます。そのサブセットのみを貼り付けました。
私の質問は、firefox の processId が 648 (出力の最初の行) の場合、その後のすべてprocessId:
が648
. なぜ2728、8112などのようなものなのか.