0
votes

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.

1
Please try with a Sleep after your call to ShellExecuteEx. stackoverflow.com/a/33472984/4181011 - Simon Kraemer
Dude, that worked! Thank you very much. - Dominus

1 Answers

0
votes

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.