[fpc-pascal] Getting multiple files from GetOpenFileNameA

James Richters james at productionautomation.net
Tue May 21 21:12:41 CEST 2019


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.Flags := OFN_EXPLORER or OFN_FILEMUSTEXIST or OFN_ALLOWMULTISELECT;
   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





More information about the fpc-pascal mailing list