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
1
Question by dustinbahr · Oct 30, 2014 at 10:45 PM · c#iospluginc++

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

I need to pass a struct from C++ to C#, and I'm not sure where to begin.

I am more familiar with Unity / C# than Xcode / c++.

I have found some answers that mention "Marshaling", but the info is over my head, and I can't seem to find a simple example.

In C++ I have this struct:

 extern "C"
         {
             typedef struct StructName {
                 void* valueA;
                 int valueB;
                 int valueC;
                 float valueD[4];
                 int valueE;
                 int valueF;
                 int valueG;
                 uint64_t valueH;
                 float valueI[6];
             } StructName;
         }
 
 extern "C" StructName GetStruct() {
     return structVariable;
 }

From C# I would like to call the above GetStruct method and be able to access the properties of it. How would I do this?

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
3

Answer by Baroque · Jan 05, 2017 at 08:40 PM

There's a detailed and fairly readable reference in MSDN here: https://msdn.microsoft.com/en-us/library/eshywdt7(v=vs.110).aspx

"Marshalling" simply means converting data from native code (i.e. raw memory) to managed code (i.e. Mono, in Unity's case), or vice-versa.

If your structure contains simple types (floats, integers, etc.) then it's often as simple as creating the same structure in C# and adding [StructLayout(LayoutKind.Sequential)] above it. Surprisingly enough this is even smart enough to automatically marshal strings from a null-terminated char * to a c# string.

For float valueI[6] in your example you need to tell C#/Mono how that array is represented in memory using [MarshalAs(UnmanagedType.R4, SizeConst=6] above that element.

Where this gets more complicated is with references to other classes, or especially void *. You can represent pointers in C# using the type System.IntPtr and safely pass them around but you can't directly do anything with them. You will have to do your own reading if you need to pass complex types around.

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 · Jan 05, 2017 at 11:16 PM 0
Share

You pretty much covered everything one could say about that topic, well done.

+1

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

28 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

Related Questions

C++ windows DLL and Android? 1 Answer

Issue with glDrawArrays in plugin 2 Answers

Bug with EtceteraGUIManager.cs . 1 Answer

Should I go with C# and Unity or C++ and Unreal/Some other engine 0 Answers

C++ Plugin for mipmapping and JPG encoding: viability 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