Accessing An External Programm

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Accessing An External Programm

Post by neel_basu »

Hello Everybody
Would Anyone Please Help Me?? I Wanna Do The Following Thing?

I am Using Windows O/S I Have A .exe File That Takes A
String And Then do Some Job.
After That Prints Another String.
And Thats Made By C

And I Wanna Do The Following Job Through php

I wanna Run That .exe File in Background & Then Pass a
String To It After That I Wanna Get The Output Resultant String From The exe File.

So Anyone Please Tell Me How Can I Do It ??
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post by Corvin »

You can do this with exec().
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

There are many ways to get it done.
Please see these functions in the php manual:
1. exec()
2. shell_exec()
3. passthru()
4. system()
5. escapeshellcmd()
6. escapeshellarg()
7. proc_open()
8. proc_close()
9. proc_get_status()

Simple execution of program can be done by exec(), shell_exec() etc. Please see the manual and use according to your needs.

Cheers,
Dibyendra
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

PLease

Post by neel_basu »

Hello
Would You Please PLease Send Me A Sample Simple Code To Do This Job Under Windows And Linux.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post by Corvin »

Just have a look at the manual pages and the user comments. ;-)
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Actually I Have Looked Over The php.net Manual
But It Was Linux Based And I Dont Know Linux And I Didnt Find Any exec() Using An exe File So Please Tell Me Send Me A Sample Code
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Here Is My Codes
============
Suppose its My exe Programm made By C Codes

Code: Select all

#include <stdio.h>
#include <conio.h>

int x;

void main(void)
  {
     scanf("%d",&x);
     printf("%d",x*5);
  }

And Then Compile It And Then Build It To Make mult.exe

So How Can I Pass An Integer To This mult.exe Through php And Get The Result Integer On A Windows Server ??
Through php ?

Would You Please Send A Sample php Code ?? PLease Please
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

To be able to get the output fo the program executed, just do the following :

Code: Select all

$output =  shell_exe('mult.exe');
print $output;
In this case, mult.exe must be in the same directory where this script is executing.

Hope this is what you are looking for.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Then How Can I Pass The Value Of x To mult.exe !
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$output =  shell_exe('mult.exe xyz');
But your c program has to read the argument(s) - it does not yet.
see http://computer.howstuffworks.com/c38.htm
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

neel_basu wrote:Then How Can I Pass The Value Of x To mult.exe !
You must make the program to accept the arguments in the mail function of C program.

Your C program must be able to capture argv arguments. I guess that will solve your problem.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

I Read That On ('http://computer.howstuffworks.com/c38.htm')

Now Its My C Code
==============

Code: Select all

#include <stdio.h>

int main(int argc)
{
	printf("%d\n",argc);
    return 0;
}
And I Saved/Build It As pass_arg.exe

And Here Is The Out Put In cmd Line Start>run>cmd>
======================================

Code: Select all

C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe
1

C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe 54
2

C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe 25654
2

C:\Documents and Settings\Neel_Zit\Desktop\Debug>

I Compiled It With Microsoft VC++
And Its Not Giving The Expected Result
It Should Output 54 If I Pass Argument 54 But It Makes An Output Of 2 Always
If There Is Any Arguments Passed To It Else The Output is 1

Please Tell Me How I Can Solve This Problem !!!!!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm not sure what you've read but http://computer.howstuffworks.com/c38.htm shows something different
http://computer.howstuffworks.com/c38.htm wrote:int main(int argc, char *argv[])
neel_basu wrote:int main(int argc)
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Sorry Sir
Can You Tell Me A Sample Output Of That Programm ??

I Compiled That Programm With That Code in That
===================================

Code: Select all

#include <stdio.h>

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

	printf("%d\n",argc);
	for(x=0;x<argc;x++)
		printf("%s\n",argv[x]);
    return 0;
}
But When I Build That Programm It Was Its Output
======================================

Code: Select all

C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe 45 abcd
3
pass_arg.exe
45
abcd
But I Dont Think It Should Be The Output Of That Programm
I Think It Should Print
===============

Code: Select all

45
a
b
c
d
While Entering
============

Code: Select all

C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe 45 abcd
In Commandline
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, the arguments are separated by whitespaces.

argument #0 is the program name
45 is argument #1
abcd is argument #2
-> 3 arguments
The output
C:\Documents and Settings\Neel_Zit\Desktop\Debug>pass_arg.exe 45 abcd
3
pass_arg.exe
45
abcd
is correct
Post Reply