0

我在 Silverlight 5 中有一个签名的、受信任的应用程序(并且知道它工作正常并且启用了 AutomationFactory)。但是,无论我做什么,当我尝试查询注册表时,都会收到类型不匹配的 COM 异常,这无济于事。视窗 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);
4

1 回答 1

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 []'类型”

于 2013-05-08T15:08:39.417 回答