System.InvalidCastException

Add-in Express™ Support Service
That's what is more important than anything else

System.InvalidCastException
 
Perry Kivolowitz




Posts: 10
Joined: 2010-11-09
Hi,

In my code:

// The getElelementsByName will always return non-null as at worst it is an empty collection.
mshtml.IHTMLElementCollection c = HTMLDocument.getElementsByName("q");
System.Diagnostics.Debug.WriteLine("Retrieved the Q element");

// Ensure the collection is not empty.
if (c.length == 0)
	return null;

// Fetch the control contained in the collection's first (only) member.
mshtml.HTMLInputElementClass e = (mshtml.HTMLInputElementClass) c.item();
System.Diagnostics.Debug.WriteLine("Performed Cast - the rest of the code exists the function.");


The first Debug.WriteLine executes. The second is not reached. Rather, the following exception occurs:


Detailed technical information follows: 
---
(Inner Exception)
Date and Time:         1/22/2011 4:09:19 PM
Machine Name:          HOMEBREW
IP Address:            fe80::4d1:3cd9:fae6:ad9c%11
Current User:          HomeBrewperryk

Application Domain:    C:Program Files (x86)BiosciencesV2
Assembly Codebase:     file:///C:/Program Files (x86)/Biosciences/V2/AddinExpress.IE.DLL
Assembly Full Name:    AddinExpress.IE, Version=6.3.376.0, Culture=neutral, PublicKeyToken=4416dd98f0861965
Assembly Version:      6.3.376.0
Assembly Build Date:   1/22/2011 4:08:40 PM

Exception Source:      V2
Exception Type:        System.InvalidCastException
Exception Message:     Unable to cast COM object of type 'System.__ComObject' to class type 'mshtml.HTMLInputElementClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Exception Target Site: CheckRequirements

---- Stack Trace ----
   V2.IEModule.CheckRequirements()
       AddinExpress.IE.DLL: N 0163 (0xA3) IL 
   V2.IEModule.IEModule_BeforeNavigate2(e As ADXIEBeforeNavigate2EventArgs)
       AddinExpress.IE.DLL: N 0012 (0xC) IL 
   AddinExpress.IE.ADXIEModule.DoBeforeNavigate2(pDisp As Object, url As Object&, flags As Object&, targetFrameName As Object&, postData As Object&, headers As Object&, cancel As Boolean&)
       AddinExpress.IE.DLL: N 0095 (0x5F) IL 



(Outer Exception)
Date and Time:         1/22/2011 4:09:19 PM
Machine Name:          HOMEBREW
IP Address:            fe80::4d1:3cd9:fae6:ad9c%11
Current User:          HomeBrewperryk

Application Domain:    C:Program Files (x86)BiosciencesV2
Assembly Codebase:     file:///C:/Program Files (x86)/Biosciences/V2/AddinExpress.IE.DLL
Assembly Full Name:    AddinExpress.IE, Version=6.3.376.0, Culture=neutral, PublicKeyToken=4416dd98f0861965
Assembly Version:      6.3.376.0
Assembly Build Date:   1/22/2011 4:08:40 PM

Exception Source:      
Exception Type:        AddinExpress.IE.ADXIEExternalException
Exception Message:     An error has occured in the code of the extension.
Exception Target Site: Object reference not set to an instance of an object.

---- Stack Trace ----


This BHO works on my machine (a development machine with no UAC) and on one test machine (possible no UAC). It fails on a x64 Windows 7 running IE 8 with all the UAC controls active.

Can you help?

Thank you
Posted 22 Jan, 2011 16:28:16 Top
Sergey Grischenko


Add-in Express team


Posts: 7202
Joined: 2004-07-05
Hi Perry.

Please try to cast the object to the mshtml.IHTMLInputElement or just mshtml.IHTMLElement interface.
Posted 25 Jan, 2011 11:41:57 Top