MSDN中描述DTE结构的示意图如下:
VC的代码如下:
#pragma warning( disable : )
//The following #import imports DTE based on its LIBID
#import libid:ccfeddddbdecdee version() lcid() raw_interfaces_only named_guids
#pragma warning( default : )
using namespace EnvDTE; // optional
//……
void CtDlg::OnBnClickedOk()
{
CoInitialize(NULL);
{
_variant_t varRelease=LRelease;
_bstr_t bstrSlnPath=LC:\\test\\W\\Wsln;
try
{
CComPtr pZFIVE_DTE;
_Solution *pZFIVE_S=NULL;
SolutionBuild *pZFIVE_SB=NULL;
SolutionConfigurations *pZFIVE_SCs=NULL;
SolutionConfiguration *pZFIVE_SC=NULL;
pZFIVE_DTECoCreateInstance(LVisualStudioDTE );
pZFIVE_DTE>get_Solution(&pZFIVE_S);
if(pZFIVE_S==NULL)
{
pZFIVE_DTE>Quit();
goto ZFiveComRet;
}
pZFIVE_S>Open(bstrSlnPath);
VARIANT_BOOL var;
pZFIVE_S>get_IsOpen(&var);
if (var==VARIANT_TRUE)
{
pZFIVE_S>get_SolutionBuild(&pZFIVE_SB);
if(pZFIVE_SB==NULL)
{
pZFIVE_S>Release();
pZFIVE_S=NULL;
pZFIVE_DTE>Quit();
goto ZFiveComRet;
}
pZFIVE_SB>get_SolutionConfigurations(&pZFIVE_SCs);
if(pZFIVE_SCs==NULL)
{
pZFIVE_SB>Release();
pZFIVE_SB=NULL;
pZFIVE_S>Release();
pZFIVE_S=NULL;
pZFIVE_DTE>Quit();
goto ZFiveComRet;
}
pZFIVE_SCs>Item(varRelease&pZFIVE_SC);
if(pZFIVE_SC!=NULL)
{
pZFIVE_SC>Activate();
pZFIVE_SC>Release();
pZFIVE_SC=NULL;
}
pZFIVE_SCs>Release();
pZFIVE_SCs=NULL;
pZFIVE_SB>Build(VARIANT_TRUE);
pZFIVE_SB>Release();
pZFIVE_SB=NULL;
pZFIVE_S>Close();
}
else
{
AfxMessageBox(打开工程错误!);
}
pZFIVE_S>Release();
pZFIVE_S=NULL;
pZFIVE_DTE>Quit();
}
catch()
{
AfxMessageBox(接口调用错误!);
}
}
ZFiveComRet:
::CoUninitialize();
return;
}