C# WindowsApiCodepack PropertySystem AccessViolationException

标签 c# windows-api-code-pack

使用 WindowsAPICodePack 在 Win8/64 位上做一些资源管理器/shell 的事情。在使用 x64 平台目标迭代文件属性时,属性系统存在一些问题导致 AccessViolationException。似乎是 PropVariant.cs 中的一些问题。切换到 x86 可以解决问题,但会导致目录列表不完整(例如 system32/drivers 中缺少“etc”)。有什么想法吗?

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);
        }
    }

最佳答案

我不太喜欢 pinvoke 和 C++ 的东西,但我已经重新编译了 source在 PropVariant.cs 中进行一些修复:

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

这解决了问题

关于C# WindowsApiCodepack PropertySystem AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28076941/

相关文章:

c++ - 微软的 CopyFileEx 错误

使用 kernal32 VirtualQueryEx 的 Java JNA 返回零(无成功错误代码)

C# 3.0 使用 MemoryStream 将 itextsharp pdf 保存到数据库

C# JSON反序列化异常

c# - 解决继承问题

c# - 如何获取文件属性?

c# - 无窗口任务栏按钮

c# - 如何比较两个不同的图像,通过tcp发送差异,然后将差异与客户端上的图像合并?

c# - 录音波 naudio 库 - 声音质量很差