简体   繁体   English

C# WindowsApiCodepack PropertySystem AccessViolationException

[英]C# WindowsApiCodepack PropertySystem AccessViolationException

Doing some explorer/shell stuff on Win8/64bit with WindowsAPICodePack.使用 WindowsAPICodePack 在 Win8/64 位上做一些资源管理器/shell 的事情。 Having some problems with the propertysystem causing an AccessViolationException when iterating over fileproperties with x64 platform target.在使用 x64 平台目标迭代文件属性时,属性系统存在一些问题导致 AccessViolationException。 Seems to be some problem in PropVariant.cs.似乎是 PropVariant.cs 中的一些问题。 Switching to x86 fixes the problems, but causes incomplete directory listings (fe "etc" missing in system32/drivers).切换到 x86 可以解决问题,但会导致目录列表不完整(例如 system32/drivers 中缺少“etc”)。 Any ideas?有任何想法吗?

using System;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

namespace ApiCodepackTest
{
    class Program
    {
        const string path = @"c:\windows\system32\drivers";
        static void Main(string[] args)
        {
            var shellObject = (ShellFolder)ShellObject.FromParsingName(path);
            showProperties(shellObject);
            showItems(shellObject);
            Console.ReadLine();
        }

        static void showProperties(ShellFolder folder)
        {
            var sys = folder.Properties.System;
            foreach (var prop in sys.GetType().GetProperties())
            {
                try
                {
                    var shellProperty = prop.GetValue(sys) as IShellProperty;
                    if (shellProperty != null && shellProperty.ValueAsObject != null)
                        Console.WriteLine(shellProperty.CanonicalName + " " + shellProperty.ValueAsObject);
                }
                catch{} //you should not pass!
            }
        }

        static void showItems(ShellFolder folder)
        {
            foreach (var i in folder)
                Console.WriteLine(i.Name);
        }
    }

I'm not really into pinvoke and c++ stuff, but I've recompiled the source with a little fix in PropVariant.cs : 我不是真的喜欢pinvoke和c ++的东西,但是我在PropVariant.cs中用一点修复重新编译了源代码

//[FieldOffset(12)] original
    [FieldOffset(16)]
    IntPtr _ptr2;

and this fixed the issue 这解决了这个问题

For anyone else finding this question, you can now avoid recompiling the source to achieve this bug fix.对于发现此问题的任何其他人,您现在可以避免重新编译源代码来实现此错误修复。

As discussed in this answer , the open source WindowsAPICodePack is now being maintained by a different developer (Pierre Sprimont).本回答中所述,开源 WindowsAPICodePack 现在由不同的开发人员 (Pierre Sprimont) 维护。 It has been updated as recently as July 2022 and is available on GitHub or as a series of Nuget packages directly through Visual Studio.它最近于 2022 年 7 月更新,可在 GitHub 上获得,或作为一系列 Nuget 包直接通过 Visual Studio 获得。 The developer's website discussing his fork of the project is here , which has links to each Github repo and Nuget page.讨论他的项目分支的开发者网站在这里,它有指向每个 Github 回购和 Nuget 页面的链接。

The AccessViolationException bug in PropVariant.cs discussed in the accepted answer by @Aleksey has been fixed, along with many other bug fixes and improvements (including support for .NET Core).在@Aleksey接受的答案中讨论的PropVariant.cs 中的 AccessViolationException 错误已得到修复,还有许多其他错误修复和改进(包括对 .NET Core 的支持)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM