[fpc-pascal] Getting multiple files from GetOpenFileNameA

Santiago A. svaa at ciberpiula.net
Thu May 23 21:50:12 CEST 2019

El 23/05/19 a las 13:52, James Richters escribió:
> I have put together a program that demonstrates the issue I am 
> having.  I have re-structured it a bit to try to make it more clear 
> where the problem is.
> While putting this together, I was able to get it to run fine with no 
> problems if I did not try to put GetOpenFileNameA back to the 
> directory of the last file I processed. Including a file sent in as a 
> parameter.   If I set Open_File.lpstrFile:=Pchar(Target_File+#0+#0); 
> where Target_File is the complete path and file name of the last file 
> I processed,  If I select only one file, then the next time around it 
> DOES work.. and will put me in the directory of Target_File and will 
> show the filename of Target_File as a default, but if I select more 
> than one file, the next time around, it crashes.. I have detailed the 
> crash information in the sameple program comments.
> Another way I wish to run this is to leave Open_File.lpstrFile alone 
> and use Open_File.lpstrInitialDir to put me in the directory of the 
> last processed file, but not specify a default filename.  I can’t get 
> this to work correctly at all, but if I use Open_File.lpstrInitialDir 
> without Open_File.lpstrFile then it does not crash,  I am just not put 
> in the directory I expect to be in.
> Any Advice or suggestions on any of this, or on how I could improve 
> the structure or methods of this program are greatly appreciated.

At the beginning you set

That is right Open_File.lpstrFile points to "ret", and "ret" is an array 
of 100000, a good buffer.

The problem is here:


That is wrong. Now Open_File.lpstrFile doesn't point to "ret" anymore, 
now it points to a string with length(Target_File)+2. But 
Open_File.nMaxFile:=100000, so the GetOpenFileNameA thinks there is 
plenty of room, so rises no error and overwrites whatever is after 

instead of




You should initialize to "ret" in each loop, in fact, it should be 
initialized inside the procedure. and Open_File.lpstrFilter:='All Files 
(*.*)'+#0+'*.*'+#0; needs an extra #0;

In addition, just for style, instead of


And why are all those variables global if they are only used inside the 

And instead of this




                If (Filenum<File_Stringlist.Count) Then



If Target_File[Length(Target_File)]<>'\' Then





             Until Filenum>=File_Stringlist.Count;

Replace with


             for file_num:=1 to File_Stringlist.count-1 do


FOR is you friend


Santiago A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190523/bde98c78/attachment.html>

More information about the fpc-pascal mailing list