Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[Solved] IL2CPP and Process.Start

Discussion in 'Scripting' started by sebas77, May 31, 2018.

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    I could make paid asset store package that will work on few platforms. What methods and platforms do you the most?
     
    ModLunar likes this.
  2. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    323
    Thanks anyways for communicating with us :) I really appreciate it
     
  3. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    Just launching a process on Mac and Windows would be huge.
     
  4. Erveon

    Erveon

    Joined:
    Sep 15, 2019
    Posts:
    11
    Didn't see this response until now. We just need this to work on Windows for our use case.
    Note that there is a package that we bought before but really all it did was call OpenURL behind the scenes which does not solve it. The package would need to call native C++ to actually work properly. Here's the code where we use it.

    I would definitely spend money on this. Apparently we have more resources than Unity :)
     
    ModLunar likes this.
  5. Aiwownomt

    Aiwownomt

    Joined:
    Apr 13, 2020
    Posts:
    6
    自版本 2021.3.5 起,此问题仍未解决
     
  6. Sponge2k

    Sponge2k

    Joined:
    Sep 22, 2021
    Posts:
    4
    Things like these can be so cumbersome, that for once I am going to mention a bit of a frustration here. Using Unity already since 2009, I have come to accept that things can take some time. It is a complicated product after all. It just takes a long time to work around it, but in the end, it is manageable. I do a lot of low level things all the time and unsafe memory functions are part of a regular day.

    However, I am starting to develop a low tolerance for things that work PERFECTLY in the editor, and no mention in Visual Studio about that some features are not available in the IL2CPP. So first you spend hours getting something to work well, and then to compile to not get it to work - for something trivial AND important as launching a process. Then you start trying to debug, compile many times, to no success.

    Then you find on a forum that CreateProcess is not supported in IL2CPP. For already three years. In those three years there has been no time to at least mention it in the VS warning list? Or during compilation? So much hours of frustration could be saved when this would be more common practice at Unity. It happens a lot with IL2CPP that you run into unexpected issues.

    Now, starting processes on the background is a rather important feature for a lot of (non) gaming applications. So, here I am, back to the Win32API days trying to start processes and redirecting standard output. Every single test Unity will be stuck and there is no way to debug why it is exactly going wrong. For a feature that was perfectly working. In the editor.

    :(.
     
    peq42 and Kurt-Dekker like this.
  7. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    I just find it really incredible that the asset store hasn't come up with some simple API.

    Do you reckon people would pay for a simple command-line launching API for unity if I made one?
     
  8. lumeneo

    lumeneo

    Joined:
    Mar 3, 2014
    Posts:
    60
    Yes, I need this badly! For Mac, Windows, and Linux(?)
     
  9. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    Yeah, I think it'd have to be.

    I'll have to look into how difficult it'd be (definitely no promises just yet)
     
  10. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    I'm making almost 1:1 direct replacement for whole System.Diagnostics.Process. So far have this working:
    Code (CSharp):
    1.  
    2. using KS.Diagnostics;
    3. (..)
    4. IEnumerator Start()
    5. {
    6.     yield return new WaitForSeconds(1);
    7.     var consolePath = Path.Combine(Directory.GetCurrentDirectory(),"NativeLibraryConsoleTest.exe");
    8.     using var proc = new Process()
    9.     {
    10.         StartInfo = new()
    11.         {
    12.             FileName = consolePath,
    13.             Arguments = "",
    14.             UseShellExecute = false,
    15.             RedirectStandardOutput = true,
    16.             //RedirectStandardError = true,
    17.             CreateNoWindow = true,
    18.         }
    19.     };
    20.     // proc.OutputDataReceived += (obj, s) => text.text += s;
    21.     proc.OutputDataReceived += OutputDataReceivedWrapper;
    22.    
    23.     proc.Start();
    24.     proc.BeginOutputReadLine();
    25.     proc.WaitForExit(1000 * 90); //90sec
    26.     proc.CancelOutputRead();
    27. }
    28. [MonoPInvokeCallback(typeof(DataReceivedHandler))]
    29. public static void OutputDataReceivedWrapper(string s)
    30. {
    31.     //run on Unity main thread
    32.     Dispatcher.Invoke(() => field.text += Environment.NewLine + s );
    33. }
    I have a bit of cross-platform solution. Main platform will be win x64 but it may also work on win arm64, linux x64/arm64, osx x64. I don't have mac so can't compile there but could send source to Unity to compile and test it.

    Next, I'll try sending lambda's delegates pointers back and forth instead of static MonoPInvokeCallback as it's a bit painful to use.
     
  11. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    Interesting, can you help me understand how this resolves the issue that System.Diagnostics.Process won't compile to IL2CPP?
     
  12. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    It uses native dll under the hood and c# wrapper, System.Diagnostics.Process isn't used at all:
    Code (CSharp):
    1. namespace KS.Diagnostics
    2. {
    3. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    4. public delegate void DataReceivedHandler([MarshalAs(UnmanagedType.LPWStr)] string value);
    5.  
    6. public class Process : IDisposable
    7. {
    8.     [DllImport("KS_Diagnostics_Process")]
    9.     static extern IntPtr CreateProcess();
    10.  
    11.     public Process()
    12.     {
    13.         ptr = CreateProcess();
    14.     }
    15.  
    16.  
    17.     [DllImport("KS_Diagnostics_Process")]
    18.     static extern void Start(IntPtr ptr);
    19.  
    20.     public void Start()
    21.     {
    22.         Start(ptr);
    23.     }
    24.  
    25.     [DllImport("KS_Diagnostics_Process")]
    26.     static extern void BeginOutputReadLine(IntPtr ptr);
    27.  
    28.     public void BeginOutputReadLine()
    29.     {
    30.         BeginOutputReadLine(ptr);
    31.     }
    32.  
    33.     [DllImport("KS_Diagnostics_Process")]
    34.     static extern void WaitForExit(IntPtr ptr);
    35.  
    36.     public void WaitForExit()
    37.     {
    38.         WaitForExit(ptr);
    39.     }
    40.  
    41. (...)
    By saying "So far have this working" I meant that's what I implemented in native dll.
     
  13. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    Submmited package to asset store (win, linux), also found a way to build for osx with cloud Ill try in few days. Let me know if anyone wants to test if it works correctly and in the result get free version for Mac/osx :).
     
    Last edited: Sep 14, 2022
  14. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    Good news, @cyriaca helped me testing Osx version and x64 looks to be working. I may be able to build osx arm64 in few weeks.

    In the end package will support:
    Windows x64
    Linux x64 (experimental)
    Osx x64 (experimental)
    Osx arm64 (experimental)

    Release once Unity approves the asset (in ~3 weeks).
     
  15. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    480
    Finally someone creating a solution to this ^^
    It's a feature I was using until IL2CPP came around and had to remove features that required it..
     
  16. syler222

    syler222

    Joined:
    Mar 10, 2022
    Posts:
    2
    So nice to hear that! I belive many of us are going to buy it. Could you please share package url?
     
  17. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    I think url isn't generated before first package release. I'll try to make Github sponsors release but validating account takes a week there.
     
  18. syler222

    syler222

    Joined:
    Mar 10, 2022
    Posts:
    2
    Awesome! Waiting :)
     
  19. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    Well, it took them less than 24h, here it is: https://github.com/sponsors/kamyker

    Let me know if anyone requires more implemented methods/properties here or on Discord.

    Oh and ignore that it's "monthly" payment, ill add everyone permanently. Github didn't implement one-time payments that give access to private repos.
     
    Last edited: Sep 20, 2022
    Pavlik228_322 and Anthiese like this.
  20. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
  21. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    927
    Added.
    *fireworks*
     
    ModLunar likes this.