Search Unity

DriveInfo.GetDrives() crashes IL2CPP build

Discussion in 'Scripting' started by talofen, Apr 11, 2020.

  1. talofen

    talofen

    Joined:
    Jan 1, 2019
    Posts:
    40
    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using UnityEngine;
    4.  
    5. public class TestGetDrives : MonoBehaviour
    6. {
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyUp(KeyCode.P))
    12.         { GetDrives(); }
    13.     }
    14.     private void GetDrives()
    15.     {
    16.         try { Debug.Log("Trying GetDrives, found: " + DriveInfo.GetDrives().Length.ToString()); }
    17.         catch (Exception ex) { Debug.Log("DriveInfo.GetDrives exception: " + ex.Message); }
    18.     }
    19. }
    This script works flawlessly in the Editor and in Mono Builds, but if you build the app with IL2CPP, the app crashes whenever you hit P key.
    player.log reports the crash, but no exception is raised even though the GetDrives() call is within a try/catch block.

    Anyone has ideas?

    Tested on 2019.3.9f1 - windows target platform.
    Submitted as bug report, Case 1236557.
     
  2. talofen

    talofen

    Joined:
    Jan 1, 2019
    Posts:
    40
    Can anyone please confirm if it happens on my system only?
     
  3. yuichi9998

    yuichi9998

    Joined:
    Oct 25, 2017
    Posts:
    1
    I face the same issue now on Unity2019.4.6 and 2020.1.1....
    I haven't find any solution about this issue.
     
  4. benjamin_schmithuesen

    benjamin_schmithuesen

    Joined:
    Jul 23, 2020
    Posts:
    6
    CBHM likes this.
  5. dxmachina

    dxmachina

    Joined:
    Jul 4, 2012
    Posts:
    66
    Sorry to dig up an old topic, but I really need to be able to determine drive space in an IL2CPP project. Given the situation is there any known workaround?

    I've tried creating a managed plugin using DriveInfo, but this does not seem to survive compile (works in editor). Any ideas?
     
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Well your managed plugin still uses DriveInfo...

    If it's for mobile, you could create a native Plugin (in Java for Android, C++/Obj-C for iOS) that gets the remaining storage and use it instead of DriveInfo. For Android, it could be even simpler since you can call directly Java code from C#.
     
  7. dxmachina

    dxmachina

    Joined:
    Jul 4, 2012
    Posts:
    66
    Thanks for the thoughts.

    Unfortunately, I primarily need Desktop (Win/Mac). I've tried a few other solutions building native plugins in C++ and/or Obj-C but it's really a frustrating situation. Followed a bunch of random info out there without much luck (so far) but still grinding away.

    Fundamentally I just need to be able to check free space at a given location (in case app is not on main drive) in Win/Mac. It really feels like an oversight that this functionality is not available in IL2CPP.
     
  8. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Then just go the native OS API/C++ way. For Windows, it's by using Win32/PInvoke:

    Code (CSharp):
    1. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    2. [return: MarshalAs(UnmanagedType.Bool)]
    3. public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
    4. out ulong lpFreeBytesAvailable,
    5. out ulong lpTotalNumberOfBytes,
    6. out ulong lpTotalNumberOfFreeBytes);
    7.  
    8. // Usage, results in bytes
    9. GetDiskFreeSpaceEx(@"C:\", out var freeSpace, out var totalSpace, out var totalFreeSpace);
    For macOS I don't know the exact code, but it should be quite similar (import a native DLL and invoke a method)