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 AliAzin · Sep 21, 2010 at 08:15 AM · pluginstringdll

How to pass string to a unity plugin?

Hi,

I have a problem passing strings to dll. I wrote this simple dll in visual studio 2008 for unity :

#if _MSC_VER #define EXPORT_API __declspec(dllexport) #endif

include <string.h>

include <iostream> #include <string> using namespace std;

int EXPORT_API main( string st) {

 if(st.string::compare("hello") == 0) return 1 ;

else return 0 ;

}

but when I want to call it through unity, I have two problems : 1. It returns 0 regardless of what string I pass into it. 2. At second call it crashes.

and this is my unity code:

using UnityEngine; using System.Runtime.InteropServices; class SomeScript : MonoBehaviour { string st = "";

[DllImport ("UnityTexture2")] public static extern int main(string st); void OnGUI(){ GUILayout.Label(st); if (GUILayout.Button("refresh")) print(main ("hello")); }

}

Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by skovacs1 · Sep 21, 2010 at 03:21 PM

This is not a Unity-specific concern, but rather more general to mono and system interop. You're having trouble with marshaling between managed and unmanaged code. See the Unity docs on plugins and more precisely the linked mono documentation in that page.

You should beware of function name mangling because the interop was written assuming a C ABI. If mono can't find your function in the dll, it's not going to work. To resolve this, either wrap your C++ code in an extern C {} and only use standard calling conventions or ensure you get the mangled name from the dll with one of your compiler's binary tools and set the DllImport.EntryPoint. Note that mangling of names is very compiler specific. Please read this section of mono's documentation on invoking unmanaged code.

The data structure of a .net language string is a fair bit different from a C++ STL string. The way they are marshaled can be handled a number of ways, but essentially it's going to marshal as some form of char array, be it a LPCSTR or wchar* or something like that, not as an STL string (since the interop is assuming a C ABI and that didn't provide for the STL string). Please read this section of the same mono documentation on marshaling strings.

As for why you're crashing, odds are it has to do with some of the above, but I can't be sure.

Also, you don't need to include iostream or string.h if you aren't using them.

Comment
Add comment · 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
4

Answer by pushxtonotdie · May 08, 2012 at 02:09 AM

I looked through the Mono docs and the plugin docs and didn't find a good answer for this, and wanted to give some more info. I found there are a few caveats to passing a string to a C plugin.

  • Make sure your Extern "C" is set correctly to prevent the name mangling.

  • The data type passed in is a char* on OS X.

  • You must copy this data using a function such as the MakeStringCopy below

Here is an example function in C:

 char* MakeStringCopy (const char* string) {
   if (string == NULL) return NULL;
   char* res = (char*)malloc(strlen(string) + 1);
   strcpy(res, string);
   return res;
 }
 static char* dialogTitle;
 
 void setDialogTitle(char* title){
     dialogTitle = MakeStringCopy(title);
 }


And corresponding MonoBehavior code:

 [DllImport ("libraryName")]
 private static extern void setDialogTitle(string title);

 void Start()
 {
     setDialogTitle("test title");        
 }
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 Bunny83 · May 08, 2012 at 02:14 AM 0
Share

$$anonymous$$ost of your answer has already been mentioned by @skovacs1's answer, however without samples ;)

Anyway, did realise this question was posted on Sep. 2010 (1.5 years ago)?

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

1 Person is following this question.

avatar image

Related Questions

Custom DLL - MissingMethodException: string[] string.Split 3 Answers

Cant link PLUGIN.DLL with PLUGIN-EDITOR.DLL 1 Answer

Unity plugins 0 Answers

Referencing a DLL in C# 0 Answers

How can I access frame data using C# from the NatNetML.dll 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