Silverlight 5 Trusted Application EnumKeys StdRegProv keeps getting Type Mismatch

0

I have a signed, trusted application in Silverlight 5 (and know its working correctly and AutomationFactory is enabled). However, no matter what I do, I get a COM exception of type mismatch, which is no help, when I try to query the registry. Windows 7 IE8.

{System.Runtime.InteropServices.COMException (0x80041005): Exception from HRESULT: 0x80041005 ---> MS.Internal.ComAutomation.ComAutomationObjectException: Type mismatch 
(Source=SWbemObjectEx)
   at MS.Internal.ComAutomation.ComAutomationNative.CheckInvokeHResult(UInt32 hr, String memberName, String exceptionSource, String exceptionDescription, String exceptionHelpFile, UInt32 exceptionHelpContext)
   at MS.Internal.ComAutomation.ComAutomationNative.Invoke(Boolean tryInvoke, String memberName, ComAutomationInvokeType invokeType, ComAutomationInteropValue[] rgParams, IntPtr nativePeer, ComAutomationInteropValue& returnValue)
   at MS.Internal.ComAutomation.ComAutomationObject.InvokeImpl(Boolean tryInvoke, String name, ComAutomationInvokeType invokeType, Object& returnValue, Object[] args)
   at MS.Internal.ComAutomation.ComAutomationObject.Invoke(String name, ComAutomationInvokeType invokeType, Object[] args)
   at System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
   at System.Runtime.InteropServices.Automation.AutomationMetaObjectProviderBase.<.cctor>b__4(Object obj, InvokeMemberBinder binder, Object[] args)
   at CallSite.Target(Closure , CallSite , Object , UInt32 , String , String[]& )
   at CallSite.Target(Closure , CallSite , Object , UInt32 , String , String[]& )

Here is my code

uint HKEY_LOCAL_MACHINE = 0x80000002;
var locatorService = AutomationFactory.CreateObject("WbemScripting.SWbemLocator");
var wmiService = locatorService.ConnectServer(".", "root\\DEFAULT");
wmiService.Security_.ImpersonationLevel = 3;
wmiService.Security_.AuthenticationLevel = 4;
var objRegistry = wmiService.Get("StdRegProv");
string strRegIdentityCodes = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
string[] result = null;
objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strRegIdentityCodes, out result);
c#
silverlight
com
registry
wmi
asked on Stack Overflow May 7, 2013 by AberAber • edited May 8, 2013 by Simon Mourier

1 Answer

0

I think you need to do two changes:

1) change:

uint HKEY_LOCAL_MACHINE = 0x80000002;

to:

int HKEY_LOCAL_MACHINE = unchecked((int)0x80000002);

to avoid the type mismatch error, and

2) change:

string[] result = null;

to:

object[] result = null;

to avoid a forthcoming "System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.String[]'"

answered on Stack Overflow May 8, 2013 by Simon Mourier

User contributions licensed under CC BY-SA 3.0