Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
3
Question by Jerdak · Jul 31, 2012 at 10:01 PM · androideditordllunload

Easier way to handle unloading dlls?

It is my understanding that Unity will not unload a .dll once 'Preview' has been run, if you use [DllImport]. Following the linked post's suggestions I instead used Kernel.dll to load and unload my dll manually. That part works fine.

The issue is that my code needs to be compatible with Android as well so I can't very well link Kernel.dll to do manual loading and unloading. Once I deploy my app I know I can just use [DllImport] and things will work as expected but I need to retain the manual loading and unloading so that Unity's editor doesn't lock my .dll forcing me to close Unity when I need to recompile my C++ DLL.

The following code is the best way I could think of to handle DLL loading on different systems while still allowing me to dynamically load and unload my DLL. Is there an easier way?:

 using System;
 using System.Collections;
 using System.Runtime.InteropServices;

 using UnityEngine;
 public class Plugin {
 
 // Unity editor doesn't unload dlls after 'preview'
 #if UNITY_EDITOR
     static class NativeMethods
     {
         [DllImport("kernel32.dll")]
         public static extern IntPtr LoadLibrary(string dllToLoad);
     
         [DllImport("kernel32.dll")]
         public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
     
         [DllImport("kernel32.dll")]
         public static extern bool FreeLibrary(IntPtr hModule);
     }
     
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     private delegate IntPtr Delegate_New();
     
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     private delegate int Delegate_GetSize(IntPtr plugin);
                                               
     // Make sure that delegate instances are identical to the actual method being called
     Delegate_New plugin_new;
     Delegate_GetSize plugin_getsize;

     IntPtr? plugin_dll = null;
 
     void InitializeHooks(){
         plugin_dll = NativeMethods.LoadLibrary(@"..\Release\Plugin.DLL");
         
         IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(plugin_dll.Value, "plugin_new");
         plugin_new = (Delegate_New)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,typeof(Delegate_New));
         
         pAddressOfFunctionToCall = NativeMethods.GetProcAddress(plugin_dll.Value, "plugin_getsize");
         plugin_getsize = (Delegate_GetSize)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,typeof(Delegate_GetSize));

         plugin_handle_ = plugin_new();
     }
     void CloseHooks(){
         if(plugin_dll == null){
             return;
         }
         bool result = NativeMethods.FreeLibrary(plugin_dll.Value);
         Console.WriteLine(result);
         
         plugin_dll = null;
     }
 #elif UNITY_ANDROID
     [DllImport("unityplugin")]
     public static IntPtr plugin_new();
 
     [DllImport("unityplugin")]
     public static int plugin_getsize(IntPtr plugin);
 
     void InitializeHooks(){plugin_handle_ = plugin_new();}
     void CloseHooks(){}
 #else
     [DllImport("unityplugin.dll")]
     public static IntPtr plugin_new();
 
     [DllImport("unityplugin.dll")]
     public static int plugin_getsize(IntPtr plugin);
 
     void InitializeHooks(){plugin_handle_ = plugin_new();}
     void CloseHooks(){}
 #endif
 IntPtr plugin_handle_;
 
 public Plugin(){
     Close();
 }
 
 /// <summary>
 /// Close this instance.
 /// </summary>
 public void Close(){
 
     CloseHooks();
 }
 
 /// <summary>
 /// Initialize this instance.
 /// </summary>
 public void Initialize(){
     InitializeHooks();    
 }

 /// <summary>
 /// Get size from dll.  Right now it'll be 42
 /// </summary>
 public int Size { 
     get {
         return plugin_getsize(plugin_handle_);
     }
 }
 }

Comment
Add comment · Show 6
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 AshMcConnell · Jun 05, 2013 at 08:20 PM 0
Share

Did you ever find a simpler way? I am about to do quite a bit of native dll coding and would rather not restart Unity every compile :). I will try your cunning pointer way first! I tried: - http://stackoverflow.com/questions/2445536/unload-a-dll-loaded-using-dllimport (2nd answer), but the $$anonymous$$odules list was empty for some reason.

avatar image Jerdak · Jun 05, 2013 at 09:05 PM 0
Share

:( I did not. I eased some of the burden by writing pre-processing code that would parse my dll headers for C declarations and spit out the boiler plate code above.

I saw on the forums that someone made a nice tutorial for working in Visual Studio outside of Unity. (Not just using Visual Studio to edit Unity scripts) $$anonymous$$ight save you some time?

avatar image AshMcConnell · Jun 05, 2013 at 09:07 PM 0
Share

Thanks for the reply. If it's possible, would you $$anonymous$$d sharing the pre-processing code? No problem if you would rather not, but it might save me some time

avatar image Jerdak · Jun 06, 2013 at 06:16 AM 0
Share

I can't find my code for this project. :( But I did find an unfinished post I started about using abstract syntax trees to extract things like "extern "C" void foo()" from code. Which is really all my original scripts did. Scrape for extern, copy the method signature, and spit out the boilerplate above.

I polished the post off and threw it up on my site. I'm not sure if you'll find it useful.

avatar image AshMcConnell · Jun 06, 2013 at 08:38 AM 0
Share

Thanks Jerdak, haven't had a play with Clang or Python before, so it might be hard to get going, but looks pretty great. I was thinking of just doing some $$anonymous$$d-twisting regular expressions, I'll see what can be done :). Thanks for taking the time!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Native DLL in Unity editor? 1 Answer

BCE0044 erro only shows when trying to build .apk 0 Answers

Can Editor scripts be in external DLL's or assemblies? 1 Answer

DLL works in editor, but gives an exception in Android 0 Answers

Exclude scripts from ios vs android builds? 1 Answer


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