278 {
280 string key = Guid.NewGuid().ToString();
281 List<Process> processes = new List<Process>();
282
283 int res = RmStartSession(out handle, 0, key);
284
285 if (res != 0)
286 throw new Exception("Could not begin restart session. Unable to determine file locker.");
287
288 try
289 {
290 const int ERROR_MORE_DATA = 234;
291 uint pnProcInfoNeeded = 0,
292 pnProcInfo = 0,
293 lpdwRebootReasons = RmRebootReasonNone;
294
295 string[] resources = new string[] { path };
296
297 res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null);
298
299 if (res != 0)
300 throw new Exception("Could not register resource.");
301
302
303
304
305 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
306
307 if (res == ERROR_MORE_DATA)
308 {
309
310 RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
311 pnProcInfo = pnProcInfoNeeded;
312
313
314 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
315
316 if (res == 0)
317 {
318 processes = new List<Process>((int)pnProcInfo);
319
320
321
322 for (int i = 0; i < pnProcInfo; i++)
323 {
324 try
325 {
326 processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
327 }
328
329 catch (ArgumentException) { }
330 }
331 }
332 else
333 throw new Exception("Could not list processes locking resource.");
334 }
335 else if (res != 0)
336 throw new Exception("Could not list processes locking resource. Failed to get size of result.");
337 }
338 finally
339 {
340 RmEndSession(handle);
341 }
342
343 return processes;
344 }