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
0
Question by ASTiDev · Nov 15, 2013 at 02:26 AM · c#nativedllimportwindows registry

C# DLL calling functions from native DLL

Hello.

I've developed a C# dll (let's call it WrapperClass) which exports a static class to wrap some functions from a native dll (let's call it NativeClass) using the DllImport attribute.

So WrapperClass will have something like the following:

      namespace Wrapper
      {
         public static class WrapperClass
         {
            [DllImport("NativeClass.dll", CallingConvention = CallingConvention.Cdecl]
            extern static int NativeFunction ();        
 
            public static int WrapperFunction()
            {
                return NativeFunction(); 
            }
         }
      }

Everything works fine but I'd like to be able to programmatically find the NativeClass dll rather than keeping it in the same runtime directory as the game. It is a dll which will be installed and maintained by an external process, so the path to it will depend on where the user installs it.

To do this I turned to the registry, since the external installer writes to it. However it seems calls to Microsoft.Win32.Registry.GetValue only ever return null from within Unity, regardless of if it's inside of a Mono script or in my C# dll.

I tried to work around this, by creating a second native dll (let's call it RegGetter) for the express purpose of retrieving registry values, loading RegGetter itself from within WrapperClass using a hardcoded position in the Assets directory. WrapperClass has a static constructor, and in this constructor I save the path RegGetter produces to a class level variable before calling AddDllDirectory to include the path in DllImport's search path.

So now WrapperClass looks like this:

     namespace Wrapper
     {
        public static class WrapperClass
        {
            [DllImport("/Assets/Product/DLLs/RegGetter.dll", CallingConvention = CallingConvention.Cdecl]
            extern unsafe static sbyte* GetPathFromReg();
 
            public static String SearchPath = "";
 
            static WrapperClass()
            {
                unsafe 
                {
                   String SearchPath = new String(GetPathFromReg());
                   AddDllDirectory(SearchPath);   
                }
            }
 
            [DllImport("NativeClass.dll", CallingConvention = CallingConvention.Cdecl]
            extern static int NativeFunction ();        
 
            public static int WrapperFunction()
            {
                return NativeFunction(); 
            }
         }
      }

So far, so good. I have a standalone tester C# program which has RegGetter successfully finding the path and WrapperClass successfully importing NativeClass and calling functions on it. However, in Unity I'm experiencing the following curious behavior:

Unity will display a DllNotFound exception looking for NativeClass in the log and crash gracelessly on trying to access it UNLESS I access some data from the WrapperClass before trying to access NativeClass. Which data it is is inconsequential. I can put the exact same code inside of the WrapperClass function (e.g. int tmp = SomeClassLevelInterger), to no effect - Unity needs to access it.

In other words, this works:

       using UnityEngine;
       using Wrapper;
 
       public class WrapperConsumer: MonoBehaviour
       {
          string tmp = Wrapper.SearchPath;
          int tmp1 = Wrapper.WrapperFunction();
       }

This doesnt:

        using UnityEngine;
        using Wrapper;
     
        public class WrapperConsumer: MonoBehaviour
        {
           int tmp1 = Wrapper.WrapperFunction();
        }

  

I've been banging my head against this for two days straight and am at a loss at how to proceed. Am I running into some kind of static constructor nonsense? Is there a freaking easier way to get into the Registry from within Unity?

Two options that are not available are 1) leaving the pointless statements in and 2) upgrading to pro to forgo the wrapper altogether - we're trying to build something to put on the Asset store that free users can use (and understand).

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
0

Answer by Threap · Jan 07, 2014 at 04:43 PM

Hi,

I am new to Unity and Mono, but not new to dot net.

This might help, you can access the registry from .net no need to use a native function to do it.

http://msdn.microsoft.com/en-us/library/z9f66s0a(v=vs.110).aspx.

I have no clue if everything in .net works in mono as I said I am new to mono + unity.

Hope that helps

Comment
Add comment · Show 1 · 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 ASTiDev · Jan 08, 2014 at 03:31 AM 0
Share

Hello,

Thanks for your help. Previously I had been using Registry.GetValue which, as I mentioned in the post, unequivocally returns null inside of Unity. It's interesting (read: $$anonymous$$dbogglingly inexplicable) that Registry$$anonymous$$ey.OpenSub$$anonymous$$ey works given the same values.

$$anonymous$$g. I had been using

 Registry.GetValue(@"H$$anonymous$$EY_LOCAL_$$anonymous$$ACHINE\SOFTWARE\Wow6432Node\$$anonymous$$yDirectory", "$$anonymous$$y$$anonymous$$ey", null)
 
 
 


which returns null.

However,

 Registry.Local$$anonymous$$achine.OpenSub$$anonymous$$ey(@"SOFTWARE\Wow6432Node\$$anonymous$$yDirectory").GetValue("$$anonymous$$y$$anonymous$$ey"));
 
 
 


works just fine. It seems like this is a bug, or at least something that should be documented clearly within Unity somewhere.

Regardless, I can be a little more concrete about what the problem is, (which still persists, btw), namely that Unity is not calling my static initializer function before it tries to call DLLImport.

Hopefully that will be easier to search for and for other people to volunteer help on.

Thanks again.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException in a wrapper 0 Answers

objective-c method call from unity 1 Answer

Renderer on object disabled after level reload 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