/// <summary> Retrieve the a moniker's display name (i.e. it's unique string) </summary> protected string getMonikerString(IMoniker moniker) { string s; moniker.GetDisplayName(null, null, out s); return(s); }
/// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { // Get the display name of the pointer to an IMoniker interface that specifies // the object to be downloaded. string name = string.Empty; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url = null; bool result = Uri.TryCreate(name, UriKind.Absolute, out url); if (result) { // Launch CSWebDownloader.exe to download the file. FileInfo assemblyFile = new FileInfo(Assembly.GetExecutingAssembly().Location); ProcessStartInfo start = new ProcessStartInfo { Arguments = name, FileName = string.Format("{0}\\CSWebDownloader.exe", assemblyFile.DirectoryName) }; Process.Start(start); return(0); } } return(1); }
// Get moniker string of the moniker private string GetMonikerString(IMoniker moniker) { string str; moniker.GetDisplayName(null, null, out str); return str; }
// Token: 0x0600001F RID: 31 RVA: 0x00002304 File Offset: 0x00000504 private string GetMonikerString(IMoniker moniker) { string result; moniker.GetDisplayName(null, null, out result); return(result); }
public string GetDisplayName() { string str = ""; moniker.GetDisplayName(pbc, null, out str); return(str); }
private string GetMonikerString(IMoniker moniker) { string text; moniker.GetDisplayName(null, null, out text); return(text); }
/// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { // Get the display name of the pointer to an IMoniker interface that specifies // the object to be downloaded. string name = string.Empty; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url = null; bool result = Uri.TryCreate(name, UriKind.Absolute, out url); if (result) { //Implement your custom download manager here //Example: //WebDownloadForm manager = new WebDownloadForm(); //manager.FileToDownload = url.AbsoluteUri; //manager.Show(); MessageBox.Show("Download URL is: " + url); return 0; //Return S_OK } } return 1; //unspecified error occured. }
private string PublishList(IMoniker res) { //Discarded unreachable code: IL_0002 //IL_0003: Incompatible stack heights: 0 vs 1 res.GetDisplayName(null, null, out string ppszDisplayName); return(ppszDisplayName); }
// // Get moniker string of the moniker // private string GetMonikerString(IMoniker moniker) { string str; moniker.GetDisplayName(null, null, out str); return(str); }
/// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { // Get the display name of the pointer to an IMoniker interface that specifies // the object to be downloaded. string name = string.Empty; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url = null; bool result = Uri.TryCreate(name, UriKind.Absolute, out url); if (result) { MessageBox.Show(url.ToString()); //pmk.BindToStorage(pbc,null,); //// Launch CSWebDownloader.exe to download the file. //FileInfo assemblyFile = // new FileInfo(Assembly.GetExecutingAssembly().Location); //ProcessStartInfo start = new ProcessStartInfo //{ // Arguments = name, // FileName = // string.Format("{0}\\CSWebDownloader.exe", assemblyFile.DirectoryName) //}; //Process.Start(start); return 0; } } return 1; }
protected string getMonikerString(IMoniker moniker) #endif #endif { string s; moniker.GetDisplayName( null, null, out s ); return( s ); }
public static string GetDisplayName(IMoniker m) { IBindCtx bc = CreateBindCtx(); m.GetDisplayName(bc, null, out var s); Marshal.ReleaseComObject(bc); // seems to be recommended return(s); }
/// <summary> /// Gets the moniker string for a specific moniker /// </summary> /// <param name="moniker">Moniker to retrieve the moniker string of</param> /// <returns>Moniker string</returns> private static string GetMonikerString(IMoniker moniker) { string result; // Get the display name of the moniker moniker.GetDisplayName(null, null, out result); return(result); }
private static DTE GetDTE(int processId, Version version) { MessageFilter.Register(); var process = Process.GetProcessById(processId); string progIdName = "VisualStudio"; switch (process.MainModule.ModuleName.ToLowerInvariant()) { case "wdexpress.exe": progIdName = "WDExpress"; break; case "vwdexpress.exe": progIdName = "VWDExpress"; break; } string progId = string.Format("!{0}.DTE.{1}:{2}", progIdName, version, processId); object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; uint numberFetched = 0; while (enumMonikers.Next(1, moniker, out numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal)) { rot.GetObject(runningObjectMoniker, out runningObject); break; } } return((DTE)runningObject); }
public static DTE GetDTE() { IRunningObjectTable Table; if (Succeeded(GetRunningObjectTable(0, out Table)) && Table != null) { IEnumMoniker MonikersTable; Table.EnumRunning(out MonikersTable); if (MonikersTable == null) { return(null); } MonikersTable.Reset(); // Look for all visual studio instances in the ROT IMoniker[] Monikers = new IMoniker[] { null }; while (MonikersTable.Next(1, Monikers, IntPtr.Zero) == 0) { IBindCtx BindContext; string OutDisplayName; IMoniker CurrentMoniker = Monikers[0]; if (!Succeeded(CreateBindCtx(0, out BindContext))) { continue; } try { CurrentMoniker.GetDisplayName(BindContext, null, out OutDisplayName); if (string.IsNullOrEmpty(OutDisplayName) || !IsVisualStudioDTEMoniker(OutDisplayName)) { continue; } } catch (UnauthorizedAccessException) { // Some ROT objects require elevated permissions continue; } object ComObject; if (!Succeeded(Table.GetObject(CurrentMoniker, out ComObject))) { continue; } return(ComObject as DTE); } } return(null); }
public static string GetMonikerDisplayName(IMoniker pmk) { string strDisplayName; IBindCtx bindCtx = CreateBindCtx(0); pmk.GetDisplayName(bindCtx, null, out strDisplayName); Marshal.ReleaseComObject(bindCtx); return(strDisplayName); }
/// <summary> /// Gets the moniker string for a specific moniker /// </summary> /// <param name="moniker">Moniker to retrieve the moniker string of</param> /// <returns>Moniker string</returns> private static string GetMonikerString(IMoniker moniker) { // Declare variables string result = ""; // Get the display name of the moniker moniker.GetDisplayName(null, null, out result); // Return result return(result); }
private static object GetRunningObject(string displayName, out IEnumerable <string> runningObjectDisplayNames) { IBindCtx bindContext = null; NativeMethods.CreateBindCtx(0, out bindContext); IRunningObjectTable runningObjectTable = null; bindContext.GetRunningObjectTable(out runningObjectTable); IEnumMoniker monikerEnumerator = null; runningObjectTable.EnumRunning(out monikerEnumerator); object runningObject = null; List <string> runningObjectDisplayNameList = new List <string>(); IMoniker[] monikers = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (monikerEnumerator.Next(1, monikers, numberFetched) == 0) { IMoniker moniker = monikers[0]; string objectDisplayName = null; try { moniker.GetDisplayName(bindContext, null, out objectDisplayName); } catch (UnauthorizedAccessException) { // Some ROT objects require elevated permissions. } if (!string.IsNullOrWhiteSpace(objectDisplayName)) { runningObjectDisplayNameList.Add(objectDisplayName); if (objectDisplayName.EndsWith(displayName, StringComparison.Ordinal)) { runningObjectTable.GetObject(moniker, out runningObject); if (runningObject == null) { throw new InvalidOperationException($"Failed to get running object with display name {displayName}"); } } } } runningObjectDisplayNames = runningObjectDisplayNameList; return(runningObject); }
public void LoadFile(String s) { if (s == null || s.Trim().Length == 0) { navi = ""; } else if (Site == null) { bwLoader.RunWorkerAsync(s); } else { IOleClientSite pClientSite = (IOleClientSite)Site.GetService(new AntiMoniker().GetType()); if (pClientSite != null) { IMoniker pimkDL = null; try { if (pimkDL == null) { IBindHost pBindHost = (IBindHost)pClientSite; pBindHost.CreateMoniker(s, null, out pimkDL, 0); } if (pimkDL == null) { IMoniker pimkName = null; pClientSite.GetMoniker(OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, out pimkName); try { int chEaten; pimkName.ParseDisplayName(null, pimkName, s, out chEaten, out pimkDL); } finally { Marshal.ReleaseComObject(pimkName); } } if (pimkDL != null) { pimkDL.GetDisplayName(null, null, out title); if (!String.IsNullOrEmpty(title)) { s = title; } } } finally { Marshal.ReleaseComObject(pimkDL); } } bwLoader.RunWorkerAsync(s); } }
IntPtr IDownloadManager.Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { // write some status string monikerName; pmk.GetDisplayName(pbc, null, out monikerName); Uri monikerUri = new Uri(monikerName); RegisterCallback(pbc, monikerUri); BindMonikerToStream(pmk, pbc); return(HResults.S_OK); }
private bool IsMatchingMonikerName(IMoniker moniker, IBindCtx bindCtx) { try { moniker.GetDisplayName(bindCtx, null, out string name); return(!string.IsNullOrWhiteSpace(name) && name.StartsWith("!VisualStudio.DTE", StringComparison.Ordinal) && name.EndsWith($":{_devEnvProcess.Id}", StringComparison.Ordinal)); } catch (NotImplementedException) { return(false); } catch (COMException) { return(false); } }
/// <summary> /// This method is for internal purpose. Don't call it directly. /// </summary> /// <param name="moniker"></param> /// <returns></returns> public int SelectedFilter(IMoniker moniker) { if (moniker == null) { throw new ArgumentNullException("moniker"); } int retval = S_OK; string monikerDisplayName = null; Guid filterClsid; // Get the moniker's display name. // DirectShow filter have this form: @device:sw:FilterCategory\CLSID moniker.GetDisplayName(null, null, out monikerDisplayName); Debug.WriteLine(string.Format("\r\nBlackListManager.SelectedFilter:\r\nMoniker: {0}", monikerDisplayName)); // Did the display name normally composed ? int i = monikerDisplayName.LastIndexOf('\\'); if (i != -1) { // Yes, tring to get the CLSID if (GuidTryParse(monikerDisplayName.Substring(i + 1), out filterClsid)) { // Display informations about this filter this.DisplayDebugInformation(moniker, filterClsid); // Test if the filter is black-listed if (this.blackList.Contains(filterClsid)) { Debug.WriteLine("WARNING: This filter is black-listed! Rejecting it."); // The filter is black listed. Rejecting it. retval = E_FAIL; } } } return(retval); }
public int Download([In, MarshalAs(UnmanagedType.Interface)] IMoniker pmk, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In, MarshalAs(UnmanagedType.U4)] uint dwBindVerb, [In] int grfBINDF, [In] IntPtr pBindInfo, [In, MarshalAs(UnmanagedType.LPWStr)] string pszHeaders, [In, MarshalAs(UnmanagedType.LPWStr)] string pszRedir, [In, MarshalAs(UnmanagedType.U4)] uint uiCP) { string name = string.Empty; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url = null; bool result = Uri.TryCreate(name, UriKind.Absolute, out url); if (result) { WebDownload manager = new WebDownload(); manager.FileToDownload = url.AbsoluteUri; manager.Show(); return(0); } } return(1); }
/// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> /// <returns>Return S_OK (0) or 1.</returns> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { string name; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url; if (Uri.TryCreate(name, UriKind.Absolute, out url)) { Debug.WriteLine("DownloadManager: initial URL is: " + url); if (FileDownloading != null) { FileDownloading(this, new FileDownloadEventArgs(url)); ////DownloadMod(url.ToString()); } return WebBrowserEx.Constants.S_OK; } } return 1; }
/// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> /// <returns>Return S_OK (0) or 1.</returns> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { string name; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url; if (Uri.TryCreate(name, UriKind.Absolute, out url)) { Debug.WriteLine("DownloadManager: initial URL is: " + url); if (FileDownloading != null) { FileDownloading(this, new FileDownloadEventArgs(url)); ////DownloadMod(url.ToString()); } return(WebBrowserEx.Constants.S_OK); } } return(1); }
/// <summary> /// event called when the browser is about to download a file /// </summary> //public event EventHandler<FileDownloadEventArgs> FileDownloading; /// <summary> /// Return S_OK (0) so that IE will stop to download the file itself. /// Else the default download user interface is used. /// </summary> public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP) { // Get the display name of the pointer to an IMoniker interface that specifies the object to be downloaded. string name; pmk.GetDisplayName(pbc, null, out name); if (!string.IsNullOrEmpty(name)) { Uri url; if (Uri.TryCreate(name, UriKind.Absolute, out url)) { //if (FileDownloading != null) //{ // FileDownloading(this, new FileDownloadEventArgs(url)); //} System.Net.WebRequest wc = System.Net.WebRequest.Create(name); //args[0]); ((HttpWebRequest)wc).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.1 Safari/525.19"; wc.Timeout = 10000; wc.Method = "HEAD"; ((HttpWebRequest)wc).CookieContainer = GetUriCookieContainer(url); WebResponse res = wc.GetResponse(); var streamReader = new System.IO.StreamReader(res.GetResponseStream()); var header = res.Headers["Content-Disposition"]; //System.Windows.MessageBox.Show(header); //Console.WriteLine(streamReader.ReadToEnd()); //return Constants.S_OK; return(1); } } return(1); }
public static DTE GetDTE(int processId) { string progId = "!VisualStudio.DTE.10.0:" + processId.ToString(); object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (enumMonikers.Next(1, moniker, numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal)) { Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject)); break; } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return((DTE)runningObject); }
/// <summary> /// This method is for internal purpose. Don't call it directly. /// </summary> /// <param name="moniker"></param> /// <returns></returns> public int SelectedFilter(IMoniker moniker) { if (moniker == null) throw new ArgumentNullException("moniker"); int retval = S_OK; string monikerDisplayName = null; Guid filterClsid; // Get the moniker's display name. // DirectShow filter have this form: @device:sw:FilterCategory\CLSID moniker.GetDisplayName(null, null, out monikerDisplayName); Debug.WriteLine(string.Format("\r\nBlackListManager.SelectedFilter:\r\nMoniker: {0}", monikerDisplayName)); // Did the display name normally composed ? int i = monikerDisplayName.LastIndexOf('\\'); if (i != -1) { // Yes, tring to get the CLSID if (GuidTryParse(monikerDisplayName.Substring(i + 1), out filterClsid)) { // Display informations about this filter this.DisplayDebugInformation(moniker, filterClsid); // Test if the filter is black-listed if (this.blackList.Contains(filterClsid)) { Debug.WriteLine("WARNING: This filter is black-listed! Rejecting it."); // The filter is black listed. Rejecting it. retval = E_FAIL; } } } return retval; }
/// <summary> /// Retrieve the a moniker's display name (i.e. it's unique string). /// </summary> protected string GetMonikerString(IMoniker moniker) { moniker.GetDisplayName(null, null, out var s); return(s); }
/// <summary> Retrieve the a moniker's display name (i.e. it's unique string) </summary> static string GetMonikerString(IMoniker Moniker) { Moniker.GetDisplayName(null, null, out var s); return(s); }
public static string GetMonikerDisplayName(IMoniker pmk) { string strDisplayName; IBindCtx bindCtx = CreateBindCtx(0); pmk.GetDisplayName(bindCtx, null, out strDisplayName); Marshal.ReleaseComObject(bindCtx); return strDisplayName; }
private static DTE GetDTE(int processId) { MessageFilter.Register(); var prefix = Process.GetProcessById(processId).ProcessName; if ("devenv".Equals(prefix, StringComparison.OrdinalIgnoreCase)) { prefix = "VisualStudio"; } string progId = string.Format("!{0}.DTE.{1}:{2}", prefix, AssemblyVersionInfo.VSVersion, processId); object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; uint numberFetched = 0; while (enumMonikers.Next(1, moniker, out numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal)) { rot.GetObject(runningObjectMoniker, out runningObject); break; } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return((DTE)runningObject); }
/// <summary> /// Gets the moniker string for a specific moniker /// </summary> /// <param name="moniker">Moniker to retrieve the moniker string of</param> /// <returns>Moniker string</returns> private static string GetMonikerString(IMoniker moniker) { // Declare variables string result = ""; // Get the display name of the moniker moniker.GetDisplayName(null, null, out result); // Return result return result; }
public static string MonikerToString(IMoniker moniker, uint codepage, out string url) { // Create binding context that will be needed to get the url from the moniker IBindCtx bindCtx; int hr = Ole32.CreateBindCtx(0, out bindCtx); if (hr != HRESULT.S_OK) throw new COMException("Error creating binding context", hr); // Get the url of the moniker string name; moniker.GetDisplayName(bindCtx, null, out name); url = name; // Get a stream to the content of the url IStream stream; ComHelper.Chk(UrlMon.URLOpenBlockingStream(IntPtr.Zero, name, out stream, 0, null)); // Read the contents of the url, which should be the html to an email message using (ComStream comStream = new ComStream(stream, false)) { using (StreamReader sr = new StreamReader(comStream, Encoding.GetEncoding((int)codepage))) { return sr.ReadToEnd(); } } }
/// <summary> /// Gets the DTE object from any devenv process. /// </summary> /// <param name="processId"> /// <returns> /// Retrieved DTE object or <see langword="null"> if not found. /// </see></returns> private static EnvDTE.DTE GetDTE(int processId) { object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (enumMonikers.Next(1, moniker, numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } Regex monikerRegex = new Regex(@"!VisualStudio.DTE\.\d+\.\d+\:" + processId, RegexOptions.IgnoreCase); if (!string.IsNullOrEmpty(name) && monikerRegex.IsMatch(name)) { Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject)); break; } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return(runningObject as EnvDTE.DTE); }
private static DTE FindDTE(string file) { const string id = "!VisualStudio.DTE."; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(0, out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (enumMonikers.Next(1, moniker, numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && name.StartsWith(id, StringComparison.Ordinal)) { object runningObject; Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject)); var dte = runningObject as DTE; if (dte != null && dte.Solution.FindProjectItem(file) != null) { Console.WriteLine(dte.Solution.FindProjectItem(file).Name); return(dte); } } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return(null); }
private static DTE GetDTE(int processId) { #if DEV15 // VS 2017 doesn't install some assemblies to the GAC that are needed to work with the // debugger, and as the tests don't execute in the devenv.exe process, those assemblies // fail to load - so load them manually from PublicAssemblies. // Use the executable name, as this is only needed for the out of proc test execution // that may interact with the debugger (vstest.executionengine.x86.exe). if (!DTELoaded) { string currentProc = Process.GetCurrentProcess().MainModule.FileName; if (StringComparer.OrdinalIgnoreCase.Equals( Path.GetFileName(currentProc), "vstest.executionengine.x86.exe")) { string baseDir = Path.GetDirectoryName(currentProc); string publicAssemblies = Path.Combine(baseDir, "..\\..\\..\\PublicAssemblies"); Assembly.LoadFrom(Path.Combine(publicAssemblies, "Microsoft.VisualStudio.OLE.Interop.dll")); Assembly.LoadFrom(Path.Combine(publicAssemblies, "envdte90.dll")); Assembly.LoadFrom(Path.Combine(publicAssemblies, "envdte80.dll")); Assembly.LoadFrom(Path.Combine(publicAssemblies, "envdte.dll")); } DTELoaded = true; } #endif MessageFilter.Register(); var prefix = Process.GetProcessById(processId).ProcessName; if ("devenv".Equals(prefix, StringComparison.OrdinalIgnoreCase)) { prefix = "VisualStudio"; } string progId = string.Format("!{0}.DTE.{1}:{2}", prefix, AssemblyVersionInfo.VSVersion, processId); object runningObject = null; IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; uint numberFetched = 0; while (enumMonikers.Next(1, moniker, out numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal)) { rot.GetObject(runningObjectMoniker, out runningObject); break; } } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return((DTE)runningObject); }
// Regex for matching the VS DTE moniker as stored in the COM running object table. // Note the process id suffix to the string which we extract as capture group 1. // Example target: "!VisualStudio.DTE.11.0:11944" /// <summary> /// Returns collection of any/all EnvDTE.DTE instances running on the machine. /// http://blogs.msdn.com/b/kirillosenkov/archive/2011/08/10/how-to-get-dte-from-visual-studio-process-id.aspx /// </summary> /// <returns>Collection of EnvDTE.DTE instances keyed by the ID of the process running the DTE.</returns> public static Dictionary <int, EnvDTE.DTE> GetAllDTEs() { Dictionary <int, EnvDTE.DTE> dtes = new Dictionary <int, EnvDTE.DTE>(); IBindCtx bindCtx = null; IRunningObjectTable rot = null; IEnumMoniker enumMonikers = null; try { Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx)); bindCtx.GetRunningObjectTable(out rot); rot.EnumRunning(out enumMonikers); IMoniker[] moniker = new IMoniker[1]; IntPtr numberFetched = IntPtr.Zero; while (enumMonikers.Next(1, moniker, numberFetched) == 0) { IMoniker runningObjectMoniker = moniker[0]; string name = null; try { if (runningObjectMoniker != null) { runningObjectMoniker.GetDisplayName(bindCtx, null, out name); } } catch (UnauthorizedAccessException) { // Do nothing, there is something in the ROT that we do not have access to. } // Parse the moniker to match against target spec. and extract process id. int processId; Match match = s_regex_dteMoniker.Match(name); if (!match.Success || match.Groups.Count != 2) { continue; } processId = int.Parse(match.Groups[1].Value); // Store the DTE. object runningObject = null; Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject)); dtes[processId] = (EnvDTE.DTE)runningObject; } } finally { if (enumMonikers != null) { Marshal.ReleaseComObject(enumMonikers); } if (rot != null) { Marshal.ReleaseComObject(rot); } if (bindCtx != null) { Marshal.ReleaseComObject(bindCtx); } } return(dtes); }