2012-08-06 98 views
5

我正在嘗試使用運行對象表來獲取DTE特定的Visual Studio實例。我打算使用MSDN上描述的技術。我設法讓其中一個實例列出,但不是其他實例。瞭解運行對象表

public static void PrintRot() 
{ 
    IRunningObjectTable rot; 
    IEnumMoniker enumMoniker; 
    int retVal = GetRunningObjectTable(0, out rot); 

    if (retVal == 0) 
    { 
     rot.EnumRunning(out enumMoniker); 

     IntPtr fetched = IntPtr.Zero; 
     IMoniker[] moniker = new IMoniker[1]; 
     while (enumMoniker.Next(1, moniker, fetched) == 0) 
     { 
      IBindCtx bindCtx; 
      CreateBindCtx(0, out bindCtx); 
      string displayName; 
      moniker[0].GetDisplayName(bindCtx, null, out displayName); 
      Console.WriteLine("Display Name: {0}", displayName); 
     } 
    } 
} 

[DllImport("ole32.dll")] 
private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc); 

[DllImport("ole32.dll")] 
private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot); 

下面是結果:

Display Name: !VisualStudio.DTE.11.0:7120 
Display Name: clsid:331F1768-05A9-4DDD-B86E-DAE34DDC998A: 
Display Name: !{7751A556-096C-44B5-B60D-4CC78885F0E5} 
Display Name: c:\users\dave\documents\visual studio 2012\Projects\MyProj\MyProj.sln 
Display Name: !{059618E6-4639-4D1A-A248-1384E368D5C3} 

我希望看到多條線路與VisualStudio.DTE我在做什麼錯?我應該期待看到什麼?

編輯:

這似乎與應用程序是否在運行提升權限。如果我保持一致並使用正常模式,那麼它就可以工作。不過,我希望它能夠工作,不管怎樣,我如何獲得所有進程的ROT?

+0

做工精細當我嘗試它。 – 2012-08-06 21:24:00

+1

你在運行另一個實例嗎?你運行這個EXE提升? – 2012-08-06 21:27:55

+0

@HansPassant你在結果中看到了什麼? – 2012-08-06 21:38:42

回答

8

你在運行另一個實例嗎?你運行的EXE提升?

當您是一個以標準用戶身份運行的進程時,您只能看到屬於您的進程/ etc。所以你不會看到以管理員身份運行的進程。

當使用升級的特權運行時,您可以看到屬於所有用戶的所有進程。

理想的情況下,一切都將始終運行的「最低特權用戶」,看到http://en.wikipedia.org/wiki/Principle_of_least_privilege

+0

任何解釋?預期GetRunningObjectTable對於不同特權的行爲是什麼? – 2012-08-06 22:18:03

+1

你希望它返回你永遠不能使用的接口指針嗎? – 2012-08-07 01:11:53

+0

儘管@HansPassant在他的精闢評論中非常明智,但更爲翔實的答案可能涉及提及安全性和流程隔離,更具體地說,允許未升級的流程操縱升級的流程可能允許未升級的流程執行不允許的事情做。這被稱爲「特權升級」,是一件壞事。 – x0n 2017-11-20 20:14:44