[fpc-pascal] GetSaveFileNameA limited to 100 characters in default name

Jean SUZINEAU jean.suzineau at wanadoo.fr
Sat Jun 19 15:54:49 CEST 2021


It's curious, I'm not completely sure we are reasoning about the same 
function and record.

You mention TFileName but for me in FPC 3.2.0:

-  TFileName is declared as "TFileName= type string;" in sysutilh.inc.

- OPENFILENAMEA is declared as record in CommDlg.pp line 94
   and TOPENFILENAMEA=OPENFILENAMEA;
   and POPENFILENAMEA=^OPENFILENAMEA;

-function GetSaveFileNameA is imported from Comdlg32.dll in CommDlg.pp 
line 595
   function GetSaveFileNameA(_para1:LPOPENFILENAME):WINBOOL; stdcall; 
external 'comdlg32' name 'GetSaveFileNameA';

Le 19/06/2021 à 02:51, James Richters via fpc-pascal a écrit :
> This got me thinking… my default filename is just a recommended 
> default… but TFileName.lpstrFileis also what returns the filename to 
> use, and the user could use the save-as dialog to add a directory, and 
> change the name of the file to something else..etc… so the file name 
> could be significantly larger than the default…
Yes
>
> So I think I’m best off setting TFileName.nMaxFile:= Max_Path;then 
> TFileName.lpstrFile will always be able to hold anything the operating 
> system can support.
>
The nMaxFile value include the terminal null character, so it should be 
Max_Path+1, and it must be the size of the buffer lpstrFile is pointing to.

You shouldn't use "TFileName.lpstrFile:=Pchar(DefaultFileName);" because 
in this case lpstrFile will point to a buffer of size 
Length(DefaultFileName)+1 .

And to get the value back from the function, given a variable s: string, 
you should do something like s:= StrPas( TFileName.lpstrFile);

 From Microsoft documentation:

nMaxFile
The size, in characters, of the buffer pointed to by lpstrFile. The 
buffer must be large enough to store the path and file name string or 
strings, including the terminating NULL character. The GetOpenFileName 
<https://docs.microsoft.com/en-us/windows/desktop/api/commdlg/nf-commdlg-getopenfilenamea> 
and GetSaveFileName 
<https://docs.microsoft.com/en-us/windows/desktop/api/commdlg/nf-commdlg-getsavefilenamea> 
functions return FALSE if the buffer is too small to contain the file 
information. The buffer should be at least 256 characters long.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210619/eb0402d8/attachment-0001.htm>


More information about the fpc-pascal mailing list