我如何使用EnvDTE打开一个新的Visual Studio实例? [英] How do I open a new visual studio instance using EnvDTE?
问题描述
我使用EnvDTE COM对象自动化的Visual Studio。我还是通过文档看,但是,我想我会在这里掉落的问题,看看是否有人已经知道如何做到这一点。
I'm using the EnvDTE COM objects to automate Visual Studio. I'm still looking through the documentation, however, I figured I'd drop the question here to see if someone already knows how to do this.
那么,问题是,当我获得DTE对象,看看有什么解决办法是开放的,我需要的,如果解决的办法是不是我很期待打开Visual Studio中的另一个实例。我并不希望加载在VS的现有实例另一种解决方案。
So, the problem is that when I get the DTE object and look at what Solution is open, I need to open another instance of Visual Studio if the solution is not the one I'm expecting. I don't necessarily want to load another solution over an existing instance of VS.
推荐答案
所以,我想通了这一点对我自己。原来,该RunningObjectTable是遍历所有VS情况下,寻找一个具有开放式的解决方案,我期待的方式。如果我没有发现我可以用一个实例,然后我调用CoCreateInstance()合作推出一个新的VS IDE:
So, I figured this out on my own. It turns out that the RunningObjectTable is the way to iterate over all VS instances and look for one with the solution open that I am expecting. If I don't find an instance that I can use, then I call CoCreateInstance() to launch a new VS IDE:
#define RETURN_ON_FAIL( expression ) \
result = ( expression ); \
if ( FAILED( result ) ) \
return false; \
else // To prevent danging else condition
...
HRESULT result;
CLSID clsid;
CComPtr<IUnknown> punk;
CComPtr<EnvDTE::_DTE> dte;
RETURN_ON_FAIL( ::CLSIDFromProgID(L"VisualStudio.DTE", &clsid) );
...
// Search through the Running Object Table for an instance of Visual Studio
// to use that either has the correct solution already open or does not have
// any solution open.
CComPtr<IRunningObjectTable> ROT;
RETURN_ON_FAIL( GetRunningObjectTable( 0, &ROT ) );
CComPtr<IBindCtx> bindCtx;
RETURN_ON_FAIL( CreateBindCtx( 0, &bindCtx ) );
CComPtr<IEnumMoniker> enumMoniker;
RETURN_ON_FAIL( ROT->EnumRunning( &enumMoniker ) );
CComPtr<IMoniker> dteMoniker;
RETURN_ON_FAIL( CreateClassMoniker( clsid, &dteMoniker ) );
CComBSTR bstrSolution( solutionPath );
CComPtr<IMoniker> moniker;
ULONG monikersFetched = 0;
while ( enumMoniker->Next( 1, &moniker, &monikersFetched ) == S_OK)
{
if ( moniker->IsEqual( dteMoniker ) )
{
result = ROT->GetObject( moniker, &punk );
if ( result == S_OK )
{
dte = punk;
if ( dte )
{
CComPtr<EnvDTE::_Solution> solution;
RETURN_ON_FAIL( dte->get_Solution( &solution ) );
VARIANT_BOOL isOpen = FALSE;
RETURN_ON_FAIL( solution->get_IsOpen( &isOpen ) );
if ( !isOpen )
{
RETURN_ON_FAIL( solution->Open( bstrSolution ) );
break;
}
else
{
CComBSTR fullName;
RETURN_ON_FAIL( solution->get_FullName( &fullName ) );
if ( fullName == bstrSolution )
break;
}
}
}
punk = NULL;
}
moniker = NULL;
}
if ( !dte )
{
RETURN_ON_FAIL( ::CoCreateInstance( clsid, NULL, CLSCTX_LOCAL_SERVER, EnvDTE::IID__DTE, (LPVOID*)&punk ) );
dte = punk;
if ( !dte )
return false;
if ( solutionPath )
{
CComPtr<EnvDTE::_Solution> solution;
RETURN_ON_FAIL( dte->get_Solution( &solution ) );
CComBSTR bstrSolution( solutionPath );
RETURN_ON_FAIL( solution->Open( bstrSolution ) );
}
}
这篇关于我如何使用EnvDTE打开一个新的Visual Studio实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!