I can start a new hidden Visual Studio process from VBScript, and drive it programmatically, by doing this:
Set DTE = CreateObject("VisualStudio.DTE.8.0")
DTE.DoStuff()
How do I do that in C#? (Edit: using the correct types, not generic COM objects as used by that VBScript code.)
I've tried this:
using EnvDTE;
...
DTE dte = new DTE();
but I get "Retrieving the COM class factory for component with CLSID {3C9CFE1E-389F-4118-9FAD-365385190329} failed".
I found the answer (thanks to Sebastiaan Megens for putting me on the right track):
[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
(The nonsense with STAThread and MessageFilter is "due to threading contention issues between external multi-threaded applications and Visual Studio", whatever that means. Pasting in the code from http://msdn.microsoft.com/en-us/library/ms228772.aspx makes it work.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With