@@ -41,13 +41,13 @@ public class HostAppList : ObservableCollection<HostAppEntry>
41
41
{
42
42
public HostAppList ( )
43
43
{
44
- FromMachine ( ) ;
44
+ // FromMachine();
45
45
}
46
46
47
47
public void FromMachine ( )
48
48
{
49
49
50
- IEnumerable < HostAppEntry > newEntries = GetHostAppEntriesFromMachine ( ) ;
50
+ IEnumerable < HostAppEntry > newEntries = GetHostAppEntriesFromMachine ( ) . ToList < HostAppEntry > ( ) ;
51
51
// Use ToList to get a fixed collection that won't get angry that we're calling
52
52
// Add and Remove on it while enumerating.
53
53
foreach ( var entry in this . Except ( newEntries ) . ToList < HostAppEntry > ( ) )
@@ -85,7 +85,7 @@ private static IEnumerable<HostAppEntry> GetHostAppEntriesFromMachine()
85
85
if ( processType == null )
86
86
{
87
87
HostAppEntry entry = new HostAppEntry (
88
- process . Parent ( ) . GetMainModuleFileName ( ) ,
88
+ process . ParentProcess ( ) . GetMainModuleFileName ( ) ,
89
89
process . GetMainModuleFileName ( ) ,
90
90
userDataPath ) ;
91
91
yield return entry ;
@@ -148,4 +148,117 @@ public static string GetMainModuleFileName(this Process process, int buffer = 10
148
148
null ;
149
149
}
150
150
}
151
+
152
+ public static class ProcessExtensions2
153
+ {
154
+ /// <summary>
155
+ /// Returns the Parent Process of a Process
156
+ /// </summary>
157
+ /// <param name="process">The Windows Process.</param>
158
+ /// <returns>The Parent Process of the Process.</returns>
159
+ public static Process ParentProcess ( this Process process )
160
+ {
161
+ int parentPid = 0 ;
162
+ int processPid = process . Id ;
163
+ uint TH32CS_SNAPPROCESS = 2 ;
164
+
165
+ // Take snapshot of processes
166
+ IntPtr hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS , 0 ) ;
167
+
168
+ if ( hSnapshot == IntPtr . Zero )
169
+ {
170
+ return null ;
171
+ }
172
+
173
+ PROCESSENTRY32 procInfo = new PROCESSENTRY32 ( ) ;
174
+
175
+ procInfo . dwSize = ( uint ) Marshal . SizeOf ( typeof ( PROCESSENTRY32 ) ) ;
176
+
177
+ // Read first
178
+ if ( Process32First ( hSnapshot , ref procInfo ) == false )
179
+ {
180
+ return null ;
181
+ }
182
+
183
+ // Loop through the snapshot
184
+ do
185
+ {
186
+ // If it's me, then ask for my parent.
187
+ if ( processPid == procInfo . th32ProcessID )
188
+ {
189
+ parentPid = ( int ) procInfo . th32ParentProcessID ;
190
+ }
191
+ }
192
+ while ( parentPid == 0 && Process32Next ( hSnapshot , ref procInfo ) ) ; // Read next
193
+
194
+ if ( parentPid > 0 )
195
+ {
196
+ return Process . GetProcessById ( parentPid ) ;
197
+ }
198
+ else
199
+ {
200
+ return null ;
201
+ }
202
+ }
203
+
204
+ /// <summary>
205
+ /// Takes a snapshot of the specified processes, as well as the heaps,
206
+ /// modules, and threads used by these processes.
207
+ /// </summary>
208
+ /// <param name="dwFlags">
209
+ /// The portions of the system to be included in the snapshot.
210
+ /// </param>
211
+ /// <param name="th32ProcessID">
212
+ /// The process identifier of the process to be included in the snapshot.
213
+ /// </param>
214
+ /// <returns>
215
+ /// If the function succeeds, it returns an open handle to the specified snapshot.
216
+ /// If the function fails, it returns INVALID_HANDLE_VALUE.
217
+ /// </returns>
218
+ [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
219
+ private static extern IntPtr CreateToolhelp32Snapshot ( uint dwFlags , uint th32ProcessID ) ;
220
+
221
+ /// <summary>
222
+ /// Retrieves information about the first process encountered in a system snapshot.
223
+ /// </summary>
224
+ /// <param name="hSnapshot">A handle to the snapshot.</param>
225
+ /// <param name="lppe">A pointer to a PROCESSENTRY32 structure.</param>
226
+ /// <returns>
227
+ /// Returns TRUE if the first entry of the process list has been copied to the buffer.
228
+ /// Returns FALSE otherwise.
229
+ /// </returns>
230
+ [ DllImport ( "kernel32.dll" ) ]
231
+ private static extern bool Process32First ( IntPtr hSnapshot , ref PROCESSENTRY32 lppe ) ;
232
+
233
+ /// <summary>
234
+ /// Retrieves information about the next process recorded in a system snapshot.
235
+ /// </summary>
236
+ /// <param name="hSnapshot">A handle to the snapshot.</param>
237
+ /// <param name="lppe">A pointer to a PROCESSENTRY32 structure.</param>
238
+ /// <returns>
239
+ /// Returns TRUE if the next entry of the process list has been copied to the buffer.
240
+ /// Returns FALSE otherwise.</returns>
241
+ [ DllImport ( "kernel32.dll" ) ]
242
+ private static extern bool Process32Next ( IntPtr hSnapshot , ref PROCESSENTRY32 lppe ) ;
243
+
244
+ /// <summary>
245
+ /// Describes an entry from a list of the processes residing
246
+ /// in the system address space when a snapshot was taken.
247
+ /// </summary>
248
+ [ StructLayout ( LayoutKind . Sequential ) ]
249
+ private struct PROCESSENTRY32
250
+ {
251
+ public uint dwSize ;
252
+ public uint cntUsage ;
253
+ public uint th32ProcessID ;
254
+ public IntPtr th32DefaultHeapID ;
255
+ public uint th32ModuleID ;
256
+ public uint cntThreads ;
257
+ public uint th32ParentProcessID ;
258
+ public int pcPriClassBase ;
259
+ public uint dwFlags ;
260
+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 260 ) ]
261
+ public string szExeFile ;
262
+ }
263
+ }
151
264
}
0 commit comments