How to open a default dialog for window if ShellEx

2019-01-26 23:12发布

问题:

I can use the windows ShellExecute function to open a file with no problems so long as the file has a correct association.

If no association exists i would like to use the default windows dialog to open the file:

Is this possible? If so how?

回答1:

The documented way to show that dialog is to use the openas verb.

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOASYNC;
sei.nShow = SW_SHOWNORMAL;
sei.lpVerb = "openas";
sei.lpFile = "C:\\yourfile.ext";
ShellExecuteEx(&sei);

If you check under HKEY_CLASSES_ROOT\Unknown\shell\openas you see that this is the same as calling the (undocumented) OpenAs_RunDLL export in shell32.



回答2:

Execute RUNDLL32 Shell32,OpenAs_RunDLL path/to/file/to/open



回答3:

Simply do not use explicit verb. Using a specific verb like 'open' is a big mistake:

  • 'open' may be not a default verb (for example, it may be 'play', 'edit' or 'run')
  • 'open' may not exists

It is a way more correct to simply pass NULL as verb. The system will automatically select most appropriate verb:

  • Default verb will be used, if it is set
  • 'open' verb will be used, if no default verb is set
  • first verb will be used, if no default and 'open' verbs are available
  • if no verbs are assigned - the system will bring "Open with" dialog

In other words, simple

ShellExecute(0, NULL, 'C:\MyFile.StrangeExt', ...);

will show "Open with" dialog.

Only use a specific verb if you want a specific action. E.g. 'print', 'explore', 'runas'. Otherwise - just pass nil.



回答4:

go with foolowing code you will get your solution-


public const uint SEE_MASK_INVOKEIDLIST = 12;//add this line in your code


CoInitializeEx(NULL, COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.nShow = SW_SHOWNORMAL;
sei.lpVerb = "openas";
sei.lpFile = "C:\\yourfile.ext";
sei.lfmask= SEE_MASK_INVOKEIDLIST;//add this line in your code
ShellExecuteEx(&sei);

SEE_MASK_INVOKEIDLIST this variable set "Verb" from presented system registry.