0

I am trying to write a program, that opens the properties of a file, from the command-line. I read that it is possible to do, using either of the functions ShellExecute and ShellExecuteEx, with the 'properties' verb.

So, I wrote such a C++ program in Visual Studio for Windows 10. This is that program:

#include <windows.h>
#include <iostream>
void ShowFileProperties(char *);
int main(int argc, char **argv)
{
    if (argc >= 2)
    {
        std::cout << argv[1] << std::endl;
        ShowFileProperties(argv[1]);
    }
    std::cout << GetLastError();
    return 0;
}

void ShowFileProperties(char *szPathName)
{
    HRESULT result = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    SHELLEXECUTEINFO Sei;
    ZeroMemory(&Sei,sizeof(SHELLEXECUTEINFO));
    Sei.cbSize = sizeof(SHELLEXECUTEINFO);
    Sei.lpFile = szPathName;
    Sei.nShow = SW_SHOW;
    Sei.fMask = SEE_MASK_INVOKEIDLIST;
    Sei.lpVerb = "properties";
    ShellExecuteEx(&Sei);
    if (result == S_OK || result == S_FALSE)
        CoUninitialize();
}

If I run the program from the command line with a valid filename (such as . or the name of the executable itself), all it outputs is the filename and a zero (there was no error), but the properties of the file don't open.

Now, I have seen that other people have this problem, i.e. that the 'properties' verb doesn't do anything or that they get a messagebox saying that the filetype doesn't have an associated program for the operation, but I have not been able to find a fix.

Is there anyone here that can help me?

Thanks in advance.

Dominus
  • 63
  • 8

1 Answers1

0

Pass the SEE_MASK_NOASYNC (0x00000100) flag in your SHELLEXECUTEINFO to tell ShellExecuteEx that you're calling it without a message loop and not to return until it has finished.

See the remarks in the SHELLEXECUTEINFO docs on MSDN.

Sleep() is neither necessary nor recommended.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • 1
    I tried that now, but it didn't work. I need to call Sleep(). – Dominus Nov 13 '16 at 13:46
  • I agree that Sleep is not a good approach. But your suggestion to use SEE_MASK_NOASYNC doesn't work either, at least not on my Win10. Just like I [commented here](https://stackoverflow.com/a/33472984/670017), if I add SEE_MASK_NOASYNC to SEE_MASK_INVOKEIDLIST, the `ShellExecuteEx` returns TRUE immediately and then later I get an error MessageBox with `"The properties for this item are not available."` Only calling it w/o SEE_MASK_NOASYNC solves the issue, but that may be just a timing issue. TBH, I hate `ShellExecute` and how convoluted it is! – c00000fd Feb 02 '18 at 21:01