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

Jean SUZINEAU jean.suzineau at wanadoo.fr
Sat Jun 19 01:15:27 CEST 2021


I'm not familiar with GetSaveFileNameA, but usually for the size of a 
FileName  buffer I use the constant MAX_PATH which is defined in unit 
SysUtils.

MAX_PATH itself seems to be an alias from system unit where it is 
defined accordingly to the operating system (260 on windows, 4096 on 
Linux, 1024 on BSD, Solaris and Darwin).

Did you have a look at Microsoft's documentation for GetSaveFileNameA 
and the OPENFILENAMEA structure it takes as parameter ?
https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-getsavefilenamea

https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea

Particularly, you need to put in |nMaxFile |the size of the buffer 
pointed to by lpstrFile.

The folowing code works with a 120 characters long filename :

program test_GetSaveFileName;
uses
     SysUtils, CommDlg;
const DefaultFilename= 'Test';
var
    Buffer: array[0..MAX_PATH] of Char; //size MAX_PATH + 1 for 
terminating null char
    ofn: TOPENFILENAMEA;
begin
      StrPLCopy( Buffer, DefaultFilename, MAX_PATH);
      ofn.lStructSize:= sizeof(ofn);
      ofn.hInstance:= 0;
      ofn.lpstrFilter:= nil;
      ofn.lpstrCustomFilter:= nil;
      ofn.nMaxCustFilter:= 0;
      ofn.nFilterIndex:= 0;
      ofn.lpstrFile:= Buffer;
      ofn.nMaxFile:= Sizeof( Buffer);
      ofn.lpstrFileTitle:= nil;
      ofn.nMaxFileTitle:= 0;
      ofn.lpstrInitialDir:= nil;
      ofn.lpstrTitle:= 'test GetSaveFileNameA';
      ofn.Flags:= 0;
      ofn.nFileOffset:= 0;
      ofn.nFileExtension:= 0;
      ofn.lpstrDefExt:= nil;
      ofn.lCustData:= 0;
      ofn.lpfnHook:= nil;
      ofn.lpTemplateName:= nil;
      ofn.pvReserved:= nil;
      ofn.dwReserved:= 0;
      ofn.FlagsEx:= 0;
      if GetSaveFileNameA( @ofn)
      then
          WriteLn( ofn.lpstrFile);
end.

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


More information about the fpc-pascal mailing list