The details tab in the properties window is populated with metadata property handlers. The metadata property system is something Microsoft introduced with Windows Vista and it was made open and extensible, enabling independent developers (like Solidworks) to implement and support their own file properties. Very roughly, the flow of execution is something like this:
User clicks file properties
Look up property handler for the file format
If found property handler:
Query property handler for properties
Populate file details with queried properties
Else:
Populate file details with generic file info
Property handlers are COM objects. COM (Component Object Model) is Microsoft's attempt at a language-independent object oriented framework whose origins go all the way back to the nineties, but for the purposes of this explanation suffice it to say that a COM object is a C++ class that implements the IUnknown
interface. A property handler has to implement the IPropertyStore
interface on top of that:
struct IPropertyStore : public IUnknown
{
public:
virtual HRESULT GetCount(
DWORD *cProps) = 0;
virtual HRESULT GetAt(
DWORD iProp,
PROPERTYKEY *pkey) = 0;
virtual HRESULT GetValue(
REFPROPERTYKEY key,
PROPVARIANT *pv) = 0;
virtual HRESULT SetValue(
REFPROPERTYKEY key,
REFPROPVARIANT propvar) = 0;
virtual HRESULT Commit( void) = 0;
};
A convenience implementation of this interface is CLSID_InMemoryPropertyStore
provided to developers to ease their own implementation of IPropertyStore
. The interesting methods here are GetValue
and SetValue
. Properties are assigned a unique GUID, which the PROPERTYKEY
structure passed into these functions contains to identify the property. The implementations details for GetValue
and SetValue
are left to the developer, so it is up to the developer how and where to store the value for each property -- these values could be stored in another file, in an alternate file stream, or in the registry to name a few options -- but for transportability reasons it is recommended to store the values in the file itself. This way, if the file is zipped up and emailed, for example, the properties go with it.
The property handler COM object is compiled into a DLL and registered with the system with regsvr32
. This allows Windows to know where to go look for properties for that specific file format. Once registered, the property handler can be obtained in a number of ways, one of which is the convenience function SHGetPropertyStoreFromParsingName
:
HRESULT GetPropertyStore(PCWSTR pszFilename, GETPROPERTYSTOREFLAGS gpsFlags, IPropertyStore** ppps)
{
WCHAR szExpanded[MAX_PATH];
HRESULT hr = ExpandEnvironmentStrings(pszFilename, szExpanded, ARRAYSIZE(szExpanded)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
if (SUCCEEDED(hr))
{
WCHAR szAbsPath[MAX_PATH];
hr = _wfullpath(szAbsPath, szExpanded, ARRAYSIZE(szAbsPath)) ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
hr = SHGetPropertyStoreFromParsingName(szAbsPath, NULL, gpsFlags, IID_PPV_ARGS(ppps));
}
}
return hr;
}
Once obtained, GetValue
and SetValue
can be invoked on the IPropertyStore
object to either obtain, change or set new value for a property. If using SetValue
though, make sure to invoke Commit
as well.
Microsoft provides a utility, called PropertyEdit
, to get and set metadata properties on a file as part of their Windows Classic Samples. It's a shame they don't mention it anywhere in their help pages. Since you already have Solidworks installed, the property handler for the file formats you're interested in should already be registered on the system and it should be a matter of compiling PropertyEdit
and using it to get and set the metadata properties the handler supports. It's a simple command line utility.
If you need, or want to support custom metadata for your own file format, there is a full-blown sample property handler as well: RecipePropertyHandler
.
For reference, to set a property by its canonical name:
HRESULT GetPropertyStore(PCWSTR pszFilename, GETPROPERTYSTOREFLAGS gpsFlags, IPropertyStore** ppps)
{
WCHAR szExpanded[MAX_PATH];
HRESULT hr = ExpandEnvironmentStrings(pszFilename, szExpanded, ARRAYSIZE(szExpanded)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
if (SUCCEEDED(hr))
{
WCHAR szAbsPath[MAX_PATH];
hr = _wfullpath(szAbsPath, szExpanded, ARRAYSIZE(szAbsPath)) ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
hr = SHGetPropertyStoreFromParsingName(szAbsPath, NULL, gpsFlags, IID_PPV_ARGS(ppps));
}
}
return hr;
}
HRESULT SetPropertyValue(PCWSTR pszFilename, PCWSTR pszCanonicalName, PCWSTR pszValue)
{
// Convert the Canonical name of the property to PROPERTYKEY
PROPERTYKEY key;
HRESULT hr = PSGetPropertyKeyFromName(pszCanonicalName, &key);
if (SUCCEEDED(hr))
{
IPropertyStore* pps = NULL;
// Call the helper to get the property store for the
// initialized item
hr = GetPropertyStore(pszFilename, GPS_READWRITE, &pps);
if (SUCCEEDED(hr))
{
PROPVARIANT propvarValue = {0};
hr = InitPropVariantFromString(pszValue, &propvarValue);
if (SUCCEEDED(hr))
{
hr = PSCoerceToCanonicalValue(key, &propvarValue);
if (SUCCEEDED(hr))
{
// Set the value to the property store of the item.
hr = pps->SetValue(key, propvarValue);
if (SUCCEEDED(hr))
{
// Commit does the actual writing back to the file stream.
hr = pps->Commit();
if (SUCCEEDED(hr))
{
wprintf(L"Property %s value %s written successfully \n", pszCanonicalName, pszValue);
}
else
{
wprintf(L"Error %x: Commit to the propertystore failed.\n", hr);
}
}
else
{
wprintf(L"Error %x: Set value to the propertystore failed.\n", hr);
}
}
PropVariantClear(&propvarValue);
}
pps->Release();
}
else
{
wprintf(L"Error %x: getting the propertystore for the item.\n", hr);
}
}
else
{
wprintf(L"Invalid property specified: %s\n", pszCanonicalName);
}
return hr;
}