/[RomCheater]/trunk/Win32/libWin32/Win32/Threading/ProcessModeleInfoEx.cs
ViewVC logotype

Contents of /trunk/Win32/libWin32/Win32/Threading/ProcessModeleInfoEx.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 101 - (show annotations) (download)
Thu May 10 09:53:16 2012 UTC (8 years, 4 months ago) by william
File size: 5004 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.Diagnostics;
7
8 namespace libWin32.Win32.Threading
9 {
10 public class ProcessModeleInfoEx
11 {
12 #region Interop
13 [DllImport("psapi.dll")]
14 private static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, uint nSize);
15 [DllImport("psapi.dll")]
16 private static extern uint GetProcessImageFileName(IntPtr hProcess, StringBuilder lpImageFileName, uint nSize);
17 [DllImport("kernel32.dll")]
18 private static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, StringBuilder lpExeName, ref uint lpdwSize);
19 [DllImport("kernel32.dll")]
20 private static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, uint uuchMax);
21 [DllImport("kernel32.dll")]
22 private static extern IntPtr GetModuleHandle(string lpModuleName);
23 [DllImport("user32.dll")]
24 private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
25 #endregion
26 private const uint PathBufferSize = 512; // plenty big enough
27 private readonly static StringBuilder _PathBuffer = new StringBuilder((int)PathBufferSize);
28
29 #region GetExecutablePath
30 public static string GetMainModulePath(Process p)
31 {
32 return GetExecutablePath((uint)p.Id);
33 }
34 private static string GetExecutablePath(uint processid)
35 {
36 //if (hwnd == IntPtr.Zero) { return string.Empty; } // not a valid window handle
37
38 // Get the process id
39
40 //GetWindowThreadProcessId(hwnd, out processid);
41 // Try the GetModuleFileName method first since it's the fastest.
42 // May return ACCESS_DENIED (due to VM_READ flag) if the process is not owned by the current user.
43 // Will fail if we are compiled as x86 and we're trying to open a 64 bit process...not allowed.
44 IntPtr hprocess = ThreadControl.OpenProcess(ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VMRead, false, processid);
45 if (hprocess != IntPtr.Zero)
46 {
47 try
48 {
49 if (GetModuleFileNameEx(hprocess, IntPtr.Zero, _PathBuffer, PathBufferSize) > 0)
50 {
51 return _PathBuffer.ToString();
52 }
53 }
54 finally
55 {
56 ThreadControl.CloseHandle(hprocess);
57 }
58 }
59
60 hprocess = ThreadControl.OpenProcess(ProcessAccessFlags.QueryInformation, false, processid);
61 if (hprocess != IntPtr.Zero)
62 {
63 try
64 {
65 // Try this method for Vista or higher operating systems
66 uint size = PathBufferSize;
67 if ((Environment.OSVersion.Version.Major >= 6) &&
68 (QueryFullProcessImageName(hprocess, 0, _PathBuffer, ref size) && (size > 0)))
69 {
70 return _PathBuffer.ToString();
71 }
72
73 // Try the GetProcessImageFileName method
74 if (GetProcessImageFileName(hprocess, _PathBuffer, PathBufferSize) > 0)
75 {
76 string dospath = _PathBuffer.ToString();
77 foreach (string drive in Environment.GetLogicalDrives())
78 {
79 if (QueryDosDevice(drive.TrimEnd('\\'), _PathBuffer, PathBufferSize) > 0)
80 {
81 if (dospath.StartsWith(_PathBuffer.ToString()))
82 {
83 return drive + dospath.Remove(0, _PathBuffer.Length);
84 }
85 }
86 }
87 }
88 }
89 finally
90 {
91 ThreadControl.CloseHandle(hprocess);
92 }
93 }
94
95 return string.Empty;
96 }
97 #endregion
98
99 //#region GetProcess()
100 //public static string[] GetProcesses()
101 //{
102 // List<string> results = new List<string>();
103 // Process[] procArr = Process.GetProcesses();
104 // string tmp;
105 // foreach (Process CurProc in procArr)
106 // {
107 // tmp = GetExecutablePath(CurProc.MainWindowHandle);
108 // if (tmp != string.Empty)
109 // {
110 // results.Add(GetExecutablePath(CurProc.MainWindowHandle));
111 // }
112 // }
113 // return results.ToArray();
114 //}
115 //#endregion
116 }
117 }

  ViewVC Help
Powered by ViewVC 1.1.22