Search Unity

c++ plugin problem

Discussion in 'Editor & General Support' started by cmorace, Aug 5, 2009.

  1. cmorace

    cmorace

    Joined:
    Jun 24, 2009
    Posts:
    2
    I'm having issues with my plugin recently. I think it might have to do with the upgrade to 2.51.

    All my existing plugin code works fine but any new plugin functions I try to use I get an EntryPointNotFoundException: (then the functions name)

    Any suggestions would be really appreciated. I am a student who has been working on a research project for the last 8 weeks. I can't make any progress until I fix this.

    Thanks
     
  2. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Did you setup your plugin to be STDCALL or CDECL or the export type?

    You can try to call it via _Method@# instead of Method
    The problem with 2.5.1 is that you do not know if the program will find your plugin method by alias, by ordinal or by standard so if your alias isn't setup and Unity can understand the alias or if you have duplicate alias, call by ordinal


    CalculateRoutine could be
    _CalculateRoutine or
    _CalculateRoutine@4 for instance

    Try the ordinal instead of alias, look at your .def file for the ordinal names.

    [sorry url tag broken at the moment.. go figure, just copy/paste this and go for it :)]
    http://msdn.microsoft.com/en-us/library/d91k01sh(VS.80).aspx
     
  3. cmorace

    cmorace

    Joined:
    Jun 24, 2009
    Posts:
    2
    I'm developing with a mac in xCode. The plugin is set up using a Carbon Bundle template.

    I can not find a .def file for the ordinal names. Is .def Windows specific?

    Thanks for the help
     
  4. lankoski

    lankoski

    Joined:
    Apr 20, 2008
    Posts:
    148
    Yes. In windows you need .def file or need to use __declspec().

    OSX works differently. Have you declared the functions as extern? E.g. something like this:

    Code (csharp):
    1. extern "C" {
    2.      float Foo();
    3. }
    I have newer tried to access class methods directly from Unity, so there might be some things you need to know.

    If you need to get the plugin work also in Windows you need something like this or use .def file:
    Code (csharp):
    1. extern "C" {
    2. #if (defined(WIN32) || defined(__WIN32__))
    3.    __declspec( dllexport ) float Foo();
    4. #else
    5.    float Foo();
    6. }
    7.  
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    also, how does your C# binding code look like if it differs in any way from already present functionality that works
     
  6. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Never mind, DllImport is just plain hosed for Carbon Bundles it looks like, I have been using (or attempting to use and failing) at C#.Net 2.0 DLL's (ticket submitted, and I think it is being worked on)

    Here is a XCode project and a Unity project to show that on 2.5.1f Pro, DllImport is just plain broke. So if you grab the 2 zips, open the XCode project and can point out what I have done wrong and open the Unity package up in Unity on the mac on 2.5.1f and see that it is saying DllImport not found reference, this shows that the problem spans a little farther than just me.

    Not sure how to help you solve this on the Mac, but this little exercise helped me to see that I am not totally insane about plugin issues.

    error:
    Assets/Scripts/UniTester.cs(5,10): error CS0246: The type or namespace name `DllImport' could not be found. Are you missing a using directive or an assembly reference?
     

    Attached Files:

  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is mainly one thing, but an elemental thing hossed:

    Code (csharp):
    1. using System.Runtime.InteropServices;
    is missing

    The other thing that is hossed is that your function isn't marked as static. Remember, you can only DLLImport normal C functions -> static

    With both things together it will be recognized and accepted by mono and will work correctly


    Additionally I would recommend to put the CS file in the plugins folder. While its not critical here, generally it will end with compile dependency issues when any other script try to use things from this script and it was not packed up to an assembly before.



    Generally, when working with plugins, be aware that any error in the assignement and marshaling will kill Mono instantanous. The kill of mono also means that unity will crash right away.
     
    LeGweg likes this.
  8. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Well, as I said, I mainly use C# and .Net framework 2.0 myself, I haven't bothered with Carbon Bundles directly in forever on the Mac.

    Making those changes made the error go away but it never did run my sum command. I'll look into that, but no error, no sum either. Not sure if this is related to his dll entry point not found error or not.

    Edit:
    Got it fixed, here is an example xcode project and unity project that works in 2.5.1f with dllimport (thanks again Dreamora for that junk I overlooked)

    Hopefully this helps you solve your dll import issues on entry point.
     

    Attached Files:

  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Sum worked for me, after I assigned the behavior to the game object.
    Before that it didn't work.


    And even if you work on C# with framework you need those two things, don't you? because the corresponding informations come straight from MSDN, more precisely http://msdn.microsoft.com/de-de/library/cc431203.aspx + the error message from Mono that static + extern are required )
     
  10. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Not if it is just native C# code, you don't need dllimport you simply just drag the dll into the asset folder and just use the class and its object, all work fine. It is only if you are referencing a non managed code assembly. You can create classes all day in C# and drag the DLL's into the Unity assets folder and simply use them. Works for Pro and Indie. Rather nicely really. (well typically it does)
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    C# don't compile to CDECL dlls (-> plugins), they compile to .NET assemblies. assemblies are not the same thing as bundle (elementally different actually). A bundle is the same as a CDECL dll created through C++ / C / ... on any OS.

    assemblies work on all OS by just dragging in, can be a .NET 2.0 assembly generated by mono or MS .NET actually
     
  12. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Correct.