Search code examples
c++windowsshellexecute

Specific Properties Tab using ShellExecute Properties Verb


Is there any way to open to a specific tab in properties using ShellExecute's Properties Verb?

Is there any way to do it at all? (Doesn't have to be ShellExecute, but can't find much that will display a file's "properties window")

(Mimicking the behavior of Right-clicking a file, selecting properties, and clicking on the Details tab)


Solution

  • I was able to find a solution. The following answers written in C# were very helpful:

    Here is some code:

    // initalize COM
    HRESULT  coInitRes = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    SHELLEXECUTEINFO ShExecInfo = {}; // initialize empty structure
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST;
    ShExecInfo.lpVerb = "properties";
    ShExecInfo.lpFile = "C:\\Users";
    ShExecInfo.lpParameters = "Security";
    // ShExecInfo.lpParameters = "Tools"; // if you want to open another tab
    ShExecInfo.nShow = SW_SHOW;
    boolean result = ShellExecuteEx(&ShExecInfo);
    if (!result) {
        int res = GetLastError(); // this retrieves the error code
        // int res = (int) ShExecInfo.hInstApp; // this retrieves another error code
                                // on success, this value is 32
    }
    
    // close COM
    if (coInitRes == S_OK || coInitRes == S_FALSE) { // do not call when result is RPC_E_CHANGED_MODE
        CoUninitialize();
    }
    

    I'm not sure if you really have to call CoInitializeEx and CoUninitialize, but the docs of ShellExecuteEx state that it is good practice.

    This example opens the Security tab of the properties dialog. You can change the value of ShExecInfo.lpParameters to specify another tab, for example Tools.

    Note that if your application uses Unicode, you have to prepend an L to the string, for example:

    ShExecInfo.lpFile = L"C:\\Users";