We provide Flash tutorial videos that install on the local (Windows) hard disk with our application. Our app uses ShellExecute to open the html file (in whatever browser is associated with html files) in which they are embedded.
Apparently there's a bug in Chrome's more recent Flash players that fails to play local files (but files over the web are fine.)
(Frankly, I'm astonished that this bug hasn't been fixed by Google. Seems like a big one to me... but maybe not many people play Flash from locations other than the web?)
There's a work-around on the about:plugins screen in Chrome, but we can't ask our users to do that. Here's a discussion of the work-around: http://techsmith.custhelp.com/app/answers/detail/a_id/3518
I want to provide my users with an option to open our html files IE. If Chrome is their default browser, then I'd show a checkbox that says something embarrassing like "If our tutorial videos fail to play, check this box to try them in IE."
Is this XE2 code (from two years ago on SO: link) still reasonable?
if pos('CHROME', UpperCase(GetAssociation('C:\Path\File.html')) > 0 then
// Chrome is the default browser
function GetAssociation(const DocFileName: string): string;
var
FileClass: string;
Reg: TRegistry;
begin
Result := '';
Reg := TRegistry.Create(KEY_EXECUTE);
Reg.RootKey := HKEY_CLASSES_ROOT;
FileClass := '';
if Reg.OpenKeyReadOnly(ExtractFileExt(DocFileName)) then
begin
FileClass := Reg.ReadString('');
Reg.CloseKey;
end;
if FileClass <> '' then begin
if Reg.OpenKeyReadOnly(FileClass + '\Shell\Open\Command') then
begin
Result := Reg.ReadString('');
Reg.CloseKey;
end;
end;
Reg.Free;
end;
If you have an actual full path to an existing file on disk, you can use FindExecutable instead. It's easier, and doesn't require access to the registry, but it does require that an actual file exists.
Here's a console app for XE2 that demonstrates use:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, ShellAPI, Windows;
var
Buffer: array[0..MAX_PATH] of Char;
Res: Integer;
begin
FillChar(Buffer, SizeOf(Buffer), #0);
Res := FindExecutable(PChar('C:\Path\File.html'), nil, Buffer);
if Res > 32 then
Writeln('Executable is ' + Buffer)
else
WriteLn(SysErrorMessage(Res));
Readln;
end.
The method you show will work, but FindExecutable
is easier (less code) and works on XP and above.
来源:https://stackoverflow.com/questions/11962753/detect-chrome-as-browser-associated-with-html-files-in-windows