473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to call C program function from PHP

hi frndz,
I need to call a C program function from php code, Is that
possible, If it is ,then tell me how ?
i.e,main()
{
int callfunction();
int i=2;
callfunction(i);
}
callfucntion(int i)
{
if(i==1)
return 2;
}

This is the C program which is in directory that have php
code too, So what i need is , From my php code ,i need to call the
callfunction() from that C program . Is that possible.
or else anyother way to call c function from php code
Rgrds
Dinesh..

Jan 25 '07 #1
3 10632
<ba*****************@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
hi frndz,
I need to call a C program function from php code, Is that
possible, If it is ,then tell me how ?
i.e,main()
{
int callfunction();
int i=2;
callfunction(i);
}
callfucntion(int i)
{
if(i==1)
return 2;
}

This is the C program which is in directory that have php
code too, So what i need is , From my php code ,i need to call the
callfunction() from that C program . Is that possible.
or else anyother way to call c function from php code

Not possible. You can't call a function inside another program written in
totally different language in php. What you can do is you can write a
program in c and call the program from command line with commandline
arguments and echo the output. Then you can in php call the program and read
the output using the built-in system functions like exec() or system() or
whatever.

Something like this:
in php:
echo system('myprogram 1');

myprogram.c:
// Apolgize my horrible C, it's been a million(?) years since i wrote C
main(argc, argv){
int i;
i = atoi(argv[1]);
j = callfunction(i);
printf('%d', j);
}

This is just a very raw example, but you'll get the basic idea, right?

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 25 '07 #2
balakrishnan.dinesh wrote:
I need to call a C program function from php code, Is that possible, If
it is ,then tell me how ?
As Kimmo said, you can't call a C function as-is. One option would be, as
he says, to compile it into a standalone executable, reading the
function's parameters from the command-line arguments, and then call the
compiled executable using a system() function in PHP.

However, if your C function wishes to receive an array or object parameter
you will probably need to compile the C function as a PHP extension. PHP
extensions become available as if they were normal built-in functions of
PHP. This method is a lot more complicated, but ultimately should provide
a faster and more stable solution. It makes it easier to link against a
large C library.

Help on creating extensions is here:
http://uk.php.net/manual/en/zend.creating.php

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 25 '07 #3
On Jan 24, 11:54 pm, "Kimmo Laine" <s...@outolempi.netwrote:
<snip>
Something like this:
in php:
echo system('myprogram 1');

myprogram.c:
// Apolgize my horrible C, it's been a million(?) years since i wrote C
main(argc, argv){
int i;
i = atoi(argv[1]);
j = callfunction(i);
printf('%d', j);

}This is just a very raw example, but you'll get the basic idea, right?

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
s...@outolempi.net | rot13(x...@bhgbyrzcv.arg)
Just a couple small nitpicks. Sorry, I've actually just begun writing
C, so I'm very engaged, and enjoy every opportunity to write in it. :-)
int main(int argc, char** argv)
{
int i,j;
i = atoi(argv[1]);
j = callfunction(i);
printf("%d", j); /* you need double quotes here, single quotes
are for characters only */
return 0;
}

Oh yes, something useful! If you want to store the output of a command
line call, it's pretty easy to do this:
<?php
$cli = `my_program arg1 arg2 arg3 ...`;
?>
Notice that back ticks are used. Also, this example assumes that your
program is in the PATH. If your program's in the same directory, and
you're on *nix, then you have to precede your program with "./".

--
Curtis

Jan 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dario | last post by:
This is a technical C++ post regarding the Microsoft runtime error R6025 Pure Virtual Function Call that sometime occurs in programs compiled with Microsoft Visual C++ 6.0. Please consider the...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
6
by: =?Utf-8?B?Sm9lbWFuYw==?= | last post by:
Hi, I'm trying to figure out the Callback method. I have a .dll that I call from a sub in my unmanaged code/calling program. That works just fine. But I'd like to have the .dll finish doing it's...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.