<div dir='auto'>I did notice lpstrFilter is terminated by one #0 instead of two. Other than that not at a pc to test.<br><br><div data-smartmail="gmail_signature">--<br>Alexander Grotewohl<br>http://dcclost.com</div></div><div class="gmail_extra"><br><div class="gmail_quote">On May 23, 2019 7:52 AM, James Richters <james@productionautomation.net> wrote:<br type="attribution" /><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><p><span style="font-size:14pt">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.</span></p><p><span style="font-size:14pt">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.</span></p><p><span style="font-size:14pt">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.  </span></p><p><span style="font-size:14pt"> </span></p><p><span style="font-size:14pt">Any Advice or suggestions on any of this, or on how I could improve the structure or methods of this program are greatly appreciated.</span></p><p><span style="font-size:14pt"> </span></p><p><span style="font-size:14pt">James</span></p><p><span style="font-size:14pt;font-family:'consolas'"> </span></p><p><span style="font-size:14pt;font-family:'consolas'"> </span></p><p><span style="font-size:14pt;font-family:'consolas'"> </span></p><p><span style="font-size:14pt;font-family:'consolas'">{$mode objfpc}{$H+}</span></p><p><span style="font-size:14pt;font-family:'consolas'">//   https://docs.microsoft.com/en-us/windows/desktop/api/commdlg/nf-commdlg-getopenfilenamea</span></p><p><span style="font-size:14pt;font-family:'consolas'">//   https://docs.microsoft.com/en-us/windows/desktop/api/commdlg/ns-commdlg-tagofna</span></p><p><span style="font-size:14pt;font-family:'consolas'">Uses</span></p><p><span style="font-size:14pt;font-family:'consolas'">  sysutils,Windows,Commdlg,Classes,CRT;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Var</span></p><p><span style="font-size:14pt;font-family:'consolas'">Open_File          : TOpenFileNameA;</span></p><p><span style="font-size:14pt;font-family:'consolas'">ret                : array[0..100000] of char;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Filenum            : Word;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Target_File        : AnsiString;</span></p><p><span style="font-size:14pt;font-family:'consolas'">File_Stringlist    : tstringList;</span></p><p><span style="font-size:14pt;font-family:'consolas'">{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}</span></p><p><span style="font-size:14pt;font-family:'consolas'">Procedure Display_File_StringList(Var Display_String_List:tStringList);</span></p><p><span style="font-size:14pt;font-family:'consolas'">Var</span></p><p><span style="font-size:14pt;font-family:'consolas'">  SL_Loop : Word;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">   If Display_String_List.Count=0 then</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Textcolor(12);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Writeln('0 Files Selected')</span></p><p><span style="font-size:14pt;font-family:'consolas'">      End</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Else</span></p><p><span style="font-size:14pt;font-family:'consolas'">   If Display_String_List.Count=1 then</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">         TextColor(10);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Writeln(Display_String_List[0]);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Textcolor(14);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Writeln('1 Files Selected');</span></p><p><span style="font-size:14pt;font-family:'consolas'">      End</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Else</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">         TextColor(13);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Writeln('Path for all files: ',Display_String_List[0]);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         TextColor(10);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         For SL_Loop:= 1 to Display_String_List.Count-1 Do</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Writeln('File #',SL_Loop,': ',Display_String_List[SL_Loop]);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Textcolor(14);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Writeln(Display_String_List.Count-1,' Files Selected');</span></p><p><span style="font-size:14pt;font-family:'consolas'">      End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}</span></p><p><span style="font-size:14pt;font-family:'consolas'">Function Get_Files_Into_Stringlist(Var String_List:tStringList):Boolean;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Var</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File_Result      : Boolean;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Position_of_Filename  : Word;</span></p><p><span style="font-size:14pt;font-family:'consolas'">Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">   String_List.Clear;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File_Result:=GetOpenFileNameA(@Open_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   If Open_File_Result then</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Position_of_Filename:=0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Repeat</span></p><p><span style="font-size:14pt;font-family:'consolas'">            String_List.add(StrPas(@Open_File.lpstrFile[Position_of_Filename]));</span></p><p><span style="font-size:14pt;font-family:'consolas'">            inc(Position_of_Filename,length(String_List[String_List.Count-1])+1);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Until Open_File.lpstrFile[Position_of_Filename]=#0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">      End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Writeln(Open_File_Result);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Get_Files_Into_Stringlist:=Open_File_Result;</span></p><p><span style="font-size:14pt;font-family:'consolas'">End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}</span></p><p><span style="font-size:14pt;font-family:'consolas'">Procedure Process_File(Some_File: AnsiString);</span></p><p><span style="font-size:14pt;font-family:'consolas'">Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">   TextColor(11);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Writeln('Processing File: ',Some_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}</span></p><p><span style="font-size:14pt;font-family:'consolas'">Begin {Main Program}</span></p><p><span style="font-size:14pt;font-family:'consolas'">   fillchar(Open_File, sizeof(Open_File), 0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   fillchar(ret, sizeof(ret), 0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lStructSize:=sizeof(Open_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.hwndOwner:=0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lpstrFile:=ret;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lpstrFile[0]:=#0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.nMaxFile:=100000;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.Flags := OFN_EXPLORER or OFN_FILEMUSTEXIST or OFN_ALLOWMULTISELECT;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lpstrDefExt:='.txt';</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lpstrTitle:='My Program - Open File';</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Open_File.lpstrFilter:='All Files (*.*)'+#0+'*.*'+#0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   File_Stringlist:=TStringlist.Create;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   If paramstr(1)<>'' then</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Target_File:=paramstr(1);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Process_File(Target_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">      End</span></p><p><span style="font-size:14pt;font-family:'consolas'">   else</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Target_File:='';</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Repeat</span></p><p><span style="font-size:14pt;font-family:'consolas'">      If Target_File<>'' then</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // The following line seems to cause my program to crash.</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // It does seem to work as I expect.  It puts me in the directory</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // of the file and shows the file name, but for some reason if I select</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // more than one file than I get</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // An unhandled exception occurred at $0040B428:</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // EAccessViolation:</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040B428</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $00414447</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040AE1E</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $00414447</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040AE1E</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $00414447</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040AE1E</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $00414447</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040AE1E</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $00414447</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040AE1E</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040A03D</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // in a repeating loop if I use the command with two #0 at the end of</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // Open_File.lpstrFile:=Pchar(Target_File+#0+#0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">            </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // Or I get:</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // EAccessViolation: An unhandled exception occurred at $0040BB56:</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // with an exit code of 253</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // or </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // An unhandled exception occurred at $0040BDD2:</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // EAccessViolation: Access violation</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // $0040BDD2</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // with an exit with a code 217</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // if I use one #0 at the end of</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // Open_File.lpstrFile:=Pchar(Target_File+#0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">            </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // these errors seem to show up more if I select multiple files or </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // change directories.  </span></p><p><span style="font-size:14pt;font-family:'consolas'">            Open_File.lpstrFile:=Pchar(Target_File+#0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">            </span></p><p><span style="font-size:14pt;font-family:'consolas'">            </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // If I leaved the above commented out, then I should be able to use the </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // command below to just put me in the directory specified, but with no default file</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // This does not work the way I expect it to work at all.</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // It does not put me into the directory of the file used as a parameter, it</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // Then for other files it puts me one level before the one I was just in</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // I want to be back where I was.  If I give it a parameter, I want to </span></p><p><span style="font-size:14pt;font-family:'consolas'">            // be in the directory of the file the parameter specifies, and then</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // I want to be in the directory of the previous file.</span></p><p><span style="font-size:14pt;font-family:'consolas'">            // This just is not working the way I expect but does not cause any crash.</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Open_File.lpstrInitialDir:=Pchar(ExtractFilePath(Target_File)+#0+#0);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Get_Files_Into_Stringlist(File_Stringlist);</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Display_File_StringList(File_Stringlist);</span></p><p><span style="font-size:14pt;font-family:'consolas'">      If (File_Stringlist.Count=1) and FileExists(File_Stringlist[0]) Then</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Target_File:=File_Stringlist[0];</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Process_File(Target_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">         End</span></p><p><span style="font-size:14pt;font-family:'consolas'">      Else</span></p><p><span style="font-size:14pt;font-family:'consolas'">      If (File_Stringlist.Count>1) Then</span></p><p><span style="font-size:14pt;font-family:'consolas'">         Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Filenum:=0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Repeat</span></p><p><span style="font-size:14pt;font-family:'consolas'">               Inc(Filenum);</span></p><p><span style="font-size:14pt;font-family:'consolas'">               If (Filenum<File_Stringlist.Count) Then</span></p><p><span style="font-size:14pt;font-family:'consolas'">                  Begin</span></p><p><span style="font-size:14pt;font-family:'consolas'">                     Target_File:=File_Stringlist[0];</span></p><p><span style="font-size:14pt;font-family:'consolas'">                     If Target_File[Length(Target_File)]<>'\' Then</span></p><p><span style="font-size:14pt;font-family:'consolas'">                        Target_File:=Target_File+'\';</span></p><p><span style="font-size:14pt;font-family:'consolas'">                     Target_File:=Target_File+File_Stringlist[Filenum];</span></p><p><span style="font-size:14pt;font-family:'consolas'">                     Process_File(Target_File);</span></p><p><span style="font-size:14pt;font-family:'consolas'">                  End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">            Until Filenum>=File_Stringlist.Count;</span></p><p><span style="font-size:14pt;font-family:'consolas'">         End;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   Until File_Stringlist.count=0;</span></p><p><span style="font-size:14pt;font-family:'consolas'">   File_Stringlist.Free;</span></p><p><span style="font-size:14pt;font-family:'consolas'">end.</span></p><p><span style="font-size:14pt;font-family:'consolas'">{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}</span></p></div></div></blockquote></div><br></div>