Recent

Author Topic: open file  (Read 5631 times)

scons

  • Full Member
  • ***
  • Posts: 141
open file
« on: February 05, 2016, 10:17:19 pm »
ok another problem

I have a ListBox and I want to open the selected file on double click, with the standard windows program.

this is my code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   i: integer;
  4. begin
  5.   for i:=0 to ListBox1.Count -1  do
  6.   if ListBox1.ItemIndex = 1 then
  7.     ShellExecute(Form1.Handle, PChar('open'),PChar(ListBox1.ItemIndex),
  8.          PChar(''), PChar(''), 1);
  9. end;

nothing happens, no error code.

what did I mess up this time ?
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: open file
« Reply #1 on: February 05, 2016, 10:58:04 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   s: UnicodeString;
  5. begin
  6.   i := ListBox1.ItemIndex;
  7.   if i<0 then Exit; // -1 means there is no selected item
  8.   s := ListBox1.Items[i];
  9.  
  10.   ShellExecute(Handle, PWideChar('open'),PWideChar(s),
  11.                PWideChar(''), PWideChar(''), 1);
  12. end;
  13.  
« Last Edit: February 06, 2016, 01:03:32 am by FTurtle »

scons

  • Full Member
  • ***
  • Posts: 141
Re: open file
« Reply #2 on: February 06, 2016, 11:14:55 am »
Your solution gave me an error, but, when I changed it to:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   s: UnicodeString;
  5. begin
  6.   i := ListBox1.ItemIndex;
  7.   if i<0 then Exit; // -1 means there is no selected item
  8.   s := ListBox1.Items[i];
  9.   ShellExecute(Handle, PChar('open'),PChar(s),
  10.                PChar(''), PChar(''), 1);
  11. end;

it works !!

Thank you for pointing me in the right direction FTurtle
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: open file
« Reply #3 on: February 06, 2016, 02:45:47 pm »
Casting a UnicodeString to PChar makes no sense to me?

Bart

balazsszekely

  • Guest
Re: open file
« Reply #4 on: February 06, 2016, 03:40:09 pm »
It should be more like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   I: Integer;
  4.   WS: WideString;
  5. begin
  6.   I := ListBox1.ItemIndex;
  7.   if I < 0 then
  8.     Exit;
  9.   WS := WideString(ListBox1.Items[I]); //or UTF8ToUTF16(ListBox1.Items[I]) from LazUTF8
  10.   ShellExecuteW(Handle, PWideChar('open'), PWideChar(WS), PWideChar(''), PWideChar(''), SW_SHOW);
  11. end;  

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: open file
« Reply #5 on: February 06, 2016, 07:22:52 pm »
Using FPC 3.0+:

Code: Pascal  [Select][+][-]
  1. ShellExecuteW(Handle, PWideChar('open'), PWideChar(WS), PWideChar(''), PWideChar(''), SW_SHOW);

is identical to:
Code: Pascal  [Select][+][-]
  1. ShellExecuteW(Handle, 'open', @WS[1], '', '', SW_SHOW);

and
Code: Pascal  [Select][+][-]
  1. WS := WideString(ListBox1.Items[I]);

is also identical to:
Code: Pascal  [Select][+][-]
  1. WS := ListBox1.Items[I];

except it shows a warning: Implicit string type conversion from "AnsiString" to "WideString"

balazsszekely

  • Guest
Re: open file
« Reply #6 on: February 06, 2016, 07:49:38 pm »
@engkin

Unfortunately, it has been proved many times before that the new "improved unicode support" sometimes will fail, or at least will not work as it should. I'm not saying in this particular case you're not right, but personally I prefer to do the conversion manually.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: open file
« Reply #7 on: February 07, 2016, 10:29:28 am »
Unfortunately, it has been proved many times before that the new "improved unicode support" sometimes will fail, or at least will not work as it should. I'm not saying in this particular case you're not right, but personally I prefer to do the conversion manually.

When the dynamic encoding of a string is correct then the automatic conversion should work always. Otherwise FPC 3 has a bug which I don't know about.
I would like to know the cases where it does not work and then at least document them.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

scons

  • Full Member
  • ***
  • Posts: 141
Re: open file
« Reply #8 on: February 07, 2016, 11:45:45 am »
I can confirm this solution works also without any errors.

It should be more like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. var
  3.   I: Integer;
  4.   WS: WideString;
  5. begin
  6.   I := ListBox1.ItemIndex;
  7.   if I < 0 then
  8.     Exit;
  9.   WS := WideString(ListBox1.Items[I]); //or UTF8ToUTF16(ListBox1.Items[I]) from LazUTF8
  10.   ShellExecuteW(Handle, PWideChar('open'), PWideChar(WS), PWideChar(''), PWideChar(''), SW_SHOW);
  11. end;  
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

scons

  • Full Member
  • ***
  • Posts: 141
Re: open file
« Reply #9 on: February 07, 2016, 11:49:16 am »
This solution also works, with the warning.

Using FPC 3.0+:

Code: Pascal  [Select][+][-]
  1. ShellExecuteW(Handle, PWideChar('open'), PWideChar(WS), PWideChar(''), PWideChar(''), SW_SHOW);

is identical to:
Code: Pascal  [Select][+][-]
  1. ShellExecuteW(Handle, 'open', @WS[1], '', '', SW_SHOW);

and
Code: Pascal  [Select][+][-]
  1. WS := WideString(ListBox1.Items[I]);

is also identical to:
Code: Pascal  [Select][+][-]
  1. WS := ListBox1.Items[I];

except it shows a warning: Implicit string type conversion from "AnsiString" to "WideString"
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: open file
« Reply #10 on: February 07, 2016, 12:51:51 pm »
I can confirm this solution works also without any errors.
Code: Pascal  [Select][+][-]
  1.   WS := WideString(ListBox1.Items[I]); //or UTF8ToUTF16(ListBox1.Items[I]) from LazUTF8

If you need a typecast there then FPC has a bug.

In fact the whole ShellExecute discussion is useless. Instead of ShellExecute you should use the cross-platform
Code: Pascal  [Select][+][-]
  1. function OpenDocument(APath: String): Boolean;
defined in LCLIntf.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018