MSDN中描述DTE结构的代码如下:
#pragma warning( disable : 4278 )
//The following #import imports DTE based on its LIBID
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0") lcid("0") raw_interfaces_only named_guids
#pragma warning( default : 4278 )
using namespace EnvDTE; // optional
//……
void Ct3Dlg::OnBnClickedOk()
{
CoInitialize(NULL);
{
_variant_t varRelease=L"Release";
_bstr_t bstrSlnPath=L"C:\\test\\W3\\W3.sln";
try
{
CComPtr<EnvDTE::_DTE> pZFIVE5_DTE;
_Solution *pZFIVE5_S=NULL;
SolutionBuild *pZFIVE5_SB=NULL;
SolutionConfigurations *pZFIVE5_SCs=NULL;
SolutionConfiguration *pZFIVE5_SC=NULL;
pZFIVE5_DTE.CoCreateInstance(L"VisualStudio.DTE.7.1", 0);
pZFIVE5_DTE->get_Solution(&pZFIVE5_S);
if(pZFIVE5_S==NULL)
{
pZFIVE5_DTE->Quit();
goto ZFive5ComRet;
}
pZFIVE5_S->Open(bstrSlnPath);
VARIANT_BOOL var;
pZFIVE5_S->get_IsOpen(&var);
if (var==VARIANT_TRUE)
{
pZFIVE5_S->get_SolutionBuild(&pZFIVE5_SB);
if(pZFIVE5_SB==NULL)
{
pZFIVE5_S->Release();
pZFIVE5_S=NULL;
pZFIVE5_DTE->Quit();
goto ZFive5ComRet;
}
pZFIVE5_SB->get_SolutionConfigurations(&pZFIVE5_SCs);
if(pZFIVE5_SCs==NULL)
{
pZFIVE5_SB->Release();
pZFIVE5_SB=NULL;
pZFIVE5_S->Release();
pZFIVE5_S=NULL;
pZFIVE5_DTE->Quit();
goto ZFive5ComRet;
}
pZFIVE5_SCs->Item(varRelease,&pZFIVE5_SC);
if(pZFIVE5_SC!=NULL)
{
pZFIVE5_SC->Activate();
pZFIVE5_SC->Release();
pZFIVE5_SC=NULL;
}
pZFIVE5_SCs->Release();
pZFIVE5_SCs=NULL;
pZFIVE5_SB->Build(VARIANT_TRUE);
pZFIVE5_SB->Release();
pZFIVE5_SB=NULL;
pZFIVE5_S->Close(0);
}
else
{
AfxMessageBox("打开工程错误!");
}
pZFIVE5_S->Release();
pZFIVE5_S=NULL;
pZFIVE5_DTE->Quit();
}
catch(...)
{
AfxMessageBox("接口调用错误!");
}
}
ZFive5ComRet:
::CoUninitialize();
return;
}
……