Search code examples
phpc++execphp-extension

Executing shared library file from php script


I created shared library client.so. When I execute it main() function get executed which reads string from user and calls function ch=foo(buffer);.

Main.cpp is here:

#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;
    }

Using exec function we can execute c program. But in php script if I do it for this I won't have option to read string from user.

I want that user can enter the string in browser itself and I pass that string to main function(probably using argv,argc- but I dont know how to do this). Then main function should proceed as usual calling foo(buffer) and so on.

foo(buffer) will return array which is stored in char* ch I want to receive in php script.

I have two option: 1] Reading string in text box from user and passing it to shared library file which has main() function. Which get executed and return "ch" in php script 2] Executing shared library file (as we do in terminal ./client) and giving string input at run time and getting entire program run. But I dont know this is possible or not. #include #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;
    }

can anyone give me some suggestion for how this could be achieved!


Solution

  • After setting the permission execute in following manner will solve the problem:

    $a=exec('/home/technoworld/Videos/LinSocket/client "critic good"'); 
    

    No need to give ./client just like I was making mistake!

    client is the executable file that I wanted to execute and "critic good" is argument to be passed!