From 2a654f11ad3f165c775b32e6f6934e508a9bc3e7 Mon Sep 17 00:00:00 2001 From: Daniel Romischer Date: Mon, 15 Feb 2016 16:38:50 -0500 Subject: [PATCH] checking in latest Yaulw --- !app/Program.cs | 6 + Win32/COM.cs | 283 ++++++++++++++++++++++++++------------------- Win32/Functions.cs | 57 ++++++++- 3 files changed, 225 insertions(+), 121 deletions(-) diff --git a/!app/Program.cs b/!app/Program.cs index 71f0a2b..ef23261 100644 --- a/!app/Program.cs +++ b/!app/Program.cs @@ -9,6 +9,12 @@ namespace _app { static void Main(string[] args) { + //var Running = Yaulw.Win32.COM.GetActiveObjects(""); + //var RunningExcelWorkbooks = Yaulw.Win32.COM.GetRunningObjects(); + + //int n1 = Running.Count(); + //int n2 = RunningExcelWorkbooks.Count(); + } } } diff --git a/Win32/COM.cs b/Win32/COM.cs index 60f3f17..6046f0c 100644 --- a/Win32/COM.cs +++ b/Win32/COM.cs @@ -1,116 +1,167 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.ComTypes; - -namespace Yaulw.Win32 -{ - /// - /// COM Win32 Helper Functions - /// - public static class COM - { - /// - /// Returns a pointer to an implementation of IBindCtx (a bind context object). This object stores information about a particular moniker-binding operation - /// - /// This parameter is reserved and must be 0 - /// Address of an IBindCtx* pointer variable that receives the interface pointer to the new bind context object. - /// This function can return the standard return values E_OUTOFMEMORY and S_OK - [DllImport("ole32.dll")] - public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc); - - /// - /// Use this to retrieve the Actice COM Object from the ROT, for the specified progId - /// - /// - /// a valid com object, or null if error occured - public static Object GetActiceCOMObject(string progId) - { - Object app = null; - try - { - app = Marshal.GetActiveObject(progId); - } - catch (SystemException) { /* ignore */ } - return app; - } - - /// - /// ROT Object Entry - /// - public struct RunningObject - { - public string name; - public object o; - } - - /// - /// Use this to Get All Running Objects in the ROT - /// - /// list of all Runnint Objects in the ROT - public static List GetRunningObjects() - { - // Get the table. - var res = new List(); - IBindCtx bc; - - CreateBindCtx(0, out bc); - IRunningObjectTable runningObjectTable; - - bc.GetRunningObjectTable(out runningObjectTable); - IEnumMoniker monikerEnumerator; - runningObjectTable.EnumRunning(out monikerEnumerator); - monikerEnumerator.Reset(); - - // Enumerate and fill our nice dictionary. - IMoniker[] monikers = new IMoniker[1]; - IntPtr numFetched = IntPtr.Zero; - - while (monikerEnumerator.Next(1, monikers, numFetched) == 0) - { - RunningObject running; - monikers[0].GetDisplayName(bc, null, out running.name); - runningObjectTable.GetObject(monikers[0], out running.o); - res.Add(running); - } - return res; - } - - /// - /// Use this to Get A specific type of Object from the ROT - /// - /// List of a specific type of Object in the ROT - public static List GetRunningObjectsOfType() - { - // Get the table. - var res = new List(); - IBindCtx bc; - - CreateBindCtx(0, out bc); - IRunningObjectTable runningObjectTable; - - bc.GetRunningObjectTable(out runningObjectTable); - IEnumMoniker monikerEnumerator; - runningObjectTable.EnumRunning(out monikerEnumerator); - monikerEnumerator.Reset(); - - // Enumerate and fill our nice dictionary. - IMoniker[] monikers = new IMoniker[1]; - IntPtr numFetched = IntPtr.Zero; - - while (monikerEnumerator.Next(1, monikers, numFetched) == 0) - { - object o; - runningObjectTable.GetObject(monikers[0], out o); - - if (o is T) - res.Add((T)o); - o = null; - } - return res; - } - } - -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; + +namespace Yaulw.Win32 +{ + /// + /// COM Win32 Helper Functions + /// + public static class COM + { + /// + /// Returns a pointer to an implementation of IBindCtx (a bind context object). This object stores information about a particular moniker-binding operation + /// + /// This parameter is reserved and must be 0 + /// Address of an IBindCtx* pointer variable that receives the interface pointer to the new bind context object. + /// This function can return the standard return values E_OUTOFMEMORY and S_OK + [DllImport("ole32.dll")] + public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc); + + /// + /// Returns a pointer to the IRunningObjectTable + /// interface on the local running object table (ROT). + /// + /// This parameter is reserved and must be 0. + /// The address of an IRunningObjectTable* pointer variable + /// that receives the interface pointer to the local ROT. When the function is + /// successful, the caller is responsible for calling Release on the interface + /// pointer. If an error occurs, *pprot is undefined. + /// This function can return the standard return values E_UNEXPECTED and S_OK. + [DllImport("ole32.dll")] + public static extern int GetRunningObjectTable(uint reserved, out System.Runtime.InteropServices.ComTypes.IRunningObjectTable pprot); + + /// + /// Use this to retrieve the Actice COM Object from the ROT, for the specified progId + /// + /// + /// a valid com object, or null if error occured + public static Object GetActiceCOMObject(string progId) + { + Object app = null; + try + { + app = Marshal.GetActiveObject(progId); + } + catch (SystemException) { /* ignore */ } + return app; + } + + /// + /// ROT Object Entry + /// + public struct RunningObject + { + public string name; + public object o; + } + + /// + /// Gets all Running Objects in the ROT (deos the same thing as GetRunningObjects()) + /// ~got code snippet from a different website. keeping it in Just in case + /// + /// + /// + public static List GetActiveObjects(string MonikerFilter) + { + var ret = new List(); + + IRunningObjectTable pprot; + IEnumMoniker ppenumMoniker; + IMoniker[] monikers = new IMoniker[1]; + + GetRunningObjectTable(0, out pprot); + pprot.EnumRunning(out ppenumMoniker); + ppenumMoniker.Reset(); + + while (ppenumMoniker.Next(1, monikers, IntPtr.Zero) == 0) + { + IBindCtx ctx; + CreateBindCtx(0, out ctx); + + string name; + monikers[0].GetDisplayName(ctx, null, out name); + + if (String.IsNullOrEmpty(MonikerFilter) || (name.IndexOf(MonikerFilter) != -1)) + { + object val; + pprot.GetObject(monikers[0], out val); + ret.Add(new RunningObject() { name = name, o = val }); + } + } + return ret; + } + + /// + /// Use this to Get All Running Objects in the ROT + /// + /// list of all Runnint Objects in the ROT + public static List GetRunningObjects() + { + // Get the table. + var res = new List(); + IBindCtx bc; + + CreateBindCtx(0, out bc); + IRunningObjectTable runningObjectTable; + + bc.GetRunningObjectTable(out runningObjectTable); + IEnumMoniker monikerEnumerator; + runningObjectTable.EnumRunning(out monikerEnumerator); + monikerEnumerator.Reset(); + + // Enumerate and fill our nice dictionary. + IMoniker[] monikers = new IMoniker[1]; + IntPtr numFetched = IntPtr.Zero; + + while (monikerEnumerator.Next(1, monikers, numFetched) == 0) + { + RunningObject running; + monikers[0].GetDisplayName(bc, null, out running.name); + Type ff1 = monikers[0].GetType(); + + runningObjectTable.GetObject(monikers[0], out running.o); + res.Add(running); + } + return res; + } + + /// + /// Use this to Get A specific type of Object from the ROT + /// + /// List of a specific type of Object in the ROT + public static List GetRunningObjectsOfType() + { + // Get the table. + var res = new List(); + IBindCtx bc; + + CreateBindCtx(0, out bc); + IRunningObjectTable runningObjectTable; + + bc.GetRunningObjectTable(out runningObjectTable); + IEnumMoniker monikerEnumerator; + runningObjectTable.EnumRunning(out monikerEnumerator); + monikerEnumerator.Reset(); + + // Enumerate and fill our nice dictionary. + IMoniker[] monikers = new IMoniker[1]; + IntPtr numFetched = IntPtr.Zero; + + while (monikerEnumerator.Next(1, monikers, numFetched) == 0) + { + object o; + runningObjectTable.GetObject(monikers[0], out o); + + if (o is T) + res.Add((T)o); + o = null; + } + return res; + } + } + +} diff --git a/Win32/Functions.cs b/Win32/Functions.cs index 78112b3..8edb671 100644 --- a/Win32/Functions.cs +++ b/Win32/Functions.cs @@ -3,7 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; -using System.Diagnostics; +using System.Diagnostics; +using diag = System.Diagnostics; using System.Windows.Forms; using System.Runtime.InteropServices; using SysIO = System.IO; @@ -328,10 +329,56 @@ namespace Yaulw.Win32 return false; } - #endregion - - #region Short / Long FileName N' Path Conversions - + #endregion + + #region Process / Handle Functionallity + + public static string GetProcessName(IntPtr hWnd) + { + int currentPid = 0; + User32.GetWindowThreadProcessId(hWnd, ref currentPid); + + diag.Process process; + try + { + process = diag.Process.GetProcessById(currentPid); + } + catch (ArgumentException e) + { + // This is thrown when Process.GetProcessById cannot find the process + throw e; + } + + return process.ProcessName; + } + + /// + /// Returns the .Net Process Object that owns the passed in hWnd + /// + /// handle to a Window + /// a .Net Process or throws an error + public static diag.Process GetProcessFromHandle(IntPtr hWnd) + { + try + { + int currentPid = 0; + User32.GetWindowThreadProcessId(hWnd, ref currentPid); + + diag.Process process; + process = diag.Process.GetProcessById(currentPid); + return process; + } + catch (ArgumentException e) + { + // This is thrown when Process.GetProcessById cannot find the process + throw e; + } + } + + #endregion + + #region Short / Long FileName N' Path Conversions + /// /// Converts a LongFileNameNPath (Modern Windows Path) into a ShortFileNPath (Old Dos 8.3) /// ~File Must exist on the system