Project specifics: Visual Studio 2005 C# Addin (under XP SP2)
I am having problems casting an project object from Project to VCProject. I
know it's a C++ project because I created a test one. When I try to cast it,
I get an E_NOINTERFACE exception with the following message:
"Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.VisualStudio.VCProjectEngine.VCProject'. This operation failed
because the QueryInterface call on the COM component for the interface with
IID '{238B5174-2429-11D7-8BF6-00B0D03DAA06}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."
Thie code is structured so that there is delegate registered to handle the
OnOpened event:
// ********************************
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
solutionEvents = _applicationObject.Events.SolutionEvents;
solutionEvents.Opened += new
_dispSolutionEvents_OpenedEventHandler(UpDateSolSettings);
}
// *********************************
The handler is supposed to modify attributes of all the VCProjects of the
solution:
// *********************************
private void UpDateSolSettings()
{
const string PROJECT_KIND_CPLUSPLUS_PROJECT =
"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
VCConfiguration conf;
try
{
string strBaseDir = GetBaseDir();
if (null != strBaseDir)
{
foreach (Project proj in
_applicationObject.Solution.Projects)
{
if (proj.Kind == PROJECT_KIND_CPLUSPLUS_PROJECT)
{
VCProject vcproj = (VCProject)proj; //
<********* THE EXCEPTION IS THROWN HERE.
IVCCollection Confs =
(IVCCollection)vcproj.Configurations;
for (int iConf = 0, nConf = Confs.Count; iConf <
nConf; iConf++)
{
conf = (VCConfiguration)Confs.Item(iConf);
conf.IntermediateDirectory = strBaseDir +
@"\obj\win32\$(TargetName)";
}
}
}
}
}
catch(Exception e)
{
MessageBox.Show("Error: " + e.Message, "Error while
processing sol file.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// *********************************
Again, I am certain the solution only one project, and it's a VC++ project,
so the "proj" object should have the VCProject interface implemented.
Any ideas on what could be going on?
Any help greatly appreciated.
Regards,
Luis Bascones
You can not cast the variable proj from its type EnvDTE.Project to
VCProject. You must use the proj.Object property:
VCProject vcproj = (VCProject)proj.Object(); //
You can know the type behing a Object property as explained in my article:
HOWTO: Know the actual type behind a System.__ComObject type returned by an
.Object property.
http://www.mztools.com/resources_vsnet_addins.htm
--
Best regards,
Carlos J. Quintero
MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Luis Bascones" <Luis.B...@mathworks.com> escribió en el mensaje
news:edPEVS1O...@TK2MSFTNGP03.phx.gbl...
Oh, I see. I guess the type I was casting from was just an interface type
and therefore I could not use it to QI for a another interface. I assume
that .Object returns IUnknown, which will allow me to QI for the interface I
am interested in (VCProject).
I will give that a shot. Hopefully it will make my code just work.
Gracias nuevamente,
-Luis Bascones
"Carlos J. Quintero [VB MVP]" <nos...@nospam.com> wrote in message
news:uqTJ$XjPHH...@TK2MSFTNGP05.phx.gbl...