Recent

Author Topic: Acessing a file dialog from a console application  (Read 1190 times)

tfurnivall

  • New Member
  • *
  • Posts: 45
Acessing a file dialog from a console application
« on: May 17, 2025, 07:28:21 pm »
Hi, a total newbie to Lazarus here.

I'm migrating an application from VBA to Lazarus, and I need to gain access to a file dialog. The app itself is totally a console app - reads and processes an input file, generating all sorts of output files. I do, however, need to specify the main source file and will also need to be able to include other files.

What I'd really like is to have an easy way (without turning the app into a Forms app) to gain access to File Open and Save Dialogs. Initially Windows, but eventually in other OS environments.

Every result I've found so far is Forms based, and I want to explore a non-forms based solution - but still to have the GUI of the dialog box.

Any ideas?

Tony

PS RTFM is an acceptable response provided a pointer to the FM is added ;)

PPS "Turn it into a Forms App" is also acceptable, but will make me very unhappy ;-) ;-)

cdbc

  • Hero Member
  • *****
  • Posts: 2220
    • http://www.cdbc.dk
Re: Acessing a file dialog from a console application
« Reply #1 on: May 17, 2025, 07:44:32 pm »
Hi
Call the raw dialogs directly from the winders API...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

tfurnivall

  • New Member
  • *
  • Posts: 45
Re: Acessing a file dialog from a console application
« Reply #2 on: May 19, 2025, 03:22:40 pm »
Thanks, Benny.

How do I mark this one as solved/closed/whatever?

tfurnivall

  • New Member
  • *
  • Posts: 45
Re: Acessing a file dialog from a console application
« Reply #3 on: May 19, 2025, 04:05:04 pm »
I think I spoke too soon!

I'm using this forum as a ready source of knowledge, and also the Lazarus wiki  and the ref.pdf, prog.pdf, user.pdf and rtl.pdf files . There are many references to a windows unit (which apparently undergoes frequent surgery). However, I can't actually find this unit! There is nothing in rtl.pdf about a windows unit, and a Google search for "Where does Lazarus keep the windows unit" returns the following little gem:

Lazarus, an IDE for the Free Pascal compiler, doesn't store the "Windows unit" in a single, fixed location.

So (kidding aside) where can I find the non-single non-fixed location which will allow me to access the Windows API routines? Specifically those for File Dialogs? (Oh, and remember this is a console application, not a GUI application - I only need to use the dialog to get one file, everything else happens automagically).

Tony

I love learning new  (to me) products. I remember having to learn Microsoft C back in the 80's, and the only available document was the reference manual which jumped right in to ways of avoiding most of the formal structures of c. (Yech)

af0815

  • Hero Member
  • *****
  • Posts: 1392
Re: Acessing a file dialog from a console application
« Reply #4 on: May 19, 2025, 04:20:30 pm »
Here is a example of howto calling winapi https://forum.lazarus.freepascal.org/index.php?topic=52902.0 in the forum.
regards
Andreas

Awkward

  • Full Member
  • ***
  • Posts: 150
Re: Acessing a file dialog from a console application
« Reply #5 on: May 19, 2025, 04:32:53 pm »
Maybe easier will be to use FreeVision then?

af0815

  • Hero Member
  • *****
  • Posts: 1392
Re: Acessing a file dialog from a console application
« Reply #6 on: May 19, 2025, 04:52:50 pm »
What I'd really like is to have an easy way (without turning the app into a Forms app) to gain access to File Open and Save Dialogs. Initially Windows, but eventually in other OS environments.

Every result I've found so far is Forms based, and I want to explore a non-forms based solution - but still to have the GUI of the dialog box.
A dialoge, windows based ist always a Forms Dialog, because the windows system itself is form based. What you mean is a dialog like in the 'old' Midnigthcommander, but this dialoge is handled/done by application.

The idea of FreeVison is good, but FreeVision is not Lazarus !! It is a TextMode IDE see https://wiki.freepascal.org/Free_Vision
regards
Andreas

PascalDragon

  • Hero Member
  • *****
  • Posts: 6008
  • Compiler Developer
Re: Acessing a file dialog from a console application
« Reply #7 on: May 19, 2025, 09:12:06 pm »
The idea of FreeVison is good, but FreeVision is not Lazarus !! It is a TextMode IDE see https://wiki.freepascal.org/Free_Vision

FreeVision is not an IDE, it's a Text User Interface framework which is used by the IDE.

tfurnivall

  • New Member
  • *
  • Posts: 45
Re: Acessing a file dialog from a console application
« Reply #8 on: May 19, 2025, 09:23:44 pm »
Hmmm,

Because I only need it for one call (and because I'm lazy and don't want to type in a fully qualified file name) I was hoping to be able to cheat and get at a Windows FileOpenDialog - because the Lazarus environment is running on Windows, so the calls ought to be somewhere in the basement. However, until I migrate this console mode app to a Form based app (which is more or less what was happening in Excel VBA), I'll resign myself to the typing (and the fat-fingers).

Thanks for all the advice and the pointers to what look like interesting articles.

For now I'm running into a much more basic problem....

Tony

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: Acessing a file dialog from a console application
« Reply #9 on: May 20, 2025, 10:15:44 pm »
Because I only need it for one call (and because I'm lazy and don't want to type in a fully qualified file name) I was hoping to be able to cheat and get at a Windows FileOpenDialog

You can invoke a FileOpenDialog from a console app without using any Forms.  You could simply create and execute the LCL's TOpenDialog component in code.

Or, on Windows, you can directly call the Win32 GetOpenFileName() function, or invoke the IFileOpenDialog COM interface.  See Open and Save As dialog boxes and Common Item Dialog on MSDN.
« Last Edit: May 20, 2025, 10:18:32 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

n7800

  • Sr. Member
  • ****
  • Posts: 368
Re: Acessing a file dialog from a console application
« Reply #10 on: May 30, 2025, 02:52:15 pm »
As far as I know, the "graphical application" created by the IDE by default differs from the "console" only in code (and some unimportant options). You can use "Main Menu > Project > View Project Source" to see the "main unit" of the application, where the key differences are indicated.

You definitely need to include the "Interfaces" and "Forms" units (and add the LCL dependency to the project, since they are located there). The first one is needed for proper program linking (it just needs to be present), and the second one is needed to call "Application.Initialize". After that, you can do everything the same way as in a regular "graphical application":

Code: Pascal  [Select][+][-]
  1. // project requires a dependency on the LCL package
  2. program Project1;
  3. uses
  4.   Interfaces, Forms,
  5.   Dialogs; // for TOpenDialog
  6. var
  7.   d: TOpenDialog;
  8. begin
  9.   Application.Initialize;
  10.   d := TOpenDialog.Create(nil);
  11.   d.Execute;
  12.   writeln(d.FileName);
  13.   d.Free;
  14. end.
  15.  

This code is simple and should be cross-platform (although I only checked it on Windows).

 

TinyPortal © 2005-2018