Using Windows APIs/Third Party DLLs from C# Unity Scripts

Greetings. I’ve got a windows only application we’ve been building in Unity and we’re continually running into problems using system resources or third party dlls that use system resources. Most of these, I believe, are because Unity rebuilds our scripts to run through mono, and uses the mono versions of various common assemblies, such as System.Drawing and System.Security. These assemblies are old and not compatible with the libraries we’re using, leading to problems.

One library fails in the System.Security DLL, run into nullrefs that cannot otherwise be explained. We have other code that uses system resources to read file thumbnails, which fails terribly in Unity but is fine otherwise.

Is there any way to run our scripts using the native CLR, or in any way run a sort of native C# setup? Our product is only targetting standalone windows builds at the moment, cross platform is obviously not expected for any such solution.

Worse case I suppose we can launch an entirely separate process and use IPC to communicate across the process boundary, but that makes debugging life massively more complicated and I’d much rather not have to go that far.

Thank you very much for any insight.

It’s frustrating, but somewhat understandable, you kinda need to treat Unity as it’s own platform, because that’s how Unity treat it.

You can get System.Drawing to work (I can’t recall how, but I’ve done it before for some project). There also use to be a nice list of what was supported in Unity and it’s compatibility with the official Mono runtime:

Sadly, they don’t seem to have maintained that since Unity 5. I really wish they’d start doing it again though. Of if they do still maintain such a list I can’t seem to find where it is.

:frowning:

This might sound like the most abysmal ever, but whenever I need to interface with windows APIs I write that code in straight C, and call the functions I want with extern. It feels nasty, but the winAPI is nasty, levels of abstraction always cause problems like this.

I’m assuming the nullrefs you are mentioning are return arguments in the api calls you want to perform? If they are, you might be SOL as far as getting a return in managed C#. If not, try passing an IntPtr.Zero.