-->

How to get command line of windows “open with ..”

2019-08-28 20:24发布

问题:

I previously asked a question that is about how to get windows "open with.." application list. Here's a link to that question.

We can use SHAssocEnumHandlers interface to get the file association with specific file extension, ex .png

Then use IAssocHandler and can retrieves the full path and file name of the executable file associated with the file type(.png). ex:['Paint': 'C:\\Windows\\system32\\mspaint.exe', ...]

But I want to get the command line of executing mspaint.exe with a given image. Like this~ "%systemroot%\system32\mspaint.exe" "%1"

Is there another msdn api could help us to get the "open with.." command? I think it should have, since windows XP already have this ability.

回答1:

Use AssocQueryString(..., ASSOCSTR_COMMAND, ...);

Example:

TCHAR commandline[1024];
DWORD size = ARRAYSIZE(commandline);
AssocQueryString(0, ASSOCSTR_COMMAND, _T(".txt"), 0, commandline, &size);


回答2:

There is the SHOpenWithDialog function.

Link to SHOpenWithDialog on MSDN

However, you can't use this to retrieve the selected program. You can only use it to invoke the "Open With" behaviour and eventually open the file (if OAIF_EXEC is set). If that's all you're interested in, then try it out:

#include <windows.h>
#include <Shlobj.h>

#pragma comment(lib, "Shell32.lib")

int main()
{
    OPENASINFO info = { 0 };
    info.pcszFile = L"C:\\Temp\\SomeFile.png";
    info.pcszClass = NULL;
    info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
    SHOpenWithDialog(NULL, &info);
    return 0;
}


标签: c++ windows msdn