简体   繁体   中英

Marshal.PtrToStructure on Android return null

I am working on a game project using unity3d.
Now I'm trying pass Structure from c++ to C# in unity.
Structure in c++ like below:

#pragma pack(push)
#pragma pack(1)
class BuildData  
{  
public:
  int m_ID;   
  int m_BuildType;   
  int m_NameID;  
  int m_DescriptionID;  
  int m_BuildLV; 
  int m_Group;    
  unsigned char m_OpenFlag;   
  unsigned char m_ShowFlag;  
  float m_UIDisplaySize;    
};
#pragma pack(pop)

In unity3d c# project, I declare the structure like that.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct BuildData
{
  public int m_ID;                  
  public int m_BuildType;           
  public int m_NameID;              
  public int m_DescriptionID;       
  public int m_BuildLV;             
  public int m_Group;         
  [MarshalAs(UnmanagedType.U1)]     
  public bool m_OpenFlag;    
  [MarshalAs(UnmanagedType.U1)]         
  public bool m_ShowFlag; 
  [MarshalAs(UnmanagedType.R4)]  
  public float m_UIDisplaySize;     
};

And I using the code below to get data.

System.IntPtr ptr = MethodToGetIntPtr();   
BuildData convert_data = (BuildData)Marshal.PtrToStructure(ptr, typeof(BuildData));

It works good on pc and android x86, but on android armeabi, it fails.
I get an IntPtr that not equal IntPtr.zero but Marshal.PtrToStructure will return null.

I have no idea why have this result.
Have someone can answer it?

Equivalent of unsigned char in C# is System.Byte .
Changing you datatype in C# will solve your problem.
You can check this link for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM