Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Extensibility / January 2007

Tip: Looking for answers? Try searching our database.

Failing to cast Project to VCProject (VS 2005 C# Addin)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Luis Bascones - 18 Jan 2007 23:01 GMT
Hello all.

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
Carlos J. Quintero [VB MVP] - 22 Jan 2007 15:00 GMT
Hi Luis,

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

Signature

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

> Hello all.
>
[quoted text clipped - 83 lines]
>
> Luis Bascones
Luis Bascones - 22 Jan 2007 15:44 GMT
Gracias Carlos,

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

> Hi Luis,
>
[quoted text clipped - 96 lines]
>>
>> Luis Bascones

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.