2

I have a .NET MAUI mobile-project and I am trying to call code that has been compiled from C++, namely an .armv8 file. I am trying the Process class to get this to work.

I have tried the following steps:

  1. Save the .armv8-file as an Embedded resource in my project and set Copy to output directory to Copy if newer. The file is in a folder in the mobile-project (so not in de platforms part of the project, but in a folder in the root of the project).

File settings

  1. Request the necessary file-permission (read & write) for Android:
PermissionStatus status = await Permissions.RequestAsync<Permissions.StorageRead>();
        PermissionStatus status2 = await Permissions.RequestAsync<Permissions.StorageWrite>();

These are granted successfully.

  1. Copy the file to the Android specific AppData folder, using the following code:
private string ExtractFiles()
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;

            foreach (var res in assembly.GetManifestResourceNames())
            {
                if (res.Contains(".armv8"))
                {
                    // read source file 
                    using var stream = assembly.GetManifestResourceStream(res);
                    using var reader = new StreamReader(stream);
                    var content = reader.ReadToEnd();

                    // generate target path inside data folder
                    string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, res);

                    // write file into data folder
                    using FileStream outputStream = System.IO.File.OpenWrite(targetFile);
                    using StreamWriter streamWriter = new StreamWriter(outputStream);

                    streamWriter.Write(content);

                    return targetFile;
                }
            }

            return string.Empty;
        }
  1. Tried to change the file-permissions to open with the following code (parameter is the output from step 2):
 public partial void SetPermissionToFile(string path)
        {
            string[] cmd = { "chmod", "744", path };
            Java.Lang.Runtime? runtime = Java.Lang.Runtime.GetRuntime();

            runtime?.Exec(cmd);
        }
  1. Starting a process with the file, with the following code:
_processStartInfo = new ProcessStartInfo
            {
                FileName = path,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };
            _process = new Process { StartInfo = _processStartInfo };
            _process.Start();

When the _process.Start() method gets called, I am getting a Permission denied error, looking like this:

Permission denied exception

Is this because I am using an emulator? Or is the file maybe in the wrong directory for the Process to work with?

I saw a similar post that got it worked out, but that does not seem to work for me yet: https://chess.stackexchange.com/questions/17685/how-to-send-uci-messages-from-c-app-to-stockfish-on-android/17856#17856?newreg=ea938bb4ac3147878e4fdc29626050cc

Thanks in advance for trying to help me :)

Ramon Brokking
  • 55
  • 2
  • 2
  • 10

1 Answers1

1

I have tested your code on a new .Net Maui project. Because I didn't have a .armv8 file so I used a pdf file instead of it. And on the android, the same error message appeared. But when I changed the UseShellExecute = false as true. It can excute on the windows.

Is this because I am using an emulator? Or is the file maybe in the wrong directory for the Process to work with?

I have tested it in the any other app's own folder and both on the physical xiaomi device and the Android 12 emulator. The results are all the same.

In addition, I created a new Xamarin.Android project according to this case about Access denied (0x80004005) on Process.Start() in android app. And then I get the accessed denied error. Eventhough I excuted the string[] cmd = { "chmod", "744", path }; command. I can't make sure the cause is the pdf file can't be granted the excutable permission or the chmod command can't work on the heigher android version.

I also tried to test the chmod 777 command and check the result with the android studio file explorer. And found the files permission wasn't changed. It seems the chmod command doesn't work any more.

Finally, you can try to test run the .armv8 file in a Xamarin.Android project according the link I provided and to check if it is a bug in the maui or not. You can also provide the file and I will test it tomorrow.

Update:

I have tried the .armv8 file with your code, the same error appeared. And I have checked the chmod command has been excuted successfully.

Then I create a new Xamarin.Android project and put the file into the /data/data/com.companyname.prjecttest/files/_stockfish.android.armv8 path with the Android Studio's File Explorer. And then I run the following code:

 var path = "/data/data/com.companyname.testprocess/files/_stockfish.android.armv8";
 string[] cmd = { "chmod", "744", path };
 Java.Lang.Runtime? runtime = Java.Lang.Runtime.GetRuntime();
 var res = runtime?.Exec(cmd).WaitFor();
// this line can check if the chmod command excute successfully.
// Value 0 means success. But I use the Android Studio's File Explorer, 
//and the file's permission is 777. The code need't to run.
 var _processStartInfo = new ProcessStartInfo
    {
      FileName = path,
      UseShellExecute = false,
      CreateNoWindow = false,
    };
 var _process = new Process { StartInfo = _processStartInfo };
 _process.Start();

There is no error message but also no any changes I can see. It seems a bug in the .net maui. You can try to report it on the github.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thank you for trying to help me. The file can be downloaded on https://stockfishchess.org/download/ and then scroll to Android section and download .armv8 file. So you are telling me that setting `UseShellExectute = true` could solve my problem? I think I already tried that and it did not work, but I will try again just to be sure. I do not see a link for the Xamarin.Android project you are referring to. – Ramon Brokking Feb 24 '23 at 10:00
  • You can check the updated part in the answer. @RamonBrokking – Liyun Zhang - MSFT Feb 27 '23 at 04:25
  • Could you give me a link to a github-repo that has your answer in it? So i can clone and easily test it – Ramon Brokking Feb 27 '23 at 08:21
  • But I put the file to the path by the Android Studio, not the code. And you can copy the code I provided above into a new Xamarin.Android project and use the code to copy the file to the path or use the Android Studio. @RamonBrokking – Liyun Zhang - MSFT Feb 27 '23 at 08:25
  • Do you think there is a way around the bug somehow? I really want to keep using .NET Maui and get this to work. All ideas are welcome! – Ramon Brokking Mar 01 '23 at 18:09
  • 1
    Sorry for my late reply. I can't find a way to resolve it because I even didn't know why this error appeared and what permission it needed. @RamonBrokking – Liyun Zhang - MSFT Mar 03 '23 at 01:33