Search Unity

Bug Trying to access a function in an external c++ windows dll results in an editor assert

Discussion in 'Editor & General Support' started by mgoemmel, Dec 27, 2022.

  1. mgoemmel

    mgoemmel

    Joined:
    Sep 16, 2022
    Posts:
    10
    Hello,

    I'm trying to create and use an external windows unmananged c++ dll. I've tried to create a simply dll with just one function, and it worked immediately. Then I added some more of my c++ functions, without even exporting them, so the final dll just have the single export function the original one without my additional functions also have (I've checked this with a dll dependency walker). When trying to access this function with the extended version of the dll in the editor, this assert message appears:

    Program: C:\Program Files\Unity\Hub\Editor\2021.3.10f1\Editor\Unity.exe
    File: minkernel\crts\ucrt\src\appcrt\stdio\fgetc.cpp
    Line: 43
    Expression: stream.valid()

    Any idea what could be wrong here? Anything I can try with my dll (it's not an external one, so I can try whatever is needed).

    Thanks for any help or hint.

    Regards
    Markus
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    When the editor mounts a DLL (eg, by running your DLLImport lookup), it's mounted. Subsequent changes won't be possible.

    To recompile native mode DLLs and interoperate with Unity, you have to close the Unity editor and rebuild, then reopen.

    There is an alternate way to dynamically mount and unmount the DLL but I haven't fiddled with it personally, but I've heard some folks use it to good effect.
     
  3. mgoemmel

    mgoemmel

    Joined:
    Sep 16, 2022
    Posts:
    10
    Thanks for your reply. Unfortunately this assert also happens with a reopened Unity editor, I even tried deleting the dll from assets, closing Unity, restarting Unity, adding the dll again, and start the application. Always the same crash.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Attach a debugger and then click "Retry". It's probably your code within that DLL crashing.
     
    mgoemmel likes this.
  5. mgoemmel

    mgoemmel

    Joined:
    Sep 16, 2022
    Posts:
    10
    You are completely correct. I'm a bit ashamed to not mentioned it myself, but the source files are not my own code but from a friend, and I didn't know that the source instantiates a class as a global variable, running through code I didn't expect.

    Now, after fixing the problem which asserted, I unfortunately face another problem. My code

    [DllImport("bgblitz_win.dll")] static extern int plusone(int number);
    int six = plusone(5);
    Debug.Log("six is " + six);

    results in

    DllNotFoundException: bgblitz_win.dll assembly:<unknown assembly> type:<unknown type> member:(null)
    Board+<_gamePlayStateMachine>d__152.MoveNext () (at Assets/GamePlay.cs:441)

    line 441 is the "int six = plusone(5);" line.

    I searched the internet and found some stuff which should help, so I already tried the following:

    * I've compiled my dll with /MT parameter
    * I use a release version of the dll
    * I installed the latest VS Redistributable 2019 x64

    Also I had a dependency look, and my dll depends on kernel32.dll, which I tried to move into the same asset folder my dll is located (the syswow64 version).

    Any idea what could be wrong here. Do I maybe search in the wrong direction.

    Thank you very much for your reply, I am aware that it is in no way normal to receive support from a unity member itself.

    Regards
    Markus
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Do not move system DLLs into the project folder. It will just make things break. When facing DLL load failures, I recommend using "loader snaps" to debug them:


    1. Install Windows SDK with "Debugging tools for Windows" component from https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/;
    2. Open admin command prompt;
    3. Type "C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\gflags.exe -i <game_exe_name>.exe +sls". Note that "<game_exe_name>" is just the name, not the full path to the executable. Use Unity.exe if you are trying to debug Unity editor;
    4. Run it under a debugger;
    5. Look at the debugger output. It should tell you exactly why it cannot load the DLL.
    6. When done, disable loader snaps with "gflags.exe -i <game_exe_name>.exe -sls".
     
    mgoemmel likes this.
  7. mgoemmel

    mgoemmel

    Joined:
    Sep 16, 2022
    Posts:
    10
    Ok, I found the problem by creating a Windows standalone version with the flag "Create Visual Studio solution". This time I was able to debug into the dll routines finding another problem in the dll init code resulting in a throw, which stopped dll loading. So it never was a problem of dll loading, but a classical programmer stupid error.

    Thanks for your time and suggestion. Excellent support!

    Regards
    Markus