1

I have a method inside the cpp shared library which uses system() call to execute an android command. I have a cpp executable as well which uses this shared library and calls that particular method. When the executable calls the method inside shared library, the method successfully calls system() and I can see the android command ran successfully. Now, I have a xamarin app which also loads the same shared cpp library and calls the same method from the shared library. However, this time, the system() call fails and it can't execute the Android command. The Android command is to open browser

Why is it not working when called from Xamarin app? Do I need to provide any specific permissions to the app. I know that the app has permissions to access storage and internet.

std::string getName()
{
    std::streambuf* cout_sbuf = std::cout.rdbuf(); // save original 
    std::ofstream   fout("/sdcard/cout.txt");
    std::cout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout'
    std::string httpUrl = "https://www.google.com";
    std::string cmd = "/system/bin/am start -a 
        android.intent.action.VIEW -d \"" + httpUrl + "\"";
    std::cout<<"cmd passing to system(): "<< cmd <<std::endl;
    if(system(cmd.c_str())==0)
    {
        std::cout<<"system() returned successful. Return value: " << 
        system(cmd.c_str()) <<std::endl;
    }
    else
    {
        std::cout<<"system() returned failure. Return value: " << 
            system(cmd.c_str()) <<std::endl;
    }
    std::cout.rdbuf(cout_sbuf); // restore the original stream buffer
    return "return from getName()";
}

The above code works fine when we run a cpp exe from the command line. However, it won't work when the xamarin app calls this method. The return code of system() is 65280

  • Please show a [mre] of at least the command you're executing. `system` really isn't the right approach to launching a browser on Android though, you should use the correct intents API, e.g https://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application – Alan Birtles Nov 28 '22 at 07:30
  • You should avoid ever using `system()`. That function is a security *nightmare*. – Jesper Juhl Nov 28 '22 at 07:31
  • You should use the Android Intent system instead of trying to spawn a browser yourself – Botje Nov 28 '22 at 08:28
  • cmd passing to system(): /system/bin/am start -a android.intent.action.VIEW -d "https://www.google.com" This is what I am doing using system() – Ayyappa Kottha Nov 28 '22 at 08:36
  • I had to change my approach for now. I am doing the browser opening thing in the C# code instead inside the C++ lib. Once the browser is open, I am reading the responses inside the C++ lib – Ayyappa Kottha Dec 07 '22 at 09:21

0 Answers0