[fpc-pascal] Getting multiple files from GetOpenFileNameA

Cyrax evvke at hotmail.com
Tue May 21 22:55:11 CEST 2019

On 21.5.2019 22.12, James Richters wrote:
> This is the function I came up with to use GetOpenFileNameA() to get multiple files, and put the file names all into a StringList.
> I think I am doing something fundamentally wrong here because I can run this in a loop sometimes 2 or 4 times, or sometimes 30 times, and it seems to work but eventually my program crashes with an access violation at various different points.. it's not always the same point in the program that crashes, but if I rig this to only build the stringlist once, I can run the rest of the process over and over including the GetOpenFileNameA() without issues.   I wonder if my method of putting the files names into the stringlist is flawed, I made it up myself based on what I could gather from the documentation of GetOpenFileNameA(),  maybe I am missing something.. or maybe the way I'm setting the string list to nil and then re-creating it is causing an issue?   The Writeln's I put in seem to be what I expect, but still it crashes at some point when I run this in a loop.   Below is my procedure, can anyone see anything I am doing in a bad way that would cause the instability I am experiencing?
> I am freeing the stringlist first thing, and I only create a new one if GetOpenFileNameA()is successful.  Then later on I check for the existence of the stringlist and if it's there, I process my files, the loop back and run this again, if not, I just exit, because if the string list does not exist then GetOpenFileNameA() was closed without selecting any files, so the process is complete.
> Another issue with this....  I am only able to select 6 or 7 files with this and then GetOpenFileNameA() returns with False... I suspect I need to make a memory buffer for more files but don't know how to do that, anyone have any advice on how to fix that?
> Procedure GetFilesIntoStringlist;
> Begin
>     IF (File_Stringlist<>Nil) Then
>        FreeAndNil(File_Stringlist);
>     fillchar(TFileName, sizeof(TFileName), 0);
>     TFileName.lStructSize:=sizeof(TFileName);
>     TFileName.hwndOwner:=0;
>     TFileName.lpstrFile:=ret;
>     TFileName.lpstrFile[0]:=#0;
>     TFileName.nMaxFile:=100;
>     TFileName.lpstrDefExt:='';
>     TFileName.lpstrTitle:='Production Automation Open Dpax File:';
>     TFileName.lpstrFilter:='Draw.Pax Files (*.DPax)'+#0+'*.DPax'+#0;
>     If Drawpaxfilename<>'' then
>        Begin
>           TFileName.lpstrFile:=Pchar(Drawpaxfilename+'DPax'+#0);
>           TFileName.lpstrInitialDir:=Pchar(ExtractFilePath(Drawpaxfilename)+#0);
>        End;
>     OpenFileResult:=GetOpenFileNameA(@TFilename);
>     If OpenFileResult then
>        Begin
>           Textcolor(10);
>           IF File_Stringlist=Nil Then
>              File_Stringlist:=TStringlist.Create;
>           Loopx:=-1;
>           filenum:=0;
>           Repeat
>              Filenamestr:='';
>              Repeat
>                 Inc(Loopx);
>                 if TFileName.lpstrFile[loopx]<>#0 Then
>                    begin
>                       Write(TFileName.lpstrFile[loopx]);
>                       Filenamestr:=Filenamestr+TFileName.lpstrFile[loopx];
>                    end;
>              Until TFileName.lpstrFile[loopx]=#0;
>              Writeln;
>              File_Stringlist.add(Filenamestr);
>           Until TFileName.lpstrFile[loopx+1]=#0;
>           Textcolor(14);
>           If File_Stringlist.Count=1 then
>              Writeln(File_Stringlist.Count,' File Selected')
>           Else
>              Writeln(File_Stringlist.Count-1,' Files Selected');
>        End;
> End;
> James
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

It looks like that you are using the type name directly (TFileName). 
Where is TFileName defined?

More information about the fpc-pascal mailing list