Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't return an array from a function in the way you are probably thinking but you can return a pointer to the array. You will also need to return the length of the array so that you can properly marshal the data to the appropriate C# type. This requires a slight change to the function signature to instead return the information through parameters passed to the function.</p> <pre><code>extern "C" EXPORT_API bool GetCharacterPositions(CharacterInformation** data, int *length) { CharacterInformation *characterInformationArray = new CharacterInformation[arraysize]; // initialize contents of array. // ... snip ... *length = arraysize; *data = characterInformationArray; return true; } </code></pre> <p>In this case you will need to add a default constructor to <code>CharacterInformation</code> and use two-phase initialization (i.e. an <code>Init()</code> function).</p> <p>Be aware that you should not return a pointer <em>or</em> reference to a non-static local variable as the contents of the variable will be destroyed when it goes out of scope (i.e. when the function returns).</p> <p>To marshal the data you can try something like the following. This is untested but should give you a push in the right direction.</p> <pre><code>[DllImport("Dllname.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool GetCharacterPositions(out IntPtr arrayPtr, out int size); public static List&lt;CharacterInformation&gt; GetCharacterPositions() { var arrayValue = IntPtr.Zero; var size = 0; var list = new List&lt;CharacterInformation&gt;(); if ( !GetCharacterPositions(out arrayValue, out size)) { return list; } var dataEntrySize = Marshal.SizeOf(typeof(CharacterInformation)); for ( var i = 0; i &lt; size; i++) { var cur = (CharacterInformation )Marshal.PtrToStructure(arrayValue, typeof(CharacterInformation )); list.Add(cur); arrayValue = new IntPtr(arrayValue.ToInt32() + dataEntrySize); } return list; } </code></pre> <p>You will need to add an additional call in order to properly delete the data in C++ otherwise you will end up with a memory leak.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload