Search Unity

Native plugin: How to pass bool[] from C# to plugin in C?

Discussion in 'Editor & General Support' started by MicroEyes, Nov 14, 2017.

  1. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Hi All,
    I am writing a plugin in which i have to pass a bool[] from C# to bool* in C. But for some reason native isn't getting data correctly.

    If i pass this from C#:
    { true, false, true, true, false };​
    then native C gets this
    { true, false, false, false, false };​

    My other data types(int, float, double, long arrays) are working fine. Here is my code:
    C# (managed):
    Declaration
    Code (csharp):
    1. [DllImport("dll_name_here")]
    2. private extern static void Internal_AddBooleanArray(bool[] a_boolArr, int a_arrSize);
    Sending data:
    Code (csharp):
    1. bool[] l_arrBool = { true, false, true, true, false };
    2. Internal_AddBooleanArray(l_arrBool, 5);

    C (unmanaged):
    Code (csharp):
    1. void DLL_EXPORT Internal_AddBooleanArray(bool* a_boolArray, int a_lengthOfArray)
    2. {
    3.         for (size_t l_index = 0; l_index < a_lengthOfArray; l_index++)
    4.         {
    5.             printf("Main::Bool at index '%d' is '%d':'%s'", l_index, a_boolArray[l_index], (a_boolArray[l_index] ? "true" : "false"));
    6.         }
    7.  
    8. }
    From above printf(...) statement i am getting { true, false, false, false, false };
     
  2. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
  3. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    @nat42, I don't think that's the issue. bool in C and C# is of 1 byte.
     
  4. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    Might be somewhat enlightening regardless to cast the bools to int and see there values?
     
  5. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    @nat42 , From my original post, i am already printing values in int as well as in bool using
    Code (csharp):
    1. printf("Main::Bool at index '%d' is '%d':'%s'", l_index, a_boolArray[l_index], (a_boolArray[l_index] ? "true" : "false"));
    Here's my output.
    Main::Bool at index '0' is '1':'true'
    Main::Bool at index '1' is '0':'false'
    Main::Bool at index '2' is '0':'false'
    Main::Bool at index '3' is '0':'false'
    Main::Bool at index '4' is '0':'false'
     
  6. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Any idea guyz?
     
  7. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
  8. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Thanks @elias_t . I'll try approach from article tonight.