Search Unity

Question Unity and WASM

Discussion in 'Web' started by brunostnnn, Feb 29, 2024.

  1. brunostnnn

    brunostnnn

    Joined:
    Feb 12, 2021
    Posts:
    1
    Hello! Good evening everyone.
    In unity I am loading an external c++/CX dll with the following code

    Code (CSharp):
    1.  
    2.         [DllImport("kernel32.dll", EntryPoint = "LoadLibrary", CharSet = CharSet.Auto, SetLastError = true)]
    3.         private static extern IntPtr Win32LoadLibrary([MarshalAs(UnmanagedType.LPTStr)] string lpLibFileName);
    4.  
    5.         [DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CharSet = CharSet.Auto, SetLastError = true)]
    6.         private static extern IntPtr Win32GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
    7.  
    8.         [DllImport("kernel32.dll", EntryPoint = "FreeLibrary", CharSet = CharSet.Auto, SetLastError = true)]
    9.         private static extern bool Win32FreeLibrary(IntPtr hModule);
    10.  
    11.         public override void Load(string path)
    12.         {
    13.             if (!string.IsNullOrEmpty(path))
    14.             {
    15.                 IntPtr hModule = Win32LoadLibrary(path);
    16.                 if (hModule != IntPtr.Zero)
    17.                 {
    18.                     Name          = Path.GetFileName(path);
    19.                     _nativeHandle = hModule;
    20.                 }
    21.                 else
    22.                 {
    23.                     throw new Exception($"Failed to load library at path '{path}' (ErrorCode: {Marshal.GetLastWin32Error()})");
    24.                 }
    25.             }
    26.             else
    27.             {
    28.                 throw new Exception("Library path is null or empty.");
    29.             }
    30.         }
    31.  
    32.         public override T GetFunction<T>(string functionName)
    33.         {
    34.             if (_nativeHandle != IntPtr.Zero)
    35.             {
    36.                 IntPtr procAddress = Win32GetProcAddress(_nativeHandle, functionName);
    37.                 if (procAddress != IntPtr.Zero)
    38.                 {
    39.                     return Marshal.GetDelegateForFunctionPointer<T>(procAddress);
    40.                 }
    41.                 else
    42.                 {
    43.                     throw new Exception($"Function '{functionName}' not found in library '{Name}'.");
    44.                 }
    45.             }
    46.             else
    47.             {
    48.                 throw new Exception($"Library not loaded, cannot get function '{functionName}'");
    49.             }
    50.         }
    51.  
    52.         public override void Free()
    53.         {
    54.             if (_nativeHandle != IntPtr.Zero)
    55.             {
    56.                 _ = Win32FreeLibrary(_nativeHandle);
    57.             }
    58.         }
    On Windows it works very well.
    However, my project is WEBGL and I would like to load the dll too, I've read on the forum that reading external files doesn't work, but all these dlls exist in JS and WASM, the question is how can I convert this dll code to import a file wasm? Is there a way to do this in unity?