• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jmgek · Dec 10, 2016 at 12:33 AM · dll error

Quick help with my DLL exception, please.

Yeah yeah another one of these "DllNotFoundException" questions, First I just need some help compiling my scene and I'll be out of your hair.

I have personal licence for unity not professional so if that's the reason I can't link my dll you can stop and let me know.

If I can link dlls in unity 5.4 on a personal version please read below for the code:

I am building the DLL in visual studio 2015, in debug 64. Then dragging and dropping the dll in a plugins folder located inside another project directory.

I'm sorry, I have no idea why this image got so crazy... alt text

Console.cpp

 #include "Main.h"
 extern "C" {
     const char* Test()
     {
         return "Why don't you fkn work!?!?";
     }
 }

console.h

 #pragma once
 #define Main __declspec(dllexport) 
 
 extern *"C" {
     char Test();
 }

RunPlugin.cs

 using UnityEngine;
 using System.Runtime.InteropServices;
  
 public class RunPlugin : MonoBehaviour
 {
     // The imported function
     [DllImport("Console", EntryPoint = "Test")]
     public static extern void Test();
 
     void Start()
     {
         Test();
     }
 }


wut.png (102.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 10, 2016 at 12:06 PM

Two things:

  • First of all your dll is named "Console.dll" not "Console". So you're missing the ".dll" in side the DllImport attribute.

  • Second your method "Test" is "returning" a char pointer but you're extern definition in C# doesn't return anything. You should return a string and actually do something with the returned value:

Like this:

  using UnityEngine;
  using System.Runtime.InteropServices;
   
  public class RunPlugin : MonoBehaviour
  {
      // The imported function
      [DllImport("Console.dll", EntryPoint = "Test")]
      public static extern string Test();
  
      void Start()
      {
          Debug.Log("The dll returned: " + Test() );
      }
  }


ps: Also keep in mind since you marked that plugin as "Standalone" only you can't test this inside the editor. If you want to test inside the editor you have to enable "Editor" as well. If that plugin doesn't work on the editor platform for some reason (when you're using a different platform for development than for building), you would need a seperate implementation of the same thing for the editor if you want to test inside the editor.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 10, 2016 at 12:25 PM 0
Share

I just noticed some more "errors" in your actual DLL.

First of all in your header file your Test method is missing the "" but there is a "" one line above where there shouldn't be one.

Second you declared the macro "$$anonymous$$ain" but did not use it. The dll export declaration should be placed before your method definition.

  extern "C" {
      $$anonymous$$ain const char* Test()
      {
          return "Why don't you fkn work!?!?";
      }
  }

btw: macros usually are written in uppercase. Also "$$anonymous$$ain" is a bit misleading. You usually call it "EXPORT" or something like that:

 #define EXPORT __declspec(dllexport)
avatar image jmgek Bunny83 · Dec 12, 2016 at 07:12 PM 0
Share

Reached the limit on our other comments,

Yeah sorry, I was confused with the extern I since changed that. I'm still lost on why this is not working :/ After reading that link you gave me I tried what they suggested but I am sure I am building it correctly and passing c# a correct char*... I guess I'll try passing an int ins$$anonymous$$d later to see if it's the method.

avatar image jmgek · Dec 10, 2016 at 07:22 PM 0
Share

I greatly appreciate your help! I am just using this for testing but I will be changing my filename and $$anonymous$$acros, I changed up my code, and now I am getting an EntryPointNotFound (Least we're getting somewere!) also I changed the link settings to Editor x86. I also assume I don't need to include " extern "C" {" in my plugin?

alt text

main.cpp

 #include "main.h"
 
 $$anonymous$$AIN char* Test()
 {
     return "Why don't you fkn work!?!?";
 }
 

main.h

     #pragma once
     #define $$anonymous$$AIN __declspec(dllexport) 
     
     //extern *"C" {
     //    char* Test()
     //}
     $$anonymous$$AIN char* Test();

RunPlugin.cs

 using UnityEngine;
 using System.Runtime.InteropServices;
 
 public class RunPlugin : $$anonymous$$onoBehaviour
 {
     [DllImport("Console.dll", EntryPoint = "Test")]
     public static extern string Test();
 
     void Start()
     {
          Debug.Log("The dll returned: " + Test());
     }
 }
 


Again thank you so much!!!

screenshot-48.png (39.6 kB)
avatar image Bunny83 jmgek · Dec 11, 2016 at 06:32 AM 0
Share

You do need the extern "C", otherwise your exported symbols get mangled. The C++ compiler encodes metadata into each symbol name (things like parameter types, classnames, namespaces, ...). An extern "C" block exports the method as C method without name mangling.

That's the reason for your EntryPointNotFound exception. Your exported method name is not "Test" when you don't use extern "C". It depends on which compiler you used to create the DLL to figure out the actual mangled name but in general it's way easier to use extern "C".

avatar image jmgek Bunny83 · Dec 11, 2016 at 06:51 AM 0
Share

Thanks, but in the above code I'm not using Extern "C" so why am I still getting a entrypoint error?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

OpenCVSharp missing dll 1 Answer

How to use System.Threading.dll and System.Threading.Task.dll together? 0 Answers

Couldn't replace Library/.. dll 0 Answers

ArgumentException (System.Dynamic) . Microsoft.CSharp But the dll is not allowed to be included or could not be found. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges