passing argument from java to an exe program

February 3, 2010 at 14:27:01
Specs: Windows XP
Is it possible (from JAVA or PHP) to run an executable program (.exe), passing in an argument. The exe program will return a value, so that Java can decide what to do based on the value?

See More: passing argument from java to an exe program

Report •


#1
February 3, 2010 at 15:37:10
I don't know anything at all about JAVA and wasn't at all sure about how to approach the problem, but I gave it a whirl using a C program and php.. no real error checking though.

c source (console app)

#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[]) {  

   int test = 0;

   if((sscanf(argv[1], "int=%d", &test)) != 1) {
      fflush(stdout);
      fprintf(stdout, "-1");
   } else {
      fflush(stdout);
      fprintf(stdout, "%d", test);
   }

   return (0);
}

php script

<?php   

   $res = exec('c:\test.exe int=250');
   settype($res, integer);
 
   if($res != -1) {
      echo $res;
   } else {
      echo "error in program";
   }  

?>

the exec() returns a string but I'm not that familiar with it. I would suspect that the computer running the script would also have to have the application. I have no idea on doing something like this from the Internet or such.

At any rate, hope that helps.


Report •

#2
February 3, 2010 at 15:56:45
My C code looks like yours. The PHP looks simple enough also. I'd like to get this to work on my web page. If it actually works, you're the hero today. I'm hoping this is the case.

I was sort of expecting "server side"/"client side" conflicts for any reply. But I'll give it a try.

Thanx for your effort.


Report •

#3
February 3, 2010 at 16:43:14
During my testing, I've come up with another question:

What's the syntax for exec(). I need to pass a string, rather than an integer. Is the exec() command a php instruction for interfacing with C programs or other executable programs?


Report •

Related Solutions

#4
February 3, 2010 at 17:11:51
I'm having my doubts with the exec() command. I fixed my C program to force a return of 1. I'm still seeing a return of 0 from the exec(). I'm thinking that the return value is only a status of the execution - 0 mean successful execution.

Unless someone can shed some light on this I'm not sure what else to do.


Report •

#5
February 3, 2010 at 21:13:23
During my testing, I've come up with another question:

What's the syntax for exec(). I need to pass a string, rather than an integer. Is the exec() command a php instruction for interfacing with C programs or other executable programs?

string exec(string cmd, (optional array), (optional int))

where array is the total output from the called application; int is the status of the executed command.

the command line arguments of the application would be strings... well a 2D char array. argv[0] is the name of the application, argv[1-n] are the arguments seperated by white space.

exec() is for spawning external programs... they can be stand alone apps or shell commands such as dir. For example of usage, try

<?php

   $arr = array();
   $ret;
   
   unset($arr);

   echo exec('dir', $arr, $ret);

   print_r($arr);
?>

I'm having my doubts with the exec() command. I fixed my C program to force a return of 1. I'm still seeing a return of 0 from the exec(). I'm thinking that the return value is only a status of the execution - 0 mean successful execution.

Unless someone can shed some light on this I'm not sure what else to do.

post the code for your script and the C program... I can try it out, but can't guarantee anything.


Report •

#6
February 4, 2010 at 09:25:02
you bet - keep in mind, for testing purposes I'm forcing a value of 1 return regardless of the passed value. I need to
pickup the return "status" value in php or java, not the
status from the successful execution of exec().

C-CODE
static char *check[1] = {"123"};
int main(int argc, char *argv[])
{
int status = 0;

//printf( "argc = %d\n", argc);
//printf( "arg[0] = %s\n", argv[0] );
//printf( "arg[1] = %s\n", argv[1] );
//printf( "arg[2] = %s", argv[2] );
if(strcmp(check[0], argv[1]) == 0)
{
// should be set to 0 here, but can only get a return
// value of 0 when running from htm file (using exec()).
// So lets force a return of 1 regardless for testing
status = 1;
}
else
{
status = 1;
}

return status;
}

<?php
// no matter if I change to 221 or 123 or 545654, I always
// get a $res = 0 status - I'm not picking up the actual
// return value of status from the C-program. Not sure
// if I can do this or not. Note 221 shoud be a string
// and not an Integer. I never see the "error in program"
// display, unless I change echo $res to "error in program".
$res = exec('this should be the path of website/test.exe
221');

settype($res, integer);
if($res == 0)
{
echo $res;
}
else
{
echo "error in program";
}
?>


Report •

#7
February 4, 2010 at 12:49:36
Ok, it looks like what may be going on is you're returning an exit code to the operating system instead of outputting anything to stdout. The exec function is expecting an echo for the C program.

if(strcmp(argv[1], check[0]) == 0) {

   char buf[10] = { 0 };
   sprintf(buf, "%d", status);

   fflush(stdout);
   puts(buf);
} else {
   ...
}

Try something like that.

strcmp(input str to test, const string to check against)

HTH


Report •

#8
February 4, 2010 at 20:18:06
So my friend, the C code outputs a 0 or a 1 depending on what the argument is.

if argv[1] = 234, then a 0 is output
if argv[1] = anything else, then a 1 is output

the problem I'm having is the status of the command is always returning a 127. I checked permissions of my executable file and changed to 777 - still same thing. I tried simplifying the command by removing the argument, same thing. I tried using system() and passthru() and still the same thing. Do you see anything wrong with this code below?

char buf0[10] = { 0 };
char buf1[10] = { 0 };

int status0 = 0;
int status1 = 1;

if(strcmp(password[0], argv[1]) == 0)
{
sprintf(buf0, "%d", status0);

fflush(stdout);
puts(buf0);

}
else
{
sprintf(buf1, "%d", status1);

fflush(stdout);
puts(buf1);
}

<?php
$arr = array();
$ret;

unset($arr);

$res = exec('argpass.exe 124', $arr, $ret);

echo "
";
print_r($ret); -- output is 127

echo "
";
settype($res, integer);
settype($ret, integer);
settype($arr[0], integer);
settype($arr[1], integer);
settype($arr[2], integer);

echo "res: $res
"; -- output is 0
echo "ret: $ret
"; -- output is 127 (same as above)
echo "arr[0]: $arr[0]
"; -- output is 0
echo "arr[1]: $arr[1]
"; -- output is 0
echo "arr[2]: $arr[2]
"; -- output is 0
?>


Report •

#9
February 5, 2010 at 15:41:15
I tried a few things on my setup - Win2k running apache 2.2 and php 5.2. I could not replicate a return code of 127, but after milling over a few posts in various forums, *most* replies hint at either a "command not found" or "path not found" sort of error. If you have access to the server's error log, have you checked it?

Are you running this from a hosted site or a local machine? Are the exec, system, passthru functions enabled? Is php running in safe mode? If so, is the safe_mode_exec_dir set in the php.ini file? <?php phpinfo(); ?> will help in finding out.

my php.ini file for safe mode

safe_mode = On
...
safe_mode_exec_dir = "c:\www\htdocs\cgi-bin"

php script
echo exec('phpexec.exe 123');

safe mode off

safe_mode = Off
...
safe_mode_exec_dir =

php script
echo exec('c:\phpexec.exe 123');

On my machine, without safe mode enabled, the full path to C executable worked fine. With safe mode enabled, I set the exec dir to cgi-bin, and it worked using the file name only. :| this function is royal cluster in my opinion.

The C program looks ok, but for testing, I'd dump a string instead of an integral type.

fflush(stdout);
fprintf(stdout, "php data");

HTH


Report •

#10
February 7, 2010 at 16:52:07
A couple of days ago, I did check out the error.log and there were no errors. I'm running from a hosted site. I had contacted them also a couple of days ago, and they told me that exec(), passthru(), system() have been disabled for security reasons. I actually called another web hosting company, and they told me also, that that functionality is not allowed due to security reasons. Makes sense to me. If I was a naughty type of guy, I could easily write a virus and launch it as soon as someone enters my site - it wouldn't be hard to do.

Do you have any thoughts on this. After my investigation, error code 127 is a bogus error. No execution of a file never occurred. Many other people are experiencing the same thing from all the blogs I read, but they are scratching their heads like I was. Am I spinning my wheels on this one?

There's a php.php file in the webhost servier dirctory public_html and all that's in it is <?phpinfo();?>. What does this do? I'm assuming that the php.php file is similar to the php.ini file, but not sure? I've never tried editing this file, or think that I should.

Any ideas on this?

Enjoy the superbowl game



Report •

#11
February 7, 2010 at 17:04:47
Disregard this message. Isn't there a way to delete your last pos?

Report •

#12
February 7, 2010 at 22:51:15
I don't know if there's a mod on this board or not, but you can try PMing the admin to remove a post/reply, or maybe try the "report offensive" option along with an explanation.

phpinfo() is handy for showing the server's configuration. If you execute the script, you'll get all sorts of details about your site - what functions are disabled, extensions, path to your space, etc.

I've never tried using any of those functions on a hosted site, but it does make sense that they'd be disabled. Maybe the 127 is the status of the functions being diabled?

The Superbowl was pretty good though I didn't have a preference on who'd win.

Good luck to you on finding a viable solution. :)


Report •

#13
February 8, 2010 at 07:58:21
thanx for all your help and insight... Until we meet again

Report •


Ask Question