我在Silverlight 5中有一个已签名,受信任的应用程序(并且知道它正常工作并启用了AutomationFactory)。但是,无论我做什么,当我尝试查询注册表时,我得到类型不匹配的COM异常,这没有任何帮助。 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[]& )
这是我的代码
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);
答案 0 :(得分:0)
我认为您需要做两处更改:
1)改变:
uint HKEY_LOCAL_MACHINE = 0x80000002;
为:
int HKEY_LOCAL_MACHINE = unchecked((int)0x80000002);
避免类型不匹配错误,
2)改变:
string[] result = null;
为:
object[] result = null;
避免即将发生的“System.InvalidCastException:无法将类型为'System.Object []'的对象强制转换为'System.String []'”