Parent Directory
|
Revision Log
|
Patch
--- trunk/Win32/libWin32/Win32/Threading/ThreadControl.cs 2012/05/09 20:52:20 88 +++ trunk/Win32/libWin32/Win32/Threading/ThreadControl.cs 2012/05/10 09:03:13 100 @@ -21,7 +21,12 @@ public static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CloseHandle(IntPtr hObject); + public static extern bool CloseHandle(IntPtr hObject); + [DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] + static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, uint nSize); + + [DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)] + public static extern int EnumProcessModules(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded); public static void SuspendProcess(int PID) { @@ -80,5 +85,45 @@ } return ""; } + + public static string GetProcessFilename(Process proc) + { + string filename = ""; + Process[] procs = new Process[] { proc }; + int mainModuleIndex = 0; + foreach (Process p in procs) + { + // Setting up the variable for the second argument for EnumProcessModules + IntPtr[] hMods = new IntPtr[1024]; + + GCHandle gch = GCHandle.Alloc(hMods, GCHandleType.Pinned); // Don't forget to free this later + IntPtr pModules = gch.AddrOfPinnedObject(); + + // Setting up the rest of the parameters for EnumProcessModules + uint uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length)); + uint cbNeeded = 0; + + if (EnumProcessModules(p.Handle, pModules, uiSize, out cbNeeded) == 1) + { + Int32 uiTotalNumberofModules = (Int32)(cbNeeded / (Marshal.SizeOf(typeof(IntPtr)))); + + for (int i = 0; i < (int)uiTotalNumberofModules; i++) + { + StringBuilder strbld = new StringBuilder(1024); + + GetModuleFileNameEx(p.Handle, hMods[i], strbld, (uint)(strbld.Capacity)); + filename = strbld.ToString(); + if (i == mainModuleIndex) + break; + } + //Console.WriteLine("Number of Modules: " + uiTotalNumberofModules); + //Console.WriteLine(); + } + + // Must free the GCHandle object + gch.Free(); + } + return filename; + } } }
ViewVC Help | |
Powered by ViewVC 1.1.22 |