How to create a property handler for an existing file with a custom file extension to enable me to set its metadata.

Shahid 0 Reputation points
2023-05-10T00:42:00.4633333+00:00

We have files with a custom extension (".xyz") for which I am trying to set metadata like Subject, Company etc. so that it is easily visible in Windows explorer.

I am using the following Windows 7 sample as a basis to register our extension and display the property list items in the Preview Pane and the Details pane of Windows explorer.

https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/Win7Samples/winui/shell/appshellintegration/IdealPropertyHandler

This sample creates the "PropertyHandler.dll" file which needs to be registered with regsvr32.exe in order for the file extension to be registered with windows. After modifying the source code slightly, this is working successfully for our file extension.

When using the "Sample.openmetadata-ms" file provided with the source code, and changing the file extension to ".xyz", I am able to set the metadata from the Windows explorer UI as well as programmatically.

However, when I try changing the "Company" field for example, of one of our existing files, I get the error "0x80030070: There is insufficient disk space to complete operation." even though there is plenty of disk space available.

This leads me to assume that the structure of the sample file's property handler was already "pre-written" to the file to make it possible to change the property store information.

So how do I initialize our existing files so that we can modify the metadata successfully? Some code examples (C# or C++) will be really helpful.

Note: currently using Windows 10

Thanks

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,068 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,622 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,512 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,623 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,628 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,101 Reputation points
    2023-05-18T13:26:26.4366667+00:00
    Hi,
    
    I'd be happy to help you out with your question. Sorry for the inconvenience caused.
    
    To create a property handler, you'll need to implement a shell extension that handles property retrieval and modification. Here are the steps involved:
    
    1) Implement the IPropertyStore interface: This interface allows you to read and write the file's properties. You can create a class that implements this interface and provides the necessary functionality for retrieving and modifying metadata.
    
    2) Register the property handler: You need to register the property handler with the Windows shell, so it associates your custom handler with files having the ".xyz" extension. You can use a registration process similar to the one used in the Windows 7 sample you mentioned.
    
    3) Implement the IInitializeWithFile interface: This interface enables your property handler to receive the file path of the accessed file. You can create a class that implements this interface and initializes the property handler with the file path.
    
    4) Implement property retrieval and modification: In your property handler implementation, you can use the IPropertyStore interface to retrieve and modify the file's properties. You'll need to add logic to handle specific properties like "Company" and perform the necessary operations to read and write the metadata.
    
    Here's an example in C# that demonstrates a basic implementation of a property handler for files with the ".xyz" extension:
    
    // Add necessary using statements
    
    [ComVisible(true)]
    [Guid("YOUR-PROPERTY-HANDLER-GUID")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IPropertyStore))]
    public class XYZPropertyHandler : IPropertyStore, IInitializeWithFile
    {
        private string filePath;
    
        public void Initialize(string pszFilePath, uint grfMode)
        {
            filePath = pszFilePath;
        }
    
        // Implement other IPropertyStore interface methods
        // GetCount, GetAt, GetValue, SetValue, Commit
    }
    
    Please replace "YOUR-PROPERTY-HANDLER-GUID" with a unique GUID for your property handler. You can generate a new GUID using tools like guidgen.exe or an online GUID generator.
    
    Compile the code into a DLL, and then register it with the Windows shell using the appropriate registration process for your Windows version.
    
    After registering the property handler, you should be able to modify the metadata of existing files with the ".xyz" extension through the Windows Explorer UI or programmatically.
    
    Keep in mind that this is a basic example, and you may need to further customize it to handle specific properties and implement the required persistence logic. It's crucial to thoroughly test and validate your code before deploying it in a production environment.
    
    Please note that shell extensions can be complex and have potential risks if not implemented carefully. Ensure you follow best practices and take necessary security precautions.
    
    Please refer to this documentation for more information - https://learn.microsoft.com/windows/win32/properties/property-system-overview
    
    If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help.
     
    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.
    
    1 person found this answer helpful.