Answers.Body: <p>Finally I made it work with big help of @SmallChess and @Andrew. The first problem came with file <code>stockfish-8-armeabi-v7a</code> with no extension and not much informations about. That's why I asked here. The answer by SmallChess was precise, I just couldn't make things work and I couldn't confirm it for very long time. I run into various problems, more or less easy to deal with, but this one took a lot of time:</p>
  
  <blockquote>
    <p>I correctly copied the file to folder of my application, but I didn't have permission to run it.</p>
  </blockquote>
  
  <p>So I had to change it with</p>
  
  <pre><code>string[] cmd = { "chmod", "744", Path.Combine(strToFolder, fileName) };
  Java.Lang.Runtime.GetRuntime().Exec(cmd);
  </code></pre>
  
  <p>and things started to be very easy from now, as Andrew's answer provides fully functional code, which would take also a lot of time to write with not much understanding of the problem, so big thanks to him too.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-18T16:07:03.533Z
Answers.LastActivityDate: 2017-06-18T16:07:03.533Z
Answers.UserDisplayName: hoacin
Answers.Body: <p>When you're using C#, you can use any UCI engine in the same manner.  With C# you have to add an event handler to the <code>OutputDataReceived</code> event (and optionally the <code>ErrorDataReceived</code> event).  As a quick example:</p>
  
  <pre><code>ProcessStartInfo si = new ProcessStartInfo() {
      FileName = uciPath,
      UseShellExecute = false,
      CreateNoWindow = true,
      RedirectStandardError = true,
      RedirectStandardInput = true,
      RedirectStandardOutput = true
  };
  
  myProcess = new Process();
  myProcess.StartInfo = si;
  try {
      // throws an exception on win98
      myProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
  } catch { }
  
  myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
  
  myProcess.Start();
  myProcess.BeginErrorReadLine();
  myProcess.BeginOutputReadLine();
  
  SendLine("uci");
  SendLine("isready");
  SendLine("ucinewgame");
  </code></pre>
  
  <p>And the methods alluded to above:</p>
  
  <pre><code>    private void SendLine(string command) {
          myProcess.StandardInput.WriteLine(command);
          myProcess.StandardInput.Flush();
      }
  
      private void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) {
          string text = e.Data;
          //Debug.WriteLine("[UCI] " + text);
      }
  </code></pre>
  
  <p>This is an incredibly simplified example.  In my own code I have a semaphore in order to fully process each output line before moving on to the next one and several semaphores in the UCI start and "isready" handling.</p>
  
  <p>For a full overview of the UCI protocol, <a href="http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm" rel="nofollow noreferrer">check out the specification</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-02T05:34:14.257Z
Answers.Text: I think the OP is asking about his "missing file" problem. Look at the comment he posted to my answer.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-02T05:37:09.420Z
Answers.Text: @SmallChess yes, looks like that is the biggest issue - I didn't see a way for the OP to actually capture the output so I figured I would add the code snippet anyways.  Cheers!
Answers.UserDisplayName: Andrew
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-31T20:22:44.823Z
Answers.Text: Hey! I have tried your code with `Debug.WriteLine()` and `Console.WriteLine()` but I do not receive any information back from the engine. How to solve this? Thanks!
Answers.UserDisplayName: Michael Haddad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-02T05:32:30.997Z
Answers.LastActivityDate: 2017-06-02T05:32:30.997Z
Answers.UserDisplayName: Andrew
Answers.Body: <p>You're using the compiled Stockfish binary so you don't need to worry anything about C++. You only need to think about C++ if you're compiling the Stockfish source code yourself (but you're not).</p>
  
  <p>The link you have is about calling a DLL in C# - this has nothing to what you want to do. The Stockfish program you have is independently compiled, and has nothing to do with Microsoft's DLL implementation.</p>
  
  <p>You're looking for this:</p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output">https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output</a></p>
  </blockquote>
  
  <p>You should start a new engine process, run it and communicate with the engine by UCI protocol.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I see you have started +100 bounty, but I think it's a <strong>mistake</strong>. I believe my answer is accurate but I'm unable to debug your environment. Your problem is the unknown file path. Have you added a text file, and try to read it in your app? If you can't load a text file in your program, you can't start a chess engine. Please give a go.</p>
  
  <ul>
  <li>Save a new text file to where you have the Stockfish engine</li>
  <li>Try to read the text file</li>
  <li>You may want to do it on a simulator. On your PC simulator, you can use path like <code>C:\Engine\SF_engine</code>.</li>
  </ul>
  
  <p>Your technical difficulty has nothing to do with chess engine or UCI protocol, and thus off-topic here.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-31T04:50:18.083Z
Answers.Text: I tried that and still fighting with the technical part. If you could please look at this question: https://stackoverflow.com/questions/44267734/how-to-run-compiled-binary-file-on-android
Answers.UserDisplayName: hoacin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-31T04:51:43.170Z
Answers.Text: @hoacin Try full path?
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-01T05:14:25.633Z
Answers.Text: When I look at file properties, it is located on PC somewhere in project folder. It doesn't work, PC folder shouldn't work from Android app. Must the file be part of the project? If I copy the file to phone to folder with other documents my app use and then use that folder, can it work? No need to be compiled with my app?
Answers.UserDisplayName: hoacin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-02T05:50:02.057Z
Answers.Text: @hoacin You'll need to debug, that's part of programming. If you need code-level assistance, you may email me privately for paid consultation. Google my contact details if you're interested.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-30T01:28:35.673Z
Answers.LastActivityDate: 2017-06-02T05:48:44.560Z
Answers.LastEditDate: 2017-06-02T05:48:44.560Z
Answers.UserDisplayName: SmallChess
Body: <p>I downloaded the binary of Android Stockfish arm64-v8a and looked in the CPP source files. In main() there is all initialization, and then there is UCI::loop(), which catches standard input with getline() and writes back with sync_cout(). How can I write to the engine from a c# (xamarin) application? And is it necessary to make some additional initialization?</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/1579074/redirect-stdoutstderr-on-a-c-sharp-windows-service">this</a>,
  which could be close to my problem, but maybe it is not. I don't want to spend three days just to see I'm doing complete nonsense.</p>
  
  <p>Also I don't know how to recognize Stockfish's console. Is there only one console, no matter how many different libraries the program can use?</p>
  
  <p>As I don't understand the technical part much, I'm including a screenshot of what I expect to be the Stockfish I should communicate with :-). If it is wrong from the beginning, please let me know.</p>
  
  <p><a href="https://i.sstatic.net/iNCVm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iNCVm.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT</strong>: What file type is the stockfish-8-arm64-v8a, is it android native library or native shell executable or something else? Where to put the file, how to set it up (in Visual Studio 2015), how to call it from code? I have some code that I tried <a href="https://stackoverflow.com/questions/44267734/how-to-run-compiled-binary-file-on-android">here</a> but still I can't make it work.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-02T05:50:40.477Z
Comments.Text: Have you tried changing "Copy to Output Directory" to "Copy" instead of "Do not copy"?  That's a common "gotcha" in Visual Studio.  Then you can use relative file paths to reference the exe
Comments.UserDisplayName: Andrew
Tags: <engines><stockfish><programming>
Title: How to send uci messages from c# app to stockfish on android?
ThreadText: # How to send uci messages from c# app to stockfish on android?
  
  (Asked by: username_0 on 2017-05-29)
  
  I downloaded the binary of Android Stockfish arm64-v8a and looked in the CPP source files. In main() there is all initialization, and then there is UCI::loop(), which catches standard input with getline() and writes back with sync\_cout(). How can I write to the engine from a c# (xamarin) application? And is it necessary to make some additional initialization?
  
  I found [this](https://stackoverflow.com/questions/1579074/redirect-stdoutstderr-on-a-c-sharp-windows-service),
  which could be close to my problem, but maybe it is not. I don't want to spend three days just to see I'm doing complete nonsense.
  
  Also I don't know how to recognize Stockfish's console. Is there only one console, no matter how many different libraries the program can use?
  
  As I don't understand the technical part much, I'm including a screenshot of what I expect to be the Stockfish I should communicate with :-). If it is wrong from the beginning, please let me know.
  
  [![enter image description here](https://i.sstatic.net/iNCVm.png)](https://i.sstatic.net/iNCVm.png)
  
  **EDIT**: What file type is the stockfish-8-arm64-v8a, is it android native library or native shell executable or something else? Where to put the file, how to set it up (in Visual Studio 2015), how to call it from code? I have some code that I tried [here](https://stackoverflow.com/questions/44267734/how-to-run-compiled-binary-file-on-android) but still I can't make it work.
  
  **Tags:** engines, stockfish, programming
  
  > **Comments:**
  > **username_1**: Have you tried changing "Copy to Output Directory" to "Copy" instead of "Do not copy"?  That's a common "gotcha" in Visual Studio.  Then you can use relative file paths to reference the exe  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2017-06-18. Score: 1 | Accepted answer
  
  Finally I made it work with big help of @username_2 and @username_1. The first problem came with file `stockfish-8-armeabi-v7a` with no extension and not much informations about. That's why I asked here. The answer by username_2 was precise, I just couldn't make things work and I couldn't confirm it for very long time. I run into various problems, more or less easy to deal with, but this one took a lot of time:
  
  > I correctly copied the file to folder of my application, but I didn't have permission to run it.
  
  So I had to change it with
  
  ```
  string[] cmd = { "chmod", "744", Path.Combine(strToFolder, fileName) };
  Java.Lang.Runtime.GetRuntime().Exec(cmd);
  
  ```
  
  and things started to be very easy from now, as username_1's answer provides fully functional code, which would take also a lot of time to write with not much understanding of the problem, so big thanks to him too.
  
  
  ---
  
  ### Answer by username_1 on 2017-06-02. Score: 3
  
  When you're using C#, you can use any UCI engine in the same manner. With C# you have to add an event handler to the `OutputDataReceived` event (and optionally the `ErrorDataReceived` event). As a quick example:
  
  ```
  ProcessStartInfo si = new ProcessStartInfo() {
      FileName = uciPath,
      UseShellExecute = false,
      CreateNoWindow = true,
      RedirectStandardError = true,
      RedirectStandardInput = true,
      RedirectStandardOutput = true
  };
  
  myProcess = new Process();
  myProcess.StartInfo = si;
  try {
      // throws an exception on win98
      myProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
  } catch { }
  
  myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
  
  myProcess.Start();
  myProcess.BeginErrorReadLine();
  myProcess.BeginOutputReadLine();
  
  SendLine("uci");
  SendLine("isready");
  SendLine("ucinewgame");
  
  ```
  
  And the methods alluded to above:
  
  ```
      private void SendLine(string command) {
          myProcess.StandardInput.WriteLine(command);
          myProcess.StandardInput.Flush();
      }
  
      private void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) {
          string text = e.Data;
          //Debug.WriteLine("[UCI] " + text);
      }
  
  ```
  
  This is an incredibly simplified example. In my own code I have a semaphore in order to fully process each output line before moving on to the next one and several semaphores in the UCI start and "isready" handling.
  
  For a full overview of the UCI protocol, [check out the specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm).
  
  > **Comments:**
  > **username_2**: I think the OP is asking about his "missing file" problem. Look at the comment he posted to my answer.  
  > **username_1**: @username_2 yes, looks like that is the biggest issue - I didn't see a way for the OP to actually capture the output so I figured I would add the code snippet anyways.  Cheers!  
  > **username_3**: Hey! I have tried your code with `Debug.WriteLine()` and `Console.WriteLine()` but I do not receive any information back from the engine. How to solve this? Thanks!  
  
  ---
  
  ### Answer by username_2 on 2017-05-30. Score: 2
  
  You're using the compiled Stockfish binary so you don't need to worry anything about C++. You only need to think about C++ if you're compiling the Stockfish source code yourself (but you're not).
  
  The link you have is about calling a DLL in C# - this has nothing to what you want to do. The Stockfish program you have is independently compiled, and has nothing to do with Microsoft's DLL implementation.
  
  You're looking for this:
  
  > <https://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output>
  
  You should start a new engine process, run it and communicate with the engine by UCI protocol.
  
  **EDIT:**
  
  I see you have started +100 bounty, but I think it's a **mistake**. I believe my answer is accurate but I'm unable to debug your environment. Your problem is the unknown file path. Have you added a text file, and try to read it in your app? If you can't load a text file in your program, you can't start a chess engine. Please give a go.
  
  * Save a new text file to where you have the Stockfish engine
  * Try to read the text file
  * You may want to do it on a simulator. On your PC simulator, you can use path like `C:\Engine\SF_engine`.
  
  Your technical difficulty has nothing to do with chess engine or UCI protocol, and thus off-topic here.
  
  > **Comments:**
  > **username_0**: I tried that and still fighting with the technical part. If you could please look at this question: https://stackoverflow.com/questions/44267734/how-to-run-compiled-binary-file-on-android  
  > **username_2**: @username_0 Try full path?  
  > **username_0**: When I look at file properties, it is located on PC somewhere in project folder. It doesn't work, PC folder shouldn't work from Android app. Must the file be part of the project? If I copy the file to phone to folder with other documents my app use and then use that folder, can it work? No need to be compiled with my app?  
  > **username_2**: @username_0 You'll need to debug, that's part of programming. If you need code-level assistance, you may email me privately for paid consultation. Google my contact details if you're interested.  
  
  ---
  
  