【发布时间】:2013-07-04 05:43:46
【问题描述】:
我创建了共享库 client.so。当我执行它时 main() 函数被执行,它从用户读取字符串并调用函数ch=foo(buffer);。
Main.cpp 在这里:
#include<stdio.h>
#include "foo.h"
int main()
{
char buffer[1024];
char *ch;
printf("Client : \n");
printf("Enter sentence to send to the server (press enter)\n");
fgets(buffer, 1024, stdin);
ch=foo(buffer);
return 0;
}
使用 exec 函数我们可以执行 c 程序。但是在 php 脚本中,如果我这样做,我将无法选择从用户读取字符串。
我希望用户可以在浏览器本身中输入字符串并将该字符串传递给主函数(可能使用 argv,argc- 但我不知道该怎么做)。然后主函数应该像往常一样调用 foo(buffer) 等等。
foo(buffer) 将返回存储在 char* ch 中的数组,我想在 php 脚本中接收。
我有两个选择: 1] 从用户读取文本框中的字符串并将其传递给具有 main() 函数的共享库文件。哪个被执行并在 php 脚本中返回“ch” 2] 执行共享库文件(就像我们在终端./client 中所做的那样)并在运行时提供字符串输入并让整个程序运行。但我不知道这是否可能。 #包括 #include "foo.h"
int main()
{
char buffer[1024];
char *ch;
printf("Client : \n");
printf("Enter sentence to send to the server (press enter)\n");
fgets(buffer, 1024, stdin);
ch=foo(buffer);
return 0;
}
谁能给我一些关于如何实现这一点的建议!
【问题讨论】:
-
我会重写 C 程序以从命令行获取句子
-
@hek2mgl:这是个不错的选择,但在那之后呢?
-
之后调用它就像:
exec('path/to/yourprog "yoursentence"');.. !!不要错过之前使用escapeshellarg($sentence); -
@hek2mgl :我做了这些更改,但没有任何改进。我的代码: $s="hello";转义shellarg($s); $a=exec('/home/technoworld/Videos/LinSocket "./client $s"');客户端是我要执行的共享库文件!
-
@hek2mgl:很好的尝试!但给出同样的错误!
标签: php c++ exec php-extension