views:

625

answers:

2

Has anyone managed to do this? I tried making a managed wrapper class for IPropertyStore but am getting AccessViolationExceptions on the methods (i.e. IPropertyStore::GetValue) that take a pointer to PROPVARIANT (rendered as a MarshalAs(UnmanagedType.Struct) out parameter in my managed version) Probably my understanding of COM and interop is inadequate --- I'm not sure if the problems are in my PROPVARIANT struct declaration (which currently just uses StructLayout.Sequential, declares a sequence of bytes, and manually manipulates the bytes to get values of the various types in the union etc.), COM issues with what process owns what, or something else. I've tried various other versions of the PROPVARIANT such as using StructLayout.Explicit for the unions, nothing's worked. Retrieving PROPERTYKEYs with IPropertyStore::GetAt --- which is declared natively as taking a pointer to PROPERTYKEY and as having an out parameter of my own StructLayout.Sequential PROPERTYKEY in my wrapper --- works just fine, by the way.

+1  A: 

Well, here's the version from MS.Internal.Interop (a trove of knowledge):

[StructLayout(LayoutKind.Sequential), FriendAccessAllowed]
internal struct PROPVARIANT
{
    internal VARTYPE vt;
    internal ushort wReserved1;
    internal ushort wReserved2;
    internal ushort wReserved3;
    internal PropVariantUnion union;
}

[FriendAccessAllowed]
internal enum VARTYPE : short
{
    VT_BSTR = 8,
    VT_FILETIME = 0x40,
    VT_LPSTR = 30,
    // etc...
}


[StructLayout(LayoutKind.Explicit), FriendAccessAllowed]
internal struct PropVariantUnion
{
    [FieldOffset(0)]
    internal BLOB blob;
    [FieldOffset(0)]
    internal short boolVal;
    // etc... see MS.Internal.Interop for full definition
}

These definitions will help you make sure your structures are at least correct. As for your other problems, I don't have an answer.

Frank Krueger
+1  A: 

You should check out http://code.msdn.microsoft.com/WindowsAPICodePack . It has support for consuming the Windows Property System, and a bunch of other windows shell capabilities. I think it's exactly what you are looking for.

Ben Karas
Unfortunately this didn't exist when I originally asked, but that's definitely the way to go now.
Max Strini

related questions