[fpc-pascal] Getting multiple files from GetOpenFileNameA

James Richters james at productionautomation.net
Tue May 21 23:17:57 CEST 2019

I have it defined with the program variables:

Here are all my Uses and Vars:


TFilename              : TOpenFileNameA;
ret                    : array[0..100] of char;
OpenFileResult         : Boolean;
LoopX,filenum          : Longint;
filenamestr            : ansistring;
File_Stringlist        : tstringList;

The only examples of how to use GetOpenFilenameA I could find by searching were in C, so what I came up with is a combination of trying to translate C code to Pascal that I previously did to get GetSaveFilenameA to work and then a lof of just messing with it to try to get GetOpenFilenameA to work... I've used GetOpenFilenameA before, but only for very small programs that would have only asked for it once and then they would be done, and I didn't use OFN_AllowMultiSelect...  I got really confused by the examples on how to break out the contents .lpstrfile but by just reading the Microsoft document that said it was just the path then #0 then each file name followed by #0 with a double #0 at the end.. so I just made up something to extract the path and files and put them all in a much easier to manage tStringList


-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Cyrax
Sent: Tuesday, May 21, 2019 4:55 PM
To: fpc-pascal at lists.freepascal.org
Subject: Re: [fpc-pascal] Getting multiple files from GetOpenFileNameA

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?

fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

More information about the fpc-pascal mailing list