简体   繁体   English

查询WMI类Win32_ShadowCopy时初始化失败

[英]Initialization failure when querying WMI-class Win32_ShadowCopy

while programming throughout the years I have never posted a question on this website, but I have encounted numerous of problems that had been addressed here before. 多年来,在进行编程时,我从未在该网站上发布过任何问题,但是我遇到了之前在此已解决的许多问题。 Now, however, I encountered a problem that I can't seem to find an answer to. 但是现在,我遇到了一个似乎找不到答案的问题。

I am creating an application in which I need information from the ShadowCopies on the system. 我正在创建一个应用程序,在该应用程序中我需要来自系统上的ShadowCopies的信息。 I am trying to achieve this by using WMI (in C#). 我正在尝试通过使用WMI(在C#中)实现这一目标。 This is however giving me an "Initialization Failure"-exception. 但是,这给了我一个“初始化失败”的例外。 Here's the code: ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2"); 这是代码:ManagementScope scope = new ManagementScope(“ \\\\。\\ ROOT \\ cimv2”);

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ShadowCopy");

//create object searcher
ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(scope, query);

//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection) 
{
// access properties of the WMI object
  Console.WriteLine("ClientAccessible : {0}", m["ClientAccessible"]);

}

Whenever the foreach line is reached, a ManagementException is thrown with the message "Initialization Failure". 每当到达foreach行时,都会引发ManagementException并显示消息“ Initialization Failure”。 I have absolutely no clue why this is happening. 我绝对不知道为什么会这样。 If I use the exact same code and change the WMI-class (to Win32_Processor/Win32_LogicalDisk/...) I am not getting this exception and the foreach-loop just works. 如果我使用完全相同的代码并更改WMI类(更改为Win32_Processor / Win32_LogicalDisk / ...),则不会收到此异常,并且foreach循环可以正常工作。 I also noticed that the exception comes forth from the statement "searcher.Get();". 我还注意到,异常来自语句“ searcher.Get();”。 I have tried this code on a Windows Server 2008-machine as well as on a Windows 7 Enterprise-machine, both generating the same exception. 我已经在Windows Server 2008计算机和Windows 7 Enterprise计算机上尝试了此代码,两者均生成相同的异常。

I have also tried using this class in a vb-script and that worked. 我也尝试过在vb脚本中使用此类,并且效果很好。 Code of VBS: VBS代码:

Set objWMIService = GetObject("winmgmts:\\" + ComputerName + "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ShadowCopy")

For Each objItem in colItems
    Wscript.Echo objItem.ClientAccessible
Next

I am really clueless on what is generating this exception, especially since the WMI-class is working when I use a VB-script. 我对产生此异常的原因一无所知,尤其是当我使用VB脚本时WMI类正在工作。 Does anybody see what I am doing wrong here? 有人在这里看到我在做什么错吗? Any help is appreciated and if you need more information to resolve this issue, just let me know! 感谢您的帮助,如果您需要更多信息来解决此问题,请告诉我!

Greetz, Simon 西蒙·格蕾兹

//NOTE: I got this code from http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/Win32_ShadowCopy/cs-samples.html //注意:我从http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/Win32_ShadowCopy/cs-samples.html获取了此代码

Assuming you are running a 64-bit version of Windows, in Project Properties, un-check "Prefer 32-bit" under General. 假设您正在运行Windows的64位版本,请在“项目属性”中,取消选中“常规”下的“首选32位”。 I found that this, in addition to running Visual Studio as Administrator, was necessary to prevent the "Initialization Failure" exception being thrown. 我发现,除了以管理员身份运行Visual Studio外,这是防止抛出“初始化失败”异常所必需的。 I believe you cannot call the WMI method from a 32-bit application on 64-bit platforms. 我相信您不能从64位平台上的32位应用程序调用WMI方法。

I had the same issue with a script I wrote that performs live WIM backups. 我编写的执行WIM实时备份的脚本存在相同的问题。 Is the system you're running this on have UAC? 您正在其上运行的系统是否具有UAC? If so, try either turning it off or running the app as Administrator - as soon as I did that it sprung to life. 如果是这样,请尝试将其关闭或以管理员身份运行该应用程序-当我这样做时,它应运而生。

Hope this helps 希望这可以帮助

Working on Windows Server 2008 R2, I had the same problem. 在Windows Server 2008 R2上工作时,我遇到了同样的问题。

Here is what I discovered: 这是我发现的:

mshta.exe exists in 2 versions in my system: mshta.exe在我的系统中存在2个版本:
- C:\\Windows\\System32\\mshta.exe (64 bits) -C:\\ Windows \\ System32 \\ mshta.exe(64位)
- C:\\Windows\\SysWOW64\\mshta.exe (32 bits) -C:\\ Windows \\ SysWOW64 \\ mshta.exe(32位)

In registry, the application associated with hta files (HKCR\\htafile\\Shell\\Open\\Command) is C:\\Windows\\SysWOW64\\mshta.exe, with which the Win32_ShadowCopy query does not work. 在注册表中,与hta文件(HKCR \\ htafile \\ Shell \\ Open \\ Command)关联的应用程序为C:\\ Windows \\ SysWOW64 \\ mshta.exe,Win32_ShadowCopy查询无法使用该程序。
On the other hand, the hta file containing the query works as expected when run with C:\\Windows\\System32\\mshta.exe. 另一方面,使用C:\\ Windows \\ System32 \\ mshta.exe运行时,包含查询的hta文件将按预期工作。

In a word : run hta applications containing a Win32_ShadowCopy wmi query with the 64 bit version of mshta.exe. 一言以蔽之:运行包含Win32_ShadowCopy wmi查询和mshta.exe 64位版本的hta应用程序。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用WQL从WMI查询Win32_NTLogEvent类时,TimeGenerated属性是否基于计算机或GMT的本地时间? - When querying the Win32_NTLogEvent Class from WMI with WQL is the TimeGenerated property based on Local time of the computer or GMT? 为什么“Win32_CDROMDrive”类的“SerialNumber”属性在与WMI分开请求时返回null? - How come the “SerialNumber” property of the “Win32_CDROMDrive” class returns null when asking for it separately from WMI? Win32_ComputerSystem WMI类的SystemType属性的值奇怪吗? - Strange values of SystemType property of Win32_ComputerSystem WMI class? WMI Win32_PageFileSetting - WMI Win32_PageFileSetting WMI:查询类的特定实例 - WMI: Querying a specific instance of a class WPF WMI Win32_Service查询 - Wpf WMI Win32_Service Query 快速替换Win32_NetworkAdapter WMI类以获取本地计算机的MAC地址 - Fast replacement for Win32_NetworkAdapter WMI class for getting MAC address of local computer 有没有人成功使用过WMI Win32_PatchPackage类? (C#) - Has anyone successfully used the WMI Win32_PatchPackage class? (C#) 如何通过 WMI 从“Win32_ProcessStopTrace”类获取附加信息? - How to obtain additional info from the 'Win32_ProcessStopTrace' class through WMI? 在Server 2008 SP2上对Win32_PageFileUsage类运行WMI查询时出现内存泄漏 - Memory leak while running WMI query to Win32_PageFileUsage class on Server 2008 SP2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM