I want to let the user select an association (open with) for an currently unregistered file extension.
Currently I'm telling the API to open the file by using ShellExecute
and it returns an ERROR_NO_ASSOCIATION
error code.
Is there a way to tell the API that it should let the user select a new association?
Go with following 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.
I use
procedure ShellOpenAs(const AFileName: string; AHandle: HWND);
begin
ShellExecute(AHandle, 'open', PChar('rundll32.exe'), PChar('shell32.dll,OpenAs_RunDLL ' + AFileName), nil, SW_SHOWNORMAL);
end;
Edit (inspired by David's comment and https://stackoverflow.com/a/13229516/1431618):
One can omit ShellExecute
and RunDll32
by calling OpenAs_RunDLL
directly:
procedure OpenAs_RunDLL(hwnd: HWND; hinst: HINST; lpszCmdLine: LPSTR; nCmdShow: Integer); stdcall; external shell32;
procedure ShellOpenAs(AHandle: HWND; const AFileName: string);
begin
OpenAs_RunDLL(AHandle, HInstance, PChar(AFileName), SW_SHOWNORMAL);
end;
There is also SHOpenWithDialog on Windows Vista and later. (I find it interesting that Microsoft wrote a RunDLL compatible entry point but until Vista didn't bother to provide a regular API function.)
Simply do not use explicit verb. Using a specific verb like 'open' is a big mistake:
It is a way more correct to simply pass nil as verb. The system will automatically select most appropriate verb:
In other words, simple
ShellExecute(0, nil, '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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With