Search Unity

Calling C++ native plugin without DllImport

Discussion in 'Windows' started by User340, Mar 19, 2017.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    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?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,678
    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?
     
    drvignesh and User340 like this.
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks, it's working. Are the dll and winmd files the only critical files? Should I copy all of them?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,678
    Yes, they are the only files you need. You don't need anything else other than ".dll" and ".winmd" files.
     
    User340 likes this.