/// <summary> /// Retrieve every visual studio instance /// </summary> /// <returns>List of visual studio instances</returns> internal static IEnumerable <EnvDTE.DTE> GetVisualStudioInstances() { System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMoniker; int retVal = GetRunningObjectTable(0, out rot); if (retVal == 0) { rot.EnumRunning(out enumMoniker); IntPtr fetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IMoniker[] moniker = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (enumMoniker.Next(1, moniker, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); string displayName; moniker[0].GetDisplayName(bindCtx, null, out displayName); //Console.WriteLine("Display Name: {0}", displayName); bool isVisualStudio = displayName.StartsWith("!VisualStudio"); if (isVisualStudio) { int currentProcessId = int.Parse(displayName.Split(':')[1]); object obj; rot.GetObject(moniker[0], out obj); var dte = obj as EnvDTE.DTE; yield return(dte); } } } }
public EnvDTE._DTE GetDteInstance() { //rot entry for visual studio running under current process. string rotEntry = String.Format("!VisualStudio.DTE.14.0:{0}", GetDteProcess().Id); System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot; GetRunningObjectTable(0, out rot); System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMoniker; rot.EnumRunning(out enumMoniker); enumMoniker.Reset(); IntPtr fetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IMoniker[] moniker = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (enumMoniker.Next(1, moniker, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); string displayName; moniker[0].GetDisplayName(bindCtx, null, out displayName); if (displayName == rotEntry) { object comObject; rot.GetObject(moniker[0], out comObject); return((EnvDTE._DTE)comObject); } } return(null); }
/// <summary> Get a moniker's human-readable name based on a moniker string. </summary> protected string getName(string monikerString) { System.Runtime.InteropServices.ComTypes.IMoniker parser = null; System.Runtime.InteropServices.ComTypes.IMoniker moniker = null; try { parser = getAnyMoniker(); int eaten; parser.ParseDisplayName(null, null, monikerString, out eaten, out moniker); return(getName(parser)); } finally { if (parser != null) { Marshal.ReleaseComObject(parser); } parser = null; if (moniker != null) { Marshal.ReleaseComObject(moniker); } moniker = null; } }
/// <summary> Retrieve the a moniker's display name (i.e. it's unique string) </summary> protected string getMonikerString(System.Runtime.InteropServices.ComTypes.IMoniker moniker) { string s; moniker.GetDisplayName(null, null, out s); return(s); }
/// <summary> /// /// </summary> /// <param name="strProgID"></param> /// <returns></returns> /// <example> /// string strMoniker = "!VisualStudio.DTE.7.1:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); /// EnvDTE.DTE dte = (EnvDTE.DTE)this.GetMSDEVFromGIT(strMoniker); /// </example> public static object GetMSDEVFromGIT(string strProgID) { System.Runtime.InteropServices.ComTypes.IRunningObjectTable prot; System.Runtime.InteropServices.ComTypes.IEnumMoniker pMonkEnum; GetRunningObjectTable(0, out prot); prot.EnumRunning(out pMonkEnum); pMonkEnum.Reset(); System.IntPtr fetched = new System.IntPtr(); System.Runtime.InteropServices.ComTypes.IMoniker[] pmon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (pMonkEnum.Next(1, pmon, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx pCtx; CreateBindCtx(0, out pCtx); string str; pmon[0].GetDisplayName(pCtx, null, out str); if (str == strProgID) { object objReturnObject; prot.GetObject(pmon[0], out objReturnObject); object ide = (object)objReturnObject; return(ide); } } return(null); }
public static bool GetDevicesOfCat(Guid cat, out ArrayList devs) { devs = null; int hr; object comObj = null; ICreateDevEnum enumDev = null; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMon = null; System.Runtime.InteropServices.ComTypes.IMoniker[] mon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; try { Type srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum); if (srvType == null) throw new NotImplementedException("System Device Enumerator"); comObj = Activator.CreateInstance(srvType); enumDev = (ICreateDevEnum)comObj; hr = enumDev.CreateClassEnumerator(ref cat, out enumMon, 0); if (hr != 0) throw new NotSupportedException("No devices of the category"); IntPtr f = IntPtr.Zero; int count = 0; do { hr = enumMon.Next(1, mon, f); if ((hr != 0) || (mon[0] == null)) break; DsDevice dev = new DsDevice(); dev.Name = GetFriendlyName(mon[0]); if (devs == null) devs = new ArrayList(); dev.Mon = mon[0]; mon[0] = null; devs.Add(dev); dev = null; count++; } while (true); return count > 0; } catch (Exception) { if (devs != null) { foreach (DsDevice d in devs) d.Dispose(); devs = null; } return false; } finally { enumDev = null; if (mon[0] != null) Marshal.ReleaseComObject(mon[0]); mon[0] = null; if (enumMon != null) Marshal.ReleaseComObject(enumMon); enumMon = null; if (comObj != null) Marshal.ReleaseComObject(comObj); comObj = null; } }
/// <summary> /// This method gets a UCOMIMoniker object. /// /// HACK: The only way to create a UCOMIMoniker from a moniker /// string is to use UCOMIMoniker.ParseDisplayName(). So I /// need ANY UCOMIMoniker object so that I can call /// ParseDisplayName(). Does anyone have a better solution? /// /// This assumes there is at least one video compressor filter /// installed on the system. /// </summary> protected System.Runtime.InteropServices.ComTypes.IMoniker getAnyMoniker() { Guid category = FilterCategory.VideoCompressorCategory; object comObj = null; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMon = null; try { // Get the system device enumerator //Type srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum); //if (srvType == null) // throw new NotImplementedException("System Device Enumerator"); //comObj = Activator.CreateInstance(srvType); ICreateDevEnum enumDev = (ICreateDevEnum) new CreateDevEnum(); // Create an enumerator to find filters in category int hr = enumDev.CreateClassEnumerator(category, out enumMon, 0); //int hr = enumDev.CreateClassEnumerator(ref category, out enumMon, 0); if (hr != 0) { throw new NotSupportedException("No devices of the category"); } // Get first filter IntPtr f = IntPtr.Zero; var mon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; hr = enumMon.Next(1, mon, f); if ((hr != 0)) { mon[0] = null; } return(mon[0]); } finally { if (enumMon != null) { Marshal.ReleaseComObject(enumMon); } enumMon = null; if (comObj != null) { Marshal.ReleaseComObject(comObj); } comObj = null; } }
public CommandHelper(System.IServiceProvider serviceProvider) { var rot = this.GetRunningObjectTable(); System.Runtime.InteropServices.ComTypes.IMoniker cmdDispatchMoniker = this.GetCmdDispatcherMoniker(); System.Runtime.InteropServices.ComTypes.IMoniker cmdNameMoniker = this.GetCmdNameMoniker(); var cmdDispatchObject = this.GetObjectFromRot(rot, cmdDispatchMoniker); var cmdNameObject = this.GetObjectFromRot(rot, cmdNameMoniker); if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } this.shellCmdTarget = (IOleCommandTarget)cmdDispatchObject; this.cmdNameMapping = (IVsCmdNameMapping)cmdNameObject; }
/// <summary> /// ROTにオブジェクトを登録する。 /// </summary> /// <param name="iUnknownObject">オブジェクト</param> /// <param name="name">名前</param> public RunningObjectTableEntry(object iUnknownObject, string name) { int hresult; const int ROTFLAGS_REGISTRATIONKEEPSALIVE = 1; System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot = null; System.Runtime.InteropServices.ComTypes.IMoniker moniker = null; try { hresult = GetRunningObjectTable(0, out rot); if (hresult < 0) { throw new COMException("GetRunningObjectTable failed.", hresult); } string wsz = string.Format("{0} {1:x16} pid {2:x8}", name, Marshal.GetIUnknownForObject(iUnknownObject).ToInt64(), System.Diagnostics.Process.GetCurrentProcess().Id); hresult = CreateItemMoniker("!", wsz, out moniker); if (hresult < 0) { throw new COMException("CreateItemMoniker failed.", hresult); } int register = rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, iUnknownObject, moniker); this.register = register; } finally { if (moniker != null) { Marshal.ReleaseComObject(moniker); } if (rot != null) { Marshal.ReleaseComObject(rot); } } }
public static System.Collections.Generic.IList<VA.Internal.Interop.RunningObject> GetRunningObjects() { // Based on: // http://blocko.blogspot.com/2006/10/driving-excel-and-powerpoint-with-c.html // http://www.codeproject.com/KB/COM/ROTStuff.aspx var results = new System.Collections.Generic.List<RunningObject>(); System.Runtime.InteropServices.ComTypes.IBindCtx bindctx; NativeMethods.CreateBindCtx(0, out bindctx); System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot; bindctx.GetRunningObjectTable(out rot); System.Runtime.InteropServices.ComTypes.IEnumMoniker enum_mon; rot.EnumRunning(out enum_mon); enum_mon.Reset(); var monikers = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; System.IntPtr numFetched = System.IntPtr.Zero; while (enum_mon.Next(1, monikers, numFetched) == 0) { var moniker = monikers[0]; string name; moniker.GetDisplayName(bindctx, null, out name); object obj; rot.GetObject(moniker, out obj); System.Guid classid; moniker.GetClassID(out classid); var ro = new RunningObject(name,obj,classid); results.Add(ro); } return results; }
/// <summary> /// Attempts to retrieve a specific visual studio instance, based on its PID /// </summary> /// <param name="processId">Visual Studio instance PID</param> /// <param name="instance">Visual Studio instance if able to found. Null otherwise.</param> /// <returns>Whether the visual studio instance was found or not.</returns> internal static bool TryToRetrieveVSInstance(int processId, out EnvDTE.DTE instance) { IntPtr numFetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IRunningObjectTable runningObjectTable; System.Runtime.InteropServices.ComTypes.IEnumMoniker monikerEnumerator; System.Runtime.InteropServices.ComTypes.IMoniker[] monikers = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; GetRunningObjectTable(0, out runningObjectTable); runningObjectTable.EnumRunning(out monikerEnumerator); monikerEnumerator.Reset(); while (monikerEnumerator.Next(1, monikers, numFetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx ctx; CreateBindCtx(0, out ctx); string runningObjectName; monikers[0].GetDisplayName(ctx, null, out runningObjectName); object runningObjectVal; runningObjectTable.GetObject(monikers[0], out runningObjectVal); if (runningObjectVal is EnvDTE.DTE && runningObjectName.StartsWith("!VisualStudio")) { // retrieve process id - "process_name:pid" int currentProcessId = int.Parse(runningObjectName.Split(':')[1]); // if it's a match; meaning, if it's the right gibbo project solution if (currentProcessId == processId) { instance = (EnvDTE.DTE)runningObjectVal; return(true); } } } instance = null; return(false); }
public static System.Collections.Generic.IList <VA.Internal.Interop.RunningObject> GetRunningObjects() { // Based on: // http://blocko.blogspot.com/2006/10/driving-excel-and-powerpoint-with-c.html // http://www.codeproject.com/KB/COM/ROTStuff.aspx var results = new System.Collections.Generic.List <RunningObject>(); System.Runtime.InteropServices.ComTypes.IBindCtx bindctx; NativeMethods.CreateBindCtx(0, out bindctx); System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot; bindctx.GetRunningObjectTable(out rot); System.Runtime.InteropServices.ComTypes.IEnumMoniker enum_mon; rot.EnumRunning(out enum_mon); enum_mon.Reset(); var monikers = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; System.IntPtr numFetched = System.IntPtr.Zero; while (enum_mon.Next(1, monikers, numFetched) == 0) { var moniker = monikers[0]; string name; moniker.GetDisplayName(bindctx, null, out name); object obj; rot.GetObject(moniker, out obj); System.Guid classid; moniker.GetClassID(out classid); var ro = new RunningObject(name, obj, classid); results.Add(ro); } return(results); }
/// <summary> /// 获取 VisualStudio 设计器实例。 /// </summary> /// <returns>返回找到的设计器实例;否则返回 null。</returns> /// <remarks> /// 此方法由方法“GetMSDEVFromGIT”改写。 /// </remarks> public static EnvDTE.DTE GetVisualStudioIDE() { //string strProgID = "!VisualStudio.DTE.7.1:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); //string strProgID = "!VisualStudio.DTE.8.0:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); //string strProgID = "!VisualStudio.DTE.9.0:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); //string strProgID = "!VisualStudio.DTE.11.0:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); string strProgID = @"!VisualStudio\.DTE\.\d+\.\d+\:" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); System.Runtime.InteropServices.ComTypes.IRunningObjectTable prot; System.Runtime.InteropServices.ComTypes.IEnumMoniker pMonkEnum; GetRunningObjectTable(0, out prot); prot.EnumRunning(out pMonkEnum); pMonkEnum.Reset(); System.IntPtr fetched = new System.IntPtr(); System.Runtime.InteropServices.ComTypes.IMoniker[] pmon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (pMonkEnum.Next(1, pmon, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx pCtx; CreateBindCtx(0, out pCtx); string str; pmon[0].GetDisplayName(pCtx, null, out str); if (System.Text.RegularExpressions.Regex.IsMatch(str, strProgID, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.ExplicitCapture)) { object objReturnObject; prot.GetObject(pmon[0], out objReturnObject); EnvDTE.DTE ide = objReturnObject as EnvDTE.DTE; if (ide != null) { return(ide); } } } return(null); }
/// <summary> Retrieve the human-readable name of the filter </summary> protected string getName(System.Runtime.InteropServices.ComTypes.IMoniker moniker) { object bagObj = null; IPropertyBag bag = null; try { Guid bagId = typeof(IPropertyBag).GUID; moniker.BindToStorage(null, null, ref bagId, out bagObj); bag = (IPropertyBag)bagObj; object val = ""; int hr = bag.Read("FriendlyName", out val, null); //int hr = bag.Read("FriendlyName", ref val, IntPtr.Zero); Marshal.ThrowExceptionForHR(hr); string ret = val as string; if ((ret == null) || (ret.Length < 1)) { throw new NotImplementedException("Device FriendlyName"); } return(ret); } catch (Exception) { return(""); } finally { bag = null; if (bagObj != null) { Marshal.ReleaseComObject(bagObj); } bagObj = null; } }
private object GetObjectFromRot(System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot, System.Runtime.InteropServices.ComTypes.IMoniker moniker) { object objectFromRot; int hr = rot.GetObject(moniker, out objectFromRot); if (ErrorHandler.Failed(hr)) { ErrorHandler.ThrowOnFailure(hr, null); } return(objectFromRot); }
public void Dispose() { if (Mon != null) Marshal.ReleaseComObject(Mon); Mon = null; }
/// <summary> /// Attempts to retrieve a specific visual studio instance, based on its PID /// </summary> /// <param name="processId">Visual Studio instance PID</param> /// <param name="instance">Visual Studio instance if able to found. Null otherwise.</param> /// <returns>Whether the visual studio instance was found or not.</returns> internal static bool TryToRetrieveVSInstance(int processId, out EnvDTE.DTE instance) { IntPtr numFetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IRunningObjectTable runningObjectTable; System.Runtime.InteropServices.ComTypes.IEnumMoniker monikerEnumerator; System.Runtime.InteropServices.ComTypes.IMoniker[] monikers = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; GetRunningObjectTable(0, out runningObjectTable); runningObjectTable.EnumRunning(out monikerEnumerator); monikerEnumerator.Reset(); while (monikerEnumerator.Next(1, monikers, numFetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx ctx; CreateBindCtx(0, out ctx); string runningObjectName; monikers[0].GetDisplayName(ctx, null, out runningObjectName); object runningObjectVal; runningObjectTable.GetObject(monikers[0], out runningObjectVal); if (runningObjectVal is EnvDTE.DTE && runningObjectName.StartsWith("!VisualStudio")) { // retrieve process id - "process_name:pid" int currentProcessId = int.Parse(runningObjectName.Split(':')[1]); // if it's a match; meaning, if it's the right gibbo project solution if (currentProcessId == processId) { instance = (EnvDTE.DTE)runningObjectVal; return true; } } } instance = null; return false; }
/// <summary> /// Retrieve every visual studio instance /// </summary> /// <returns>List of visual studio instances</returns> internal static IEnumerable<EnvDTE.DTE> GetVisualStudioInstances() { System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMoniker; int retVal = GetRunningObjectTable(0, out rot); if (retVal == 0) { rot.EnumRunning(out enumMoniker); IntPtr fetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IMoniker[] moniker = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (enumMoniker.Next(1, moniker, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); string displayName; moniker[0].GetDisplayName(bindCtx, null, out displayName); //Console.WriteLine("Display Name: {0}", displayName); bool isVisualStudio = displayName.StartsWith("!VisualStudio"); if (isVisualStudio) { int currentProcessId = int.Parse(displayName.Split(':')[1]); object obj; rot.GetObject(moniker[0], out obj); var dte = obj as EnvDTE.DTE; yield return dte; } } } }
static extern int CreateItemMoniker([MarshalAs(UnmanagedType.LPWStr)] string lpszDelim, [MarshalAs(UnmanagedType.LPWStr)] string lpszItem, out System.Runtime.InteropServices.ComTypes.IMoniker ppmk);
public void ModifyUnityBuildXML(string projFileName, string slnFileName, bool unitybuild, string uniqueName, bool settingMenuProject) { Guid guid = new Guid(); string solutionName = slnFileName; object solutionObject = null; IRunningObjectTable rot; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMoniker; int retVal = GetRunningObjectTable(0, out rot); if (retVal == 0) { rot.EnumRunning(out enumMoniker); enumMoniker.Reset(); IntPtr fetched = IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IMoniker[] moniker = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (enumMoniker.Next(1, moniker, fetched) == 0) { IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); string displayName; moniker[0].GetDisplayName(bindCtx, null, out displayName); bool isPrebuildSolution = displayName.Contains(solutionName); if (isPrebuildSolution) { rot.GetObject(moniker[0], out solutionObject); break; } } } if (solutionObject != null) { Solution solution = solutionObject as Solution; EnvDTE.DTE dte = solution.DTE; Array projectsObject = (Array)dte.ActiveSolutionProjects; Project project; ServiceProvider serviceProvider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte); IVsSolution vsSolution = (IVsSolution)serviceProvider.GetService(typeof(IVsSolution)); if (vsSolution == null) { return; } IVsHierarchy vsHierarchy = null; if (uniqueName == null) { project = projectsObject.GetValue(0) as Project; vsSolution.GetProjectOfUniqueName(project.UniqueName, out vsHierarchy); } else { vsSolution.GetProjectOfUniqueName(uniqueName, out vsHierarchy); } vsSolution.GetGuidOfProject(vsHierarchy, out guid); IVsSolution4 vsSolution4 = (IVsSolution4)serviceProvider.GetService(typeof(SVsSolution)); vsSolution4.UnloadProject(guid, (uint)_VSProjectUnloadStatus.UNLOADSTATUS_UnloadedByUser); ///////////////////////////////// xml수정 /////////////////////////////// if (settingMenuProject) { int unitybuildMenuValue = SetUnitybuildEnableInfo(projFileName); SetUnitybuildMenuinfo(unitybuildMenuValue); } if (unitybuild == true && settingMenuProject == false) { SettingEnableUnityBuild(projFileName); } else if (unitybuild == false && settingMenuProject == false) { SettingDisableUnityBuild(projFileName); } /////////////////////////////// xml수정 /////////////////////////////// vsSolution4.ReloadProject(guid); } }
public static object GetMSDEVFromGIT(string strProgID, string processId) { System.Runtime.InteropServices.ComTypes.IRunningObjectTable prot; System.Runtime.InteropServices.ComTypes.IEnumMoniker pMonkEnum; try { GetRunningObjectTable(0, out prot); prot.EnumRunning(out pMonkEnum); pMonkEnum.Reset(); // Churn through enumeration. IntPtr fetched=IntPtr.Zero; System.Runtime.InteropServices.ComTypes.IMoniker[] pmon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; while (pMonkEnum.Next(1, pmon, fetched) == 0) { System.Runtime.InteropServices.ComTypes.IBindCtx pCtx; CreateBindCtx(0, out pCtx); string str; pmon[0].GetDisplayName(pCtx, null, out str); // #if DEBUG // System.Windows.Forms.MessageBox.Show(str+" strProgId="+strProgID+" processId="+processId); // #endif if (str.IndexOf(strProgID) > 0 && (str.IndexOf(":" + processId) > 0 || processId == "")) { object objReturnObject; prot.GetObject(pmon[0], out objReturnObject); object ide = (object)objReturnObject; return ide; } } } catch { return null; } return null; }
public int SelectedFilter(System.Runtime.InteropServices.ComTypes.IMoniker pMon) { throw new Exception("The method or operation is not implemented."); }
/// <summary> /// This method gets a UCOMIMoniker object. /// /// HACK: The only way to create a UCOMIMoniker from a moniker /// string is to use UCOMIMoniker.ParseDisplayName(). So I /// need ANY UCOMIMoniker object so that I can call /// ParseDisplayName(). Does anyone have a better solution? /// /// This assumes there is at least one video compressor filter /// installed on the system. /// </summary> protected System.Runtime.InteropServices.ComTypes.IMoniker getAnyMoniker() { Guid category = FilterCategory.VideoCompressorCategory; object comObj = null; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMon = null; try { // Get the system device enumerator //Type srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum); //if (srvType == null) // throw new NotImplementedException("System Device Enumerator"); //comObj = Activator.CreateInstance(srvType); ICreateDevEnum enumDev = (ICreateDevEnum)new CreateDevEnum(); // Create an enumerator to find filters in category int hr = enumDev.CreateClassEnumerator(category, out enumMon, 0); //int hr = enumDev.CreateClassEnumerator(ref category, out enumMon, 0); if (hr != 0) throw new NotSupportedException("No devices of the category"); // Get first filter IntPtr f = IntPtr.Zero; var mon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; hr = enumMon.Next(1, mon, f); if ((hr != 0)) mon[0] = null; return (mon[0]); } finally { if (enumMon != null) Marshal.ReleaseComObject(enumMon); enumMon = null; if (comObj != null) Marshal.ReleaseComObject(comObj); comObj = null; } }
/// <summary> Populate the InnerList with a list of filters from a particular category </summary> protected static IEnumerable <Filter> getFilters(Guid category) { object comObj = null; System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMon = null; var mon = new System.Runtime.InteropServices.ComTypes.IMoniker[1]; try { // Get the system device enumerator //Type srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum); //if (srvType == null) // throw new NotImplementedException("System Device Enumerator"); //comObj = Activator.CreateInstance(srvType); ICreateDevEnum enumDev = (ICreateDevEnum) new CreateDevEnum(); // Create an enumerator to find filters in category int hr = enumDev.CreateClassEnumerator(category, out enumMon, 0); if (hr != 0) { throw new NotSupportedException("No devices of the category"); } // Loop through the enumerator IntPtr f = IntPtr.Zero; do { // Next filter hr = enumMon.Next(1, mon, f); if ((hr != 0) || (mon[0] == null)) { break; } // Add the filter Filter filter = new Filter(mon[0]); //InnerList.Add(filter); yield return(filter); // Release resources Marshal.ReleaseComObject(mon[0]); mon[0] = null; }while (true); // Sort //InnerList.Sort(); } finally { if (mon[0] != null) { Marshal.ReleaseComObject(mon[0]); } mon[0] = null; if (enumMon != null) { Marshal.ReleaseComObject(enumMon); } enumMon = null; if (comObj != null) { Marshal.ReleaseComObject(comObj); } comObj = null; } }
/// <summary> Create a new filter from its moniker </summary> internal Filter(System.Runtime.InteropServices.ComTypes.IMoniker moniker) { Name = getName(moniker); MonikerString = getMonikerString(moniker); }