[Lazarus] Opening a file with spaces in the filename (again)

Reinier Olislagers reinierolislagers at gmail.com
Wed Mar 6 08:45:01 CET 2013


On 6-3-2013 0:43, Alejandro Gonzalo wrote:
> If you only will be using Windows, you can use:
>  
>     ShellExecute(HWND(nil),nil,PChar('myFile.pdf'),PChar(''),nil ,1);
>  
> You will need ShellAPI in the uses clause.
>  
OpenDocument calls OpenURL which calls ShellExecute (Laz trunk version
below):

// Open a given URL with the default browser
function OpenURL(AURL: String): Boolean;
var
{$IFDEF WinCE}
  Info: SHELLEXECUTEINFO;
{$ELSE}
  ws: WideString;
  ans: AnsiString;
  IsFileUriWithSpaces, IsQuoted: Boolean;
const
  FileURIScheme = 'file://';
{$ENDIF}
begin
  Result := False;
  if AURL = '' then Exit;
  {$IFDEF WinCE}
  FillChar(Info, SizeOf(Info), 0);
  Info.cbSize := SizeOf(Info);
  Info.fMask := SEE_MASK_FLAG_NO_UI;
  Info.lpVerb := 'open';
  Info.lpFile := PWideChar(UTF8Decode(AURL));
  Result := ShellExecuteEx(@Info);
  {$ELSE}
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    //Urls that start with file:// are allowed to contain spaces and
should be quoted on NT platform,
    //but on Win9x quoting it fails
    //Since on Windows filenames cannot contain the " character, we need
not care about it and simply enclose the AURL
    IsFileUriWithSpaces := (Pos(#32,AURL) > 0) and
(CompareText(Copy(AURL,1,Length(FileURIScheme)), FileURIScheme) = 0);
    IsQuoted := (Length(AURL) > 1) and (AURL[1] = '"') and
(AURL[Length(AURL)] = '"');
    if IsFileUriWithSpaces and not IsQuoted then AURL := '"' + AURL + '"';
    ws := UTF8Decode(AURL);
    Result := ShellExecuteW(0, nil, PWideChar(ws), nil, nil,
SW_SHOWNORMAL) > 32;
  end
  else
  begin
    ans := Utf8ToAnsi(AURL); // utf8 must be converted to Windows
Ansi-codepage
    Result := ShellExecute(0, nil, PAnsiChar(ans), nil, nil,
SW_SHOWNORMAL) > 32;
  end;
  {$ENDIF}
end;







More information about the Lazarus mailing list