Search Unity

Passing a struct from c# to c++ on Android

Discussion in 'Android' started by garrilla, Jul 15, 2019.

  1. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    I'm trying to pass a struct into a method of a native library (this library works in windows DLL)

    the values of the struct are set before the method is called

    but when the initial method in the C, the struct's value are nil or zero - as though it were passed an unpopulated struct

    `AudioSourceMetaData` is the struct and is the same definition in both c# c/c++
    `meta` is populated on the c# but unpopulated in c/c++

    Code (C/C++):
    1.  
    2. // unitybridge.h - just has the headers
    3. ...
    4. int addFileToPlaylist(AudioSourceMetaData* meta);
    5. ...
    6. extern "C" {
    7.     int addFileToPlaylist(UnityBridge* _unitybridge, AudioSourceMetaData* meta);
    8. }...

    Code (C/C++):
    1.  
    2. // unitybridge.cpp -
    3.  
    4. int UnityBridge::addFileToPlaylist(AudioSourceMetaData* meta) {
    5.  
    6.    MetaDataAudioFile* audioFile = new MetaDataAudioFile;
    7.    audioFile->setValue(*meta);
    8.    playlist.add(audioFile);
    9.    int idx = playlist.indexOf(audioFile);
    10.    createAudioAndMetaData(audioFile, idx);
    11.    return idx;
    12. }
    13.  
    14.    int addFileToPlaylist(UnityBridge* _unitybridge, AudioSourceMetaData* meta)
    15.     {
    16.         if (_unitybridge == nullptr)
    17.         {
    18.             return K_ERR_INT;
    19.         }
    20.         return _unitybridge->addFileToPlaylist(meta);
    21.     }
    any thoughts? am I missing something here?
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Not sure, but if you Google for "Passing a struct from c# to c++" you find several articles that mention properly marshaling the data types, perhaps you've already seen these.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    It would help if showed the struct itself and C# code calling it.
    The only thing I can tell now is that your C++ function takes a pointer to struct, so in C# code it must be a ref parameter.
     
  4. garrilla

    garrilla

    Joined:
    Jan 12, 2016
    Posts:
    28
    sorry for the delay in responding, @JeffDUnity3D and @Aurimas-Cernius - it was a bit of a marshalling issue on a boolean and I needed to use the `in` keyword, you both put me on the right track so thanks very much