2

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:

image

Is this possible? If so how?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Agent DM
  • 23
  • 1
  • 3

3 Answers3

6

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.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
Anders
  • 97,548
  • 12
  • 110
  • 164
  • Thanks that seemed to work. It is interesting to note, however, that the "openas" verb is not listed under the reference documentation for any of the ShellExecute functions. – Agent DM Jun 22 '11 at 04:03
  • @Darius: the document at the first link mentions the "openas" verb, though admittedly only in passing. – Harry Johnston May 10 '14 at 01:39
  • I use ShellExecute(Handle, 'openas', PChar(txtFile.Text), nil, nil, SW_SHOWNORMAL) in Delphi but get error. It seems 'openas' is not documented, see this https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea – alancc Oct 06 '22 at 03:04
  • @alancc https://learn.microsoft.com/en-us/previous-versions//bb776820(v=vs.85)?redirectedfrom=MSDN#canonical-verbs – Anders Oct 06 '22 at 09:46
1

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

Stefan
  • 43,293
  • 10
  • 75
  • 117
  • Note that `OpenAs_RunDLL` is undocumented and [does not always work.](http://stackoverflow.com/questions/23566667/rundll32-shell32-dll) – Harry Johnston May 10 '14 at 01:40
0

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.

Alex
  • 5,477
  • 2
  • 36
  • 56
  • I use ShellExecute(Handle, nil, PChar(txtFile.Text), nil, nil, SW_SHOWNORMAL) in Delphi, but still get error. It seems that passing nil will not work if a file has no associated program. It will not invoke the "Open with" dialog – alancc Oct 06 '22 at 03:05