2009-08-13 11 views
4
私はVBScriptのから新しい隠されたVisual Studioのプロセスを開始し、このことによって、プログラムでそれを運転することができ

:私はそれを行うにはどうすればよいVisual Studioをプログラムで起動します。 C VBのCreateObjectのの#1当量(「VisualStudio.DTE.8.0」)

Set DTE = CreateObject("VisualStudio.DTE.8.0") 
DTE.DoStuff() 

をC#? (編集:そのVBScriptコードで使用されるような正しいタイプではなく、汎用のCOMオブジェクトを使用して。)私が試した

この:

using EnvDTE; 
... 
DTE dte = new DTE(); 

が、私は、コンポーネントのCOMクラスファクトリを取得」取得CLSID {3C9CFE1E-389F-4118-9FAD-365385190329}で失敗しました。

+0

私はこれについてはわかりませんが、Process.Start( "devenv.exe"); ? – Galilyou

+0

@ 7alwagy: "プログラムでドライブする" – RichieHindle

答えて

7

私は(右のトラックに私を置くためのSebastiaan Megensのおかげで)答えを見つけました:

[STAThread] 
static void Main(string[] args) 
{ 
    System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0", true); 
    DTE2 dte = (EnvDTE80.DTE2)System.Activator.CreateInstance(t, true); 

    // See http://msdn.microsoft.com/en-us/library/ms228772.aspx for the 
    // code for MessageFilter - just paste it in. 
    MessageFilter.Register(); 

    dte.DoStuff(); 
    dte.Quit(); 
} 

public class MessageFilter : IOleMessageFilter 
{ 
    ... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx 

(STAThreadとMessageFilterとナンセンス「によるものです外部のマルチスレッドアプリケーションとVisual Studioとの間の競合問題をスレッド化すること」を意味します。http://msdn.microsoft.com/en-us/library/ms228772.aspxのコードを貼り付けることで動作します)

+1

ああ、聞いて冷たい!面白いことに、将来私はいつか自分でそれが必要になるでしょう。 :) よろしく、 Sebastiaan –

+0

誰か他の人に問題がある場合は、この/ Microsoftの 'MessageFilter.Register()'コールを私が**置くまで**成功させていないだけです'CreateInstance'呼び出しです。 – sorrell

0

簡単な答え:VBで書いてコンパイルし、リフレクターで開き、C#モードで逆コンパイルしてください!

+0

VBコードではレイトバインディングを使用しています(つまり、DTEは汎用COMオブジェクトです)、早期バインディングを使用するようにします。 – RichieHindle

+0

Reflectorを使用する代わりに、Microsoftが提供するソースを表示するだけではどうですか? – AMissico

3

私は、Visual Studioの新しいインスタンス起動方法を知りませんが、私は呼び出すことにより、既存のインスタンスを使用します。

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0"); 

はたぶん、新しいインスタンスを作成すると、似たようなのですか?これが少し助けてくれることを願います。

よろしく、

Sebastiaan

+0

私は正しい軌道に乗るために+1 - あなたのコードのためのグーグルは私につながったhttp://msdn.microsoft.com/en-us/library/68shb4dw%28VS.80%29.aspx新しいインスタンスを作成するための既存のインスタンス*および*コード – RichieHindle

2

VBのCreateObject用のMicrosoftソースコード。

<HostProtection(Resources:=HostProtectionResource.ExternalProcessMgmt)> _ 
    <SecurityPermissionAttribute(SecurityAction.Demand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _ 
    Public Function CreateObject(ByVal ProgId As String, Optional ByVal ServerName As String = "") As Object 
     'Creates local or remote COM2 objects. Should not be used to create COM+ objects. 
     'Applications that need to be STA should set STA either on their Sub Main via STAThreadAttribute 
     'or through Thread.CurrentThread.ApartmentState - the VB runtime will not change this. 
     'DO NOT SET THREAD STATE - Thread.CurrentThread.ApartmentState = ApartmentState.STA 

     Dim t As Type 

     If ProgId.Length = 0 Then 
      Throw VbMakeException(vbErrors.CantCreateObject) 
     End If 

     If ServerName Is Nothing OrElse ServerName.Length = 0 Then 
      ServerName = Nothing 
     Else 
      'Does the ServerName match the MachineName? 
      If String.Compare(Environment.MachineName, ServerName, StringComparison.OrdinalIgnoreCase) = 0 Then 
       ServerName = Nothing 
      End If 
     End If 

     Try 
      If ServerName Is Nothing Then 
       t = Type.GetTypeFromProgID(ProgId) 
      Else 
       t = Type.GetTypeFromProgID(ProgId, ServerName, True) 
      End If 

      Return System.Activator.CreateInstance(t) 
     Catch e As COMException 
      If e.ErrorCode = &H800706BA Then 
       '&H800706BA = The RPC Server is unavailable 
       Throw VbMakeException(vbErrors.ServerNotFound) 
      Else 
       Throw VbMakeException(vbErrors.CantCreateObject) 
      End If 
     Catch ex As StackOverflowException 
      Throw ex 
     Catch ex As OutOfMemoryException 
      Throw ex 
     Catch ex As System.Threading.ThreadAbortException 
      Throw ex 
     Catch e As Exception 
      Throw VbMakeException(vbErrors.CantCreateObject) 
     End Try 
    End Function 
関連する問題