• 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 unity_j02W_-izRBhJLQ · Jul 06 at 12:07 PM · stringdllc++dllimportunicode

Converting from c# string to c++ string

I'm using a Dll created from a C++ file. When I either put the .dll and .lib files in my Unity-project folder or when I use the function that I need, Unity crashes and I can't open the project untile I remove the .dll or delete the function from the c# script.

This function works well on C++, both in Visual Studio and in Dev-C++

PS: Assets/alzBraccioCorretto.json is the file that I need to read

I've tried the same procedure for more simple dlls (just functions with int or double variables) and it worked fine, so I don't know what I'm missing with this one, probably UNICODE

In the Unity script I wrote

 [DllImport("QuartaLibreria.dll", CharSet = CharSet.Unicode)]

 static extern string LeggiFile(string nomeFile);

 Text testo;

 unsafe void Start()
 {
 testo = GetComponent<Text>();

 temp = LeggiFile("Assets/alzBraccioCorretto.json");
 testo.text = temp;
 }

In the header of the library I have

 #define QUARTALIBRERIA_API __declspec(dllexport)

 //all the other headers and #include

 string leggiFile(char* nomeFile);

 extern "C" {
 QUARTALIBRERIA_API wstring LeggiFile(wstring nomeFile);}

In the cpp of the library I have

 string leggiFile(string nomeFile) {

 ifstream _stream(nomeFile.c_str());
 string temp;
 _stream >> temp >> temp;
 _stream.close();
 return temp;
 }


 QUARTALIBRERIA_API wstring LeggiFile(wstring nomeFile){
 std::string str(nomeFile.begin(), nomeFile.end());
 string daRit = leggiFile(str);
 std::wstring WdaRit(daRit.length(), L' '); // Make room for characters

 std::copy(daRit.begin(), daRit.end(), WdaRit.begin());

 return WdaRit;
 }
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
Best Answer

Answer by Bunny83 · Jul 06 at 12:44 PM

You can not return a string from a native code method. Inside .NET managed land memory has to be managed memory. Have a look at this SO question. If you want to return string data to C# you should reverse the ownership. So you have to allocate the buffer memory on the managed side and pass a pointer to the native method which can fill the the buffer.


Though your code looks a bit strange. Why would you pass a file name to a native code plugin to read the content and then just return that content back to native code?

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 unity_j02W_-izRBhJLQ · Jul 07 at 08:47 AM 0
Share

The code was just a sample for a more complex function. No need to return a string, but I need to give it as an input because I need to read a file with ifstream using the dll. Thank you for your help! I'll try my best and I'll let you know!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

114 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 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

Chain of dependency dll's not loading correctly. 1 Answer

Failed to load DLL with error "The specified module cannot be found." 0 Answers

unity3d import c++ dll for pass by reference method 0 Answers

What is the correct location for a native (C++) plugin's DLL dependencies 1 Answer

What could cause C++ dll muti-threading to perform worse in Build mode 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges