Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help catching STDIN in background

Status
Not open for further replies.

rekcut01

Programmer
Jun 19, 2014
14
0
0
US
Is there a way to catch <STDIN> from a background process running in windows? I want to write a service that loops, running it in the back ground and just listen for <STDIN>, when it gets input go automatically and run a perl program.

Code:
#!c:/dwimperl/perl/bin/perl.exe 
# PERL MODULES WE WILL BE USING 
use strict; 
use warnings; 
use integer; 
use CGI; 
use Win32::OLE; 
use Win32::OLE qw(in with); 
use Win32::OLE::Variant; 
use Win32::OLE::Const 'Microsoft Excel'; 
use Spreadsheet::ParseExcel::Worksheet; 
use Spreadsheet::ParseExcel::SaveParser; 
use Spreadsheet::WriteExcel; 
use Time::Piece; 
use Data::Dumper; 
use 5.010001; 
 
 
 
 
print "Content-type: text/html\n\n"; 
 
while (1) 
{ 
	#my $input = <STDIN>; 
	my $input; 
	if (-t STDIN) 
	{ 
		print "hello"; 
		$input = <STDIN>; 
		$input =~ s/\r\n//; 
	} 
 
	#$input =~ s/\r\n//; 
	my $global_col; 
	my $global_row; 
 
	my $parser   = Spreadsheet::ParseExcel::SaveParser->new(); 
	my $workbook = $parser->parse('C:\Users\tuckerri\Desktop\Report.xls'); 
	if ( !defined $workbook )  
	{          
		die $parser->error(), ".\n";  
	} 
	my $worksheet = $workbook->worksheet(0); 
	for my $worksheet ( $workbook->worksheets() )  
	{ 
 
		my ( $row_min, $row_max ) = $worksheet->row_range(); 
		my ( $col_min, $col_max ) = $worksheet->col_range(); 
 
		for my $row ( $row_min .. $row_max )  
		{ 
			for my $col ( $col_min .. $col_max )  
			{ 
				my $cell = $worksheet->get_cell( $row, $col ); 
				next unless $cell; 
 
				my $cmp_string = $cell->value(); 
				 
				#print "$row $col \n"; 
 
				if ($cmp_string eq $input) 
				{ 
					my $out_time = localtime; 
					my $n= &mod_spreadsheet($row, $col); 
				} 
			} 
		} 
	} 
 
} 
 
sub mod_spreadsheet() 
{ 
	my $sub_row = $_[0]; 
	my $sub_col = $_[1]; 
	my $date = localtime->strftime('%m/%d/%y %H:%M', localtime); 
	print "$date \n"; 
	 
	#my $sub_out_time = $_[2]; 
	#my $out_time = sub main::localtime; 
	#print $sub_out_time; 
 
	my $parser1   = Spreadsheet::ParseExcel::SaveParser->new(); 
	my $workbook1 = $parser1->Parse('c:\Users\tuckerri\Desktop\Report.xls'); 
	if ( !defined $workbook1 )  
	{          
		die $parser1->error(), ".\n"; 
	} 
	my $worksheet1 = $workbook1->worksheet(0); 
	my $cell1 = $worksheet1->get_cell( $sub_row, $sub_col + 1 ); 
 
	$worksheet1->AddCell( $sub_row, $sub_col + 1, $date ); 
	## Write over the existing file or write a new file. 
	$workbook1->SaveAs('c:\Users\tuckerri\Desktop\Report.xls'); 
}

I would like to put this in the background listening for <STDIN>.... the PC I have this on has no keyboard, mouse or monitor so the only input will be from the scanner.

Is there a way to make this happen?

Thanks in advance
-Rick
 
This isn't an elegant solution, but I've used this idea / concept in a couple of programs.
Read the documentation on GuiTest and some of this will make more sense.

- Start your program and give it an easily identifiable title
- use Win32:GuiTest ;
- $hWnd = FindWindowLike('your program title') ; # See documentation on GuiTest
You'll get a window handle out of this (possibly @hWnd if the name is generic)
- ($l,$t,$r,$b) = GetWindowRect(hWnd) ; # Get the coordinates of your window
- Each time you want to look for STDIN use
($x,$y) = GetCursorPos() ;
- check to see if ($x, $y) are in the borders of your program window
- If so, break out for generic: print "What now? " ; $response = <STDIN> ;
type input retrieval. If ($x, $y) are not in your borders - just keep
doing what you were doing before.
*** Caution:
- Generate a function that will test the state of the window $hWnd.
If the user moves it, ($l,$t,$r,$b) values will change

 
Hi Rick,

I've not written this kind of Perl script for Windows but you do something similar to this in *nix when you write a daemon process - you close and then reopen stdin, stdout and stderr. I'll have a look around.

Mike


 
My answer (above) is complete twaddle...

I don't think you can *quite* do what you're asking, but there's probably a way around it.

Can you start the process on the end of a pipe and then block until there's something to read?

Perl:
open(P, 'scanner_proc |')
while(<P>){
#  do whatever you're going to do with the scanner output
}
close(P);

Mike


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top