Recent

Author Topic: Trying to get My Documents folder  (Read 12766 times)

GetMem

  • Hero Member
  • *****
  • Posts: 3519
Re: Trying to get My Documents folder
« Reply #15 on: July 19, 2017, 10:23:35 am »
@rvk
According to @Josh SHGetKnownFolderPathW also fails, see a few post above.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: Trying to get My Documents folder
« Reply #16 on: July 19, 2017, 10:25:12 am »
@rvk
According to @Josh SHGetKnownFolderPathW also fails, see a few post above.
Really? Can you quote that part? I can't find it.
Edit: Woops, I found it.
GetWindowsSpecialDir doesn't work either.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3657
  • I like bugs.
Re: Trying to get My Documents folder
« Reply #17 on: July 19, 2017, 10:42:27 am »
The problem is that it is not even getting to the systoutf8 function, the function SHGetSpecialFolderPathW(0, SpecialPath, CSIDL, False) is returning FALSE, and the error being reported is 'The System Cannot find the path specified'.
Why don't you just test with the working function provided by GetMem? Why must you modify it first?
[Edit] Ok, I saw you had tried it as-is first. Why the WinAPI function returns FALSE is a mystery then.

Just to be sure: You use default settings in Lazarus, right?
It is possible to disable the default Unicode support by -dDisableUTF8RTL as explained here:
 http://wiki.freepascal.org/Lazarus_with_FPC3.0_without_UTF-8_mode
... although even it should not affect the value returned from WinAPI.

Quote
I have tried using WinCPToUTF8 but this has no change in that the error is prior to this.
Uhhh, no! For some reason you fail to understand the fundamentals.
With the current Unicode support in Lazarus you practically never need explicit conversion functions.
No SysToUTF8, no WinCPToUTF8, no AnsiToUTF8, nothing.
FPC 3.0+ has dynamic string encodings. Data is converted automatically when it is assigned.
The only exception is when you really must use Windows system codepages, but in your case it is not necessary.
When you use only Unicode, things go smoothly.

wp and others, please don't recommend system codepages to him or anybody else.
« Last Edit: July 19, 2017, 10:55:10 am by JuhaManninen »

josh

  • Hero Member
  • *****
  • Posts: 754
Re: Trying to get My Documents folder
« Reply #18 on: July 19, 2017, 10:52:53 am »
Hi Guys,

Something strange is happening.......Some progress

using the  code
Code: Pascal  [Select]
  1. function GetSpecialFolder(const CSIDL: Integer): WideString;
  2. var
  3.   SpecialPath: PWideChar;
  4. begin
  5.   Result := '';
  6.   SpecialPath := WideStrAlloc(MAX_PATH);
  7.   try
  8.     FillChar(SpecialPath^, MAX_PATH, 0);
  9.     if SHGetSpecialFolderPathW(0, SpecialPath, CSIDL, False) then
  10.       Result := SysToUTF8(SpecialPath)
  11.     else ShowMessage(SysErrorMessage(GetLastOSError));
  12.   finally
  13.     StrDispose(SpecialPath);
  14.   end;
  15. end;      
  16.  

This works from ide and if app is launched on old laz
On trunk, it does not work from ide, but does work if I launch the compiled application.

Even though this is progress, in that my application may work when run directly, I am unable to use ide to develop my apps.


Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

GetMem

  • Hero Member
  • *****
  • Posts: 3519
Re: Trying to get My Documents folder
« Reply #19 on: July 19, 2017, 10:56:09 am »
@Juha
According to @Josh the function "SHGetSpecialFolderPathW" fails. The api returns a BOOL type which in his case is false. It has nothing to do with Lazarus and any conversion below the function SHGetSpecialFolderPathW. The last OS error is: "The System Cannot find the path specified".  The question is why? It could be a privilege or elevation issue maybe an Antivirus?

@Josh
Try to run lazarus trunk as admin(see image), disable AV and remove SysToUTF8 it's not needed as @Juha explained earlier.
« Last Edit: July 19, 2017, 11:01:43 am by GetMem »

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: Trying to get My Documents folder
« Reply #20 on: July 19, 2017, 11:00:02 am »
Almost got trunk installed in a VM. So I'll try it there.
(edit: Works fine for me in Windows 7, Lazarus 1.6.4 and trunk in and out of the IDE)

But if you are using trunk, I don't think you should use WideString and SysToUTF8.
(Aren't you getting a lot of warnings about string conversions??)

Try this in trunk:

Code: Pascal  [Select]
  1. uses windirs, ShlObj;
  2.  
  3. function GetSpecialFolder(const CSIDL: Integer): String;
  4. var
  5.   SpecialPath: PWideChar;
  6. begin
  7.   Result := '';
  8.   SpecialPath := WideStrAlloc(MAX_PATH);
  9.   try
  10.     FillChar(SpecialPath^, MAX_PATH, 0);
  11.     if SHGetSpecialFolderPathW(0, SpecialPath, CSIDL, False) then
  12.       Result := SpecialPath
  13.     else ShowMessage(SysErrorMessage(GetLastOSError));
  14.   finally
  15.     StrDispose(SpecialPath);
  16.   end;
  17. end;
  18.  
  19. procedure TForm1.Button1Click(Sender: TObject);
  20. begin
  21.   Memo1.Lines.Add(GetWindowsSpecialDir(CSIDL_PERSONAL));
  22.   Memo1.Lines.Add(GetSpecialFolder(CSIDL_PERSONAL));
  23. end;
  24.  

B.T.W. What was wrong with using GetWindowsSpecialDir from windirs? Or does that also not work in your latest tests?
« Last Edit: July 19, 2017, 11:16:05 am by rvk »

josh

  • Hero Member
  • *****
  • Posts: 754
Re: Trying to get My Documents folder
« Reply #21 on: July 19, 2017, 11:00:13 am »
Hi

I have tried without systoutf8, but the error is before this, the oddity which you probably missed is that the error is occurring when running in ide.

I have created a video, which I will upload somewhere in minute, so you can see the oddity ( note the vid has the systoutf8 in it, but I have tested with and without and the result is the same).

Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3657
  • I like bugs.
Re: Trying to get My Documents folder
« Reply #22 on: July 19, 2017, 11:04:03 am »
You can use both. UnicodeString is reference counted on the other hand WideString basically is the same good old COM BSTR type windows always used, without ref. count. WideString is slower though, since windows makes a copy on winapi calls.
That's why UnicodeString should be used instead. WideString is a wrong type here. Why to use it without any benefit?
FPC developer Marco has repeated the same thing: use UnicodeString.

Quote
The funny thing is UnicodeChar points back to WideChar.  :)
Yes, there is no memory management involved. The types are identical.
« Last Edit: July 19, 2017, 11:09:07 am by JuhaManninen »

GetMem

  • Hero Member
  • *****
  • Posts: 3519
Re: Trying to get My Documents folder
« Reply #23 on: July 19, 2017, 11:10:52 am »
@Juha
Quote
Why to use it without any benefit?
Widestring is available from delphi4 up, is like a good old friend.  Besides I have a few, old projects in delphi 2007 where UnicodeString is not yet available. I'm using for historical reasons, no real benefit.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3657
  • I like bugs.
Re: Trying to get My Documents folder
« Reply #24 on: July 19, 2017, 11:15:50 am »
Widestring is available from delphi4 up, is like a good old friend.  Besides I have a few, old projects in delphi 2007 where UnicodeString is not yet available. I'm using for historical reasons, no real benefit.
Ok, that is a good reason.  :)

josh

  • Hero Member
  • *****
  • Posts: 754
Re: Trying to get My Documents folder
« Reply #25 on: July 19, 2017, 11:22:58 am »
Hi

uploaded a video showing my problem, I also changed the code to not use systoutf8

https://youtu.be/V9dGD1k3XLA
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3657
  • I like bugs.
Re: Trying to get My Documents folder
« Reply #26 on: July 19, 2017, 11:23:09 am »
I have created a video, which I will upload somewhere in minute, so you can see the oddity ( note the vid has the systoutf8 in it, but I have tested with and without and the result is the same).
We believe the problem is real. Did you test privileges as GetMem suggested? It sounds like the most probable cause now.

Sorry for nagging but if you still test with a seriously wrong SysToUTF8() conversion, it means you just don't understand the string types used by WinAPI "W" functions, FPC 3.0+ and Lazarus. Please study them!
This is about Lazarus:
 http://wiki.freepascal.org/Unicode_Support_in_Lazarus

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: Trying to get My Documents folder
« Reply #27 on: July 19, 2017, 11:24:59 am »
https://youtu.be/V9dGD1k3XLA

This video is unavailable.

Could you also post a complete test-project?

And please specify what OS and what version of Lazarus work and doesn't work.
(just "old laz" doesn't really cover it)

And did you also test the GetWindowsSpecialDir from windirs?

josh

  • Hero Member
  • *****
  • Posts: 754
Re: Trying to get My Documents folder
« Reply #28 on: July 19, 2017, 11:28:18 am »
Hi JuhaManninen
I am now not using systoutf8, and the error is still occuring in the IDE, the first 20 seconds of the video, show the simple app running in the IDE, you can see the code of the function. the second half of video, show the application that was created 20 seconds earlier running correctly when launched outside of the ide.
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

josh

  • Hero Member
  • *****
  • Posts: 754
Re: Trying to get My Documents folder
« Reply #29 on: July 19, 2017, 11:42:22 am »
@rvk

New Project, with tbutton on it and onclick event.

The code below on mine displays error when ran in IDE, but operates correctly when launched directly.

The old laz lazarus 1.3 fpc 2.71, hense the use of the strtoutf8 used previously, this has now been removed. 

Hopefully the video will start to appear soon...


Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,shlobj;//,LazUTF8 ;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. function GetSpecialFolder(const CSIDL: Integer): WideString;
  33. var
  34.   SpecialPath: PWideChar;
  35. begin
  36.   Result := '';
  37.   SpecialPath := WideStrAlloc(MAX_PATH);
  38.   try
  39.     FillChar(SpecialPath^, MAX_PATH, 0);
  40.     if SHGetSpecialFolderPathW(0, SpecialPath, CSIDL, False) then
  41.       Result := SpecialPath
  42.     else ShowMessage(SysErrorMessage(GetLastOSError));
  43.   finally
  44.     StrDispose(SpecialPath);
  45.   end;
  46. end;
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50.   ShowMessage(String(GetSpecialFolder(CSIDL_PERSONAL)));
  51.  
  52.  
  53.  // ShowMessage(GetWindowsSpecialDir(CSIDL_PERSONAL));
  54.   //ShowMessage(UTF16ToUTF8(GetSpecialFolder(CSIDL_PERSONAL))); //for FPC 2.6.4 --> unit LazUTF8
  55. end;
  56.  
  57.  
  58. end.
  59.  
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.