Recent

Author Topic: Sending commands to external consoleapplication  (Read 7833 times)

GannTrader

  • New Member
  • *
  • Posts: 13
Sending commands to external consoleapplication
« on: July 07, 2014, 10:56:55 am »
Hi

I have an external consoleapplication that i want to control with a Windows program, Lazarus application.
I have tried using TProcess with no success. Can this be done with TProcess or is there another way to do this in Lazarus or is Windows API what i should use. I want the external consoleapplication to react on a keystroke, like F1.

Im starting the external consoleapplication with this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
Aprocess.Executable:=Path+'program.exe';
Aprocess.Options:=AProcess.Options+[poUsePipes, poStderrToOutPut];
Aprocess.Execute;
end;


I try to send the keystroke to the external consoleapplication with this code:

procedure TForm1.Button2Click(Sender: TObject);
var str:string;
begin
str:=inttostr(VK_F1)+LineEnding;
AProcess.Input.Write(str[1], Length(str));
end;


I have tried some other str settings as well but with no success, the external consoleapplication is not responding.
What str setting do i need to use, is this the right way or should this be done in another way?

/GannTrader

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

GannTrader

  • New Member
  • *
  • Posts: 13
Re: Sending commands to external consoleapplication
« Reply #2 on: July 07, 2014, 11:05:53 am »
Have a look here
http://wiki.lazarus.freepascal.org/Executing_External_Programs
I have looked there and i cant find anything on that page that helps me.

/GannTrader

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Sending commands to external consoleapplication
« Reply #5 on: July 07, 2014, 11:34:27 am »
Sorry to hear that, but.... how do you think anybody can help you if you don't show what code you attempted?

On a different tack (assuming the extenral program in your initial post hasn't quit while the Button2Click event was fired): perhaps you could send the program some windows message sending it a key. No experience with that at all, but I think others may have it and perhaps a forum/wiki search could be useful as well...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1271
Re: Sending commands to external consoleapplication
« Reply #6 on: July 07, 2014, 11:50:23 am »
G'day,

I've recently done a load of work with the mplayer package in Lazarus-CCR.  It's a perfect example of using TProcess to communicate backwards and forwards with a running process.

http://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/mplayer/mplayerctrl.pas

Check out:
procedure TCustomMPlayerControl.Play;  // All the TProcess initialisation is done there
procedure TCustomMPlayerControl.SendMPlayerCommand(Cmd: string); // This is where all the input is sent to the running process
procedure TCustomMPlayerControl.TimerEvent(Sender: TObject); // This is where all the output is read from the running process

Don't worry about the fact that TPocessUTF8 is being used.  Conceptually they are the same control, just with minor additions for UTF8 handling....

Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

GannTrader

  • New Member
  • *
  • Posts: 13
Re: Sending commands to external consoleapplication
« Reply #7 on: July 07, 2014, 11:57:37 am »
Sorry to hear that, but.... how do you think anybody can help you if you don't show what code you attempted?
It wouldn't help showing the rest of the code. All thats importent is shown.

On a different tack (assuming the extenral program in your initial post hasn't quit while the Button2Click event was fired): perhaps you could send the program some windows message sending it a key. No experience with that at all, but I think others may have it and perhaps a forum/wiki search could be useful as well...
Yes i suspect that i must use Windoes API and i dont have any expeiance of it myself so i suspect i have a bit of reading to do unless someone can help with it.

/GannTrader

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Sending commands to external consoleapplication
« Reply #8 on: July 07, 2014, 12:02:46 pm »
Sorry to hear that, but.... how do you think anybody can help you if you don't show what code you attempted?
It wouldn't help showing the rest of the code. All thats importent is shown.
I'll take your word for it that those examples mentioned in the wiki article contain nothing more then....

Hope Mike Cornflake's example is a solution and/or that you find what you're looking for re the Windows API...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Sending commands to external consoleapplication
« Reply #9 on: July 07, 2014, 12:27:52 pm »
I don't think you can send function key through standard input, but I'm not sure either.

 

TinyPortal © 2005-2018