如何为未注册的文件扩展名显示"打开方式"对话框?

Tob*_*s R 10 windows delphi winapi

我想让用户为当前未注册的文件扩展名选择一个关联(打开方式).

目前我告诉API使用打开文件ShellExecute,它返回ERROR_NO_ASSOCIATION错误代码.

有没有办法告诉API它应该让用户选择一个新的关联?

Uli*_*rdt 11

我用

procedure ShellOpenAs(const AFileName: string; AHandle: HWND);
begin
  ShellExecute(AHandle, 'open', PChar('rundll32.exe'), PChar('shell32.dll,OpenAs_RunDLL ' + AFileName), nil, SW_SHOWNORMAL);
end;
Run Code Online (Sandbox Code Playgroud)

编辑(灵感来自David的评论和/sf/answers/926066151/):可以省略ShellExecute并直接RunDll32调用OpenAs_RunDLL:

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;
Run Code Online (Sandbox Code Playgroud)

Windows Vista及更高版本上还有SHOpenWithDialog.(我觉得有趣的是,微软编写了一个与RunDLL兼容的入口点,但直到Vista都没有提供常规API函数.)