Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by SuperWang · Jul 09, 2016 at 07:39 AM · iospluginil2cppc++

IL2CPP C# passed ref float[] to C++ can't get right result

Hi, i was trying to write a C++ .a lib for iOS IL2CPP , and try to pass a ref float[] to a C++ func. In C++ proj, i init the float[]'s element, and want to get the value in C# code, but in C# script i get the float[] length is 1,not what i passed to C++.

eg:

C# Script :

     [DllImport ("__Internal")]
     private static extern bool TestRef(ref float[] array);
 
     void Start()
     {
         Debug.LogError ("Before TestRef");
         float[] a = new float[5];
         for(int i = 0; i < a.Length; i++) 
             i = -1;
         TestRef (ref a);
         Debug.LogError ("After TestRef : " + a.Length);
         for (int i = 0; i < a.Length; i++)
             Debug.LogError ("a[" + i + "] = " + a[i]);
 
     }

C++ Plugin:

 bool TestRef(float*& a)
 {
     cout << "[Plugin] Begin" << endl;
     for (Int i = 0; i < 5; i++)
    {
            a[i] = i;
    }
     cout << "[Plugin] After" << endl;
     return true;
 }

and after run this demo, i get

 After TestRef : 1
 
 a[0] = 0

after google i got this :

The il2cpp_codegen_marshal_array function simply returns a pointer to the existing managed array memory, that’s it!

http://blogs.unity3d.com/cn/2015/07/02/il2cpp-internals-pinvoke-wrappers/

and i want to know how to get the right result: a = {0,1,2,3,4}

and btw this code run everything ok at Mac, PC, iOS(not IL2CPP) so i believe it is something about IL2CPP

thanks all ~

Comment
Add comment · Show 1
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 fatih_k · Jul 12, 2021 at 08:03 AM 0
Share

Hi Mr. Wang, did you get any solution on this issue, I got the very same error in 2021

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoshPeterson · Jul 12, 2016 at 11:34 AM

To accomplish what the native code does in this case, you don't really need to pass the array by reference.

You could instead make the C# code look like this:

 [DllImport ("__Internal")]
 private static extern bool TestRef(float[] array);
     
 // Existing C# code to create the array an call the native code here.

Then the native code would look like this:

 bool TestRef(float[5] a)
 {
      cout << "[Plugin] Begin" << endl;
      for (Int i = 0; i < 5; i++)
      {
          a[i] = i;
      }
      cout << "[Plugin] After" << endl;
      return true;
 }

It is really only beneficial to pass an array from managed to native code by reference if you want to allocate new memory for the array in native code. That is certainly possible, but I would avoid it if you don't need it, as it does add some complexity.

If you do need to pass the array to native code by reference, you'll want to tell the runtime (IL2CPP, in this case) the size of the array using the SizeParamIndex marshaling directive. It looks like this:

 [DllImport ("__Internal")]
 private static extern bool TestRef([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] float[] array, int size);
         
 // Existing C# code to create the array an call the native code here.

This marshaling directive means that the argument at index 1 (`size` in this case) is an integer that indicates the size of the managed array. The runtime uses this information to determine how to properly marshal the array to native code.

Then your native code should look something like this:

 bool TestRef(float** a, int size)
 {
     cout << "[Plugin] Begin" << endl;
     for (Int i = 0; i < size; i++)
     {
         a[i] = i;
     }
     cout << "[Plugin] After" << endl;
     return true;
 }

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I18N.dll and I18N.CJK.dll don't work with IL2CPP 1 Answer

Can I code a native plugin for iOS in C/C++? 0 Answers

iOS Plugin - Pass a struct from C++ to C# 1 Answer

Getting a plugin working on iOS 1 Answer

Callbacks from C to C# are not working in 5.4.0f3 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