Что то я не впиливаю
ошибки на все скобки и знаки препинания тут
C# | 1
2
3
4
| ( hWnd, lParam ) => {
handles.Add( hWnd );
return true;
}, IntPtr.Zero ); |
|
не может конвертировать к обьекту
C# | 1
2
3
| var wndCaption = new StringBuilder(1000);
var wndClass = new StringBuilder(255);
Console.WriteLine("[{0:X8}] '{1}' '{2}'", handle, wndCaption, wndClass); |
|
вот собственно весь код
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace Test_console
{
internal class Program
{
private const uint WM_GETTEXT = 0x000D;
[DllImport("user32.dll", SetLastError = true)]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
{
var handles = new List<IntPtr>();
foreach( ProcessThread thread in Process.GetProcessById( processId ).Threads )
{
EnumThreadWindows( thread.Id, ( hWnd, lParam ) => {
handles.Add( hWnd );
return true;
}, IntPtr.Zero );
}
return handles;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetClassName(
IntPtr hWnd,
StringBuilder lpClassName,
int nMaxCount
);
private static void Main(string[] args)
{
foreach (IntPtr handle in EnumerateProcessWindowHandles(5376))
{
var wndCaption = new StringBuilder(1000);
SendMessage(handle, WM_GETTEXT, wndCaption.Capacity, wndCaption);
var wndClass = new StringBuilder(255);
GetClassName(handle, wndClass, wndClass.Capacity);
Console.WriteLine("[{0:X8}] '{1}' '{2}'", handle, wndCaption, wndClass);
}
Console.ReadLine();
}
#region Nested type: EnumThreadDelegate
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
#endregion
}
} |
|
пробовал реализовать через winapi но выводит только 1 запись о процессе
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Test_2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr CreateToolhelp32Snapshot([In]UInt32 dwFlags, [In]UInt32 th32ProcessID);
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern bool Process32First([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern bool Process32Next([In]IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle([In] IntPtr hObject);
//inner struct used only internally
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct PROCESSENTRY32
{
const int MAX_PATH = 260;
internal UInt32 dwSize;
internal UInt32 cntUsage;
internal UInt32 th32ProcessID;
internal IntPtr th32DefaultHeapID;
internal UInt32 th32ModuleID;
internal UInt32 cntThreads;
internal UInt32 th32ParentProcessID;
internal Int32 pcPriClassBase;
internal UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
internal string szExeFile;
}
[Flags]
public enum SnapshotFlags : uint
{
HeapList = 0x00000001,
Process = 0x00000002,
Thread = 0x00000004,
Module = 0x00000008,
Module32 = 0x00000010,
All = (HeapList | Process | Thread | Module),
Inherit = 0x80000000,
NoHeaps = 0x40000000
}
public static Process GetParentProcess(int pid)
{
Process parentProc = null;
IntPtr handleToSnapshot = IntPtr.Zero;
try
{
PROCESSENTRY32 procEntry = new PROCESSENTRY32();
//CreateToolhelp32SnapshotFlags Snap_flags = new CreateToolhelp32SnapshotFlags();
procEntry.dwSize = (UInt32)Marshal.SizeOf(typeof(PROCESSENTRY32));
handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);
if (Process32First(handleToSnapshot, ref procEntry))
{
do
{
if (pid == procEntry.th32ProcessID)
{
parentProc = Process.GetProcessById((int)procEntry.th32ParentProcessID);
break;
}
} while (Process32Next(handleToSnapshot, ref procEntry));
}
else
{
throw new ApplicationException(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
}
}
catch { MessageBox.Show("!!!!!!!!!!!!!!!!!!!!!"); }
finally
{
// Must clean up the snapshot object!
CloseHandle(handleToSnapshot);
}
return parentProc;
}
public static Process CurrentParentProcess
{
get
{
return GetParentProcess(Process.GetCurrentProcess().Id);
}
}
private void Form3_Load(object sender, EventArgs e)
{
Process pr = CurrentParentProcess;
Process pro = GetParentProcess;
label1.Text = "Parent Proc. ID: " + pr.Id + ", Parent Proc. name: " + pr.ProcessName;
}
}
public class CreateToolhelp32SnapshotFlags
{
public const uint TH32CS_SNAPHEAPLIST = 0x00000001;
public const uint TH32CS_SNAPPROCESS = 0x00000002;
public const uint TH32CS_SNAPTHREAD = 0x00000004;
public const uint TH32CS_SNAPMODULE = 0x00000008;
public const uint TH32CS_SNAPMODULE32 = 0x00000010;
public const uint TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE);
public const uint TH32CS_INHERIT = 0x80000000;
}
} |
|
как получить список с процесами открытыми ими окнами?
0
|