Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 unity_j02W_-izRBhJLQ · Jul 06, 2019 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, 2019 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, 2019 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.

Follow this Question

Answers Answers and Comments

112 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

Related Questions

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

Failed to load DLL with error "The specified module cannot be found." 1 Answer

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

Connect matlab and unity 0 Answers

How to step into a native C++ dll in Visual Studio? 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