Calling C++ native plugin without DllImport

I’ve seen Visual Studio projects where they have C# and C++ code and the C# code talks directly to the C++ without any DllImport calls. A class is declared in C++ and accessed from C# as if it was written in C#. I can’t seem to replicate this behavior in Unity. I have created a C++ dll, placed it into Plugins/WSA, but I get compilation errors. How can this be done?

The file that enables this is “.winmd” file, which should get created alongside a DLL file when you build your C++ code into a “Windows Runtime Component”. It contains the necessary metadata that describes your types in a way that .NET can understand and call into them. Did you place this file into your project?

Secondly, you can only do this in the Windows Store player - that will not work in editor. Did you not forget to wrap your code in #ifdefs?

2 Likes

Thanks, it’s working. Are the dll and winmd files the only critical files? Should I copy all of them?

Yes, they are the only files you need. You don’t need anything else other than “.dll” and “.winmd” files.

1 Like