* * *

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

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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: 920
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.

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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

  • Sr. Member
  • ****
  • Posts: 354
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.
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

sash

  • Full Member
  • ***
  • Posts: 120
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 1.8.4 Unversioned directory FPC 3.0.4 x86_64-linux-gtk2

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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: 920
Re: Pointing a program to a filestream
« Reply #6 on: November 11, 2018, 12:38:22 am »
make the file as hidden.

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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

  • Jr. Member
  • **
  • Posts: 85
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
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 1.8.4 + FPC 3.0.4 (32-bit w/64-bit cross-compile)
Occasional testing of NewPascal
Current testing of Lazarus 2.0RC2
Windows 10 Pro x64, Version 1803 (Build 17134.228)

(Technically, I logon to these forums from multiple versions of Windows Pro/Enterprise...

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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: 7140
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 »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus