Recent

Author Topic: Pointing a program to a filestream  (Read 2909 times)

guest48704

  • Guest
Pointing a program to a filestream
« on: November 10, 2018, 09:16:23 pm »
I am executing an external program that requires an input file:

external_pgm.exe  input_file_name  --a_parameter

Is there any way to make the program recognize a FileStream as input instead of an actual file?

Thanks

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Pointing a program to a filestream
« Reply #1 on: November 10, 2018, 09:53:57 pm »
I think to make sure it works it would be best to simply write the filestream out to disk via a name and give it that name..

You could also try using the "CreateFileMapping" but that is for windows and I don't know what the cross platform equal
is.. This API can treat a chunk of memory as a file and you give it a file name for other sources to read from...

 This is something that maybe beyond you ? who knows.
The only true wisdom is knowing you know nothing

guest48704

  • Guest
Re: Pointing a program to a filestream
« Reply #2 on: November 10, 2018, 11:06:18 pm »
Never have played with FileMapping. 

The output from the externally executed program is pipped into a MemoryStream as below, but how to put the input data into a MemoryStream and get it into the external program, I don't know.

function TForm1.RunExternalApp(DosApp:String) : string;
const READ_BYTES = 2048;
var aProcess: TProcess;
    MemStream: TMemoryStream;
    NumBytes: LongInt;
    BytesRead: LongInt;
    Lines: TStringList;
begin
     MemStream := TMemoryStream.Create;
     Lines := TStringList.Create;
     BytesRead := 0;
     aProcess := TProcess.Create(nil);
     aProcess.CommandLine := DosApp;
     aProcess.ShowWindow := swoHIDE;
     aProcess.Options := aProcess.Options + [poUsePipes];
     aProcess.Execute;

     while aProcess.Running do begin
      MemStream.SetSize(BytesRead + READ_BYTES);
      NumBytes := aProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);

      if NumBytes > 0 then
         Inc(BytesRead, NumBytes)
         else
         BREAK;
    end;

    MemStream.SetSize(BytesRead);
    Lines.LoadFromStream(MemStream);
    Result := Lines.Text;
     aProcess.Free;
     Lines.Free;
     MemStream.Free;
end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Pointing a program to a filestream
« Reply #3 on: November 10, 2018, 11:51:31 pm »
I may be wrong but I think that if the external program requires a real file name there is no cross-platform way to make it do with a stream. Your best (or only!) bet would then be to save your data to a (temporal) file and pass it to the external program. As that's actually rather easy to do, there's no need to complicate your life further.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Pointing a program to a filestream
« Reply #4 on: November 11, 2018, 12:11:29 am »
Is there any way to make the program (written by someone else) recognize a FileStream (in your program) as input instead of an actual file?
Generally no (I'm not counting hacking process code or OS kernel and virtual filesystems). If process requires a filename it expects a file.
In contrary, if third party process reads stdin - it could be easily fed with a file instead.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

guest48704

  • Guest
Re: Pointing a program to a filestream
« Reply #5 on: November 11, 2018, 12:22:33 am »
I may be wrong but I think that if the external program requires a real file name there is no cross-platform way to make it do with a stream. Your best (or only!) bet would then be to save your data to a (temporal) file and pass it to the external program. As that's actually rather easy to do, there's no need to complicate your life further.

  That is probably the correct answer.  This program deals with passwords and I didn't want to put anything out to file if possible.  Once read back in, the file is scrubbed and deleted immediately, so that is as much security as I can give.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Pointing a program to a filestream
« Reply #6 on: November 11, 2018, 12:38:22 am »
make the file as hidden.
The only true wisdom is knowing you know nothing

guest48704

  • Guest
Re: Pointing a program to a filestream
« Reply #7 on: November 11, 2018, 03:45:25 am »
make the file as hidden.

I never knew that could be done.  Is this how it would be done?

  function WriteStringTo(const FileName: string; data : string): boolean;
  var outfile : textfile;
  begin
       assignfile(outfile, FileName);
       FileSetAttr(FileName, faHidden);
       rewrite(outfile);

       writeln(outfile, data);

       closefile(outfile);
       Result := True;
  end;

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Pointing a program to a filestream
« Reply #8 on: November 11, 2018, 05:58:38 am »
A hidden file is still pretty trivial to see/read.

You might want to ensure that your app runs in a specific security context, and that only it has access to that file while in use.

That would be more secure than just hidden alone.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

guest48704

  • Guest
Re: Pointing a program to a filestream
« Reply #9 on: November 11, 2018, 02:39:49 pm »
A hidden file is still pretty trivial to see/read.

You might want to ensure that your app runs in a specific security context, and that only it has access to that file while in use.

That would be more secure than just hidden alone.

Ok, thanks.  Going with this unless some objection:

Code: [Select]
  function WriteStringTo(const FileName: string; data : string): boolean;
  var outfile : textfile;
  begin
       FileSetAttr(FileName, FMSHAREEXCLUSIVE and FMOPENWRITE and FAHIDDEN);
       assignfile(outfile, FileName);
       rewrite(outfile);

       writeln(outfile, data);

       closefile(outfile);
       Result := True;
  end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Pointing a program to a filestream
« Reply #10 on: November 11, 2018, 02:46:56 pm »
Well, AND is wrong anyway for file options. You should OR the values, since it is Boolean logic.... There's more wrong, but I'll have a look.
Ratio <sigh  >:D> fmOpenXXX and fmShareExclusive are mutually exclusive values. So AND never applies. If you OR then you include both values.
Elementary, Watson...
Code: Pascal  [Select][+][-]
  1. program andorlogic;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. begin
  4.   writeln (2 and 1); // this is zero, not three.... %10 and %01 is %00
  5.   writeln (2 or 1 ); // this makes it three....%10 or %01 is %11
  6. end.

Hope that helps... :-\ :-\ :-\ :-X

« Last Edit: November 11, 2018, 03:23:05 pm by Thaddy »
Specialize a type, not a var.

guest48704

  • Guest
Re: Pointing a program to a filestream
« Reply #11 on: November 11, 2018, 06:27:24 pm »
Well, I resemble that remark - a Mr. Watson.

  Thanks.  I have apparently always been doing that wrong.  Learned it from a bad piece of code I copied once.

 

TinyPortal © 2005-2018