5

Doing some explorer/shell stuff on Win8/64bit with WindowsAPICodePack. Having some problems with the propertysystem causing an AccessViolationException when iterating over fileproperties with x64 platform target. Seems to be some problem in PropVariant.cs. Switching to x86 fixes the problems, but causes incomplete directory listings (f.e. "etc" missing in system32/drivers). 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);
        }
    }
g.lupsch
  • 53
  • 6

2 Answers2

4

I'm not really into pinvoke and c++ stuff, but I've recompiled the source with a little fix in PropVariant.cs :

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

and this fixed the issue

Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
  • 1
    Great job finding the source of this awful bug (which was making my app crash). As I discuss in [my supplemental answer](https://stackoverflow.com/a/75012078/20036274), the WindowsAPICodePack is now being maintained by a new developer who also fixed this bug. So I was able to simply switch my Nuget packages to his version to take advantage of the bug-fix, without having to recompile from scratch. – wopr_xl Jan 04 '23 at 22:58
0

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). 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. The developer's website discussing his fork of the project is here, which has links to each Github repo and Nuget page.

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).

wopr_xl
  • 150
  • 1
  • 9