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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] IL2CPP and Process.Start

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

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    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:
    372
    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:
    13
    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:
    12
    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.

    :(.
     
    KuraiAndras, 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:
    1,084
    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:
    1,084
    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:
    1,084
    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:
    1,084
    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:
    490
    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:
    1,084
    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:
    1,084
    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:
    1,084
  21. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Added.
    *fireworks*
     
    ModLunar likes this.
  22. x1alphaz1

    x1alphaz1

    Joined:
    Dec 19, 2020
    Posts:
    23
    @Zapan15 u r a superstar
    cheers


     
  23. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    47
    Ouch, it is insane that Unity has not implemented this so many years later.... especially given that now most mobile builds require them to be built using IL2CPP.

    Kamyker's solution is looking pretty good right about now.
     
  24. WalterAnthony

    WalterAnthony

    Joined:
    Apr 10, 2018
    Posts:
    8
    It's really regrettable. Is there any solution or alternative?
     
  25. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Did you actually read through the thread here?. On windows you can always use the usual WinAPI directly. The Process class is just a wrapper around that anyways. One potential solution was shown in post 44. That's why it's not a good idea to just post "thank you", "same", "any update?" or "that sucks" to a thread as people come to these threads just won't read through it because it gets too long. As a result people just pile up even more posts without any contribution.

    Literally two posts above yours someone quoted Zapan's solution. If you have a more concrete problem (for example how it may work on a specific platform) you should ask a separate question and include more details about what you want to do exactly. Add some background because others may need the same thing. However all host one-line comments are not really helping. We're at 75 posts now. Bumping old threads is never a good idea.
     
    MaskedMouse likes this.
  26. kei233

    kei233

    Joined:
    Dec 16, 2019
    Posts:
    49
    Actually "Application.OpenURL(filePath)" works for me,
    even if the filePath contains special characters like chinese.
    I tested explorer.exe to open folder, and another game build of mine.
    at: unity build 2021.3.23f1c1, windows 11
     
  27. Rainnnnnnnnnn

    Rainnnnnnnnnn

    Joined:
    Apr 19, 2022
    Posts:
    1
  28. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    For anyone wondering the plugin was confirmed to work on all listed platforms by several users. One rename is required for M2:
     
  29. louisdeane

    louisdeane

    Joined:
    Jan 16, 2013
    Posts:
    2
    Can I please just comment here how it's completely unacceptable that this isn't supported on windows in IL2CPP. Any kind of serious use of unity is completely prohibited on windows if you can't do basic interprocess interop. I'm sure some of the tasty new profit from the unity runtime fee could be used to support this going forward.

    "No, we don't have plans to implement this soon. We simple don't have the resources to work on it at the moment, sorry" gotta save a few hundred million dollars for your board ay?
     
  30. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Any kind of serious use? You do know that Unity is a game engine, right? Games very rarely must or should reach out to other applications on a users machine. Are there potential valid usecases? Yes, sure. Are those essential? Not at all. Are there workarounds? Yes there are. So your request has a very very low priority. Unity has shortcommings in many many other areas which actually affect most developers. Unity presents many highlevel abstractions and even lately they added a lot lowlevel APIs to many areas, there are still many which could receive an overhaul.

    What's your actual usecase? Something like a game launcher shouldn't be made in Unity. Inter process communication is still possible, sockets, pipes, window messages, and many others work just fine. So what's the thing you actually want to do?

    Note that the Process class comes with Mono and certain things aren't actually implemented in the Mono framework at all. IL2CPP means you actually compile natively as a C++ program and not a C# / managed project.
     
  31. louisdeane

    louisdeane

    Joined:
    Jan 16, 2013
    Posts:
    2
    Our application communicates with the ADB process to synchronise build versions across attached devices as well as the game client to kick off connected second screen experiences, a very valid use of interprocess communication and a game engine, especially in a multiplayer scenario. Our app gains performance advantages and interop advantages from using IL2CPP in other areas which leads to a trade off. It's not the fact it's unsupported it's the quite frankly completely flimsy argument given for not supporting what would be a pretty simple feature on the engine side and having to rely on app store plugins, natively using PInvoke or other workarounds.

    Also the site and recent shareholder messaging from Unity indicates that they intend to be "REAL-TIME TOOLS AND MORE" which is directly quoted from the site, so your statement "You do know that Unity is a game engine, right?" is actually not well aligned with what Unity is trying to be. If the true intentions of the engine is that it provides functionality above and beyond that of a game engine as appears to be the strategy, omission of such simple functionality as starting a process on an operating system isn't going to help them get there.
     
    Crazycarpet likes this.