Click here to Skip to main content
15,897,273 members

Custom File Properties in Win 7

Kyudos asked:

Open original thread
In previous code for Win Xp etc I set some custom file properties as in the code below. I now find in Win7 that this locks my file because the pPropSetStg->Release() fails. But further, on Win 7 there are no custom file properties? Or at least they don't work in the same way. I can't find anything about why this was changed or how to get around it. Does anyone know (or have a link to) the proper Win7 way to do this?


EDIT:

A partial answer... to make the built in custom properties appear for your file type, you neeed to add a registry key as per System Supplied Property Handlers[^].

I added the registry entry to display the four properties I'm after, however, the code below still doesn't work (it won't release, and doesn't set the specified properties).

If I try then to edit the properties manually in Windows Explorer, I get..

"An unexpected error is keeping you from applying properties to the file...

Error 0x80004005: Unspecified Error"



USES_CONVERSION;
WCHAR wcFilename[1024];
IStorage *pStorage = NULL;
IPropertyStorage *pPropStg = NULL;
IPropertySetStorage *pPropSetStg = NULL;
PROPSPEC propspec[4];
PROPVARIANT propvarWrite[4];
HRESULT hr;
IID riid;

const FMTID fmtid = FMTID_SummaryInformation;
const int MAX_BIG_STRING_LEN = 1024;

setlocale(LC_ALL, "");
size_t i = mbstowcs(wcFilename, newName, strlen(newName));
setlocale(LC_ALL, "C");
wcFilename[i] = 0;

riid = IID_IPropertySetStorage;

hr = StgOpenStorageEx(wcFilename, STGM_READ|STGM_SHARE_DENY_WRITE, STGFMT_FILE, 0, NULL, NULL, riid, (void **)(&pStorage));
if (!FAILED(hr) && pStorage != NULL)
{
    hr = pStorage->QueryInterface(riid, (void **)&pPropSetStg);
    if (!FAILED(hr) && pPropSetStg != NULL)
    {
        hr = pPropSetStg->Create(fmtid, NULL, PROPSETFLAG_DEFAULT, STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, &pPropStg);

        if (!FAILED(hr) && pPropStg != NULL)
        {
            propspec[0].ulKind = PRSPEC_PROPID;
            propspec[0].propid = PIDSI_TITLE;
            propvarWrite[0].vt = VT_LPWSTR;
            propvarWrite[0].pwszVal = A2W(MyTitle);

            propspec[1].ulKind = PRSPEC_PROPID;
            propspec[1].propid = PIDSI_SUBJECT;
            propvarWrite[1].vt = VT_LPWSTR;
            propvarWrite[1].pwszVal = A2W(MyDescription);

            propspec[2].ulKind = PRSPEC_PROPID;
            propspec[2].propid = PIDSI_AUTHOR;
            propvarWrite[2].vt = VT_LPWSTR;
            propvarWrite[2].pwszVal = A2W(MyAuthor);

            propspec[3].ulKind = PRSPEC_PROPID;
            propspec[3].propid = PIDSI_COMMENTS;
            propvarWrite[3].vt = VT_LPWSTR;
            propvarWrite[3].pwszVal = A2W(MyComments);

            hr = pPropStg->WriteMultiple(4, propspec, propvarWrite, NULL);
            if (!FAILED(hr))
            {
                hr = pPropStg->Commit(STGC_DEFAULT);
            }

            hr = pPropStg->Release();
            pPropStg = NULL;
        }

        hr = pPropSetStg->Release();
        pPropSetStg = NULL;
    }
}
Tags: Windows, Windows 7, File

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900