Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 4.1.1 Bug: sending a string from a DLL back to Unity

Discussion in 'Editor & General Support' started by Gargi, Mar 22, 2013.

  1. Gargi

    Gargi

    Joined:
    Jun 6, 2012
    Posts:
    26
    Hi

    Sending a string from a native DLL back to Unity will crash the engine.
    Same bug in 4.0, 4.01, 4.1 and now also in 4.1.1.

    Now i sent the same bug report the third time.
    When i get a response, i will post the bug report number.

    Greetings

    Edit: bug report number (Case 533967)
     
    Last edited: Mar 22, 2013
  2. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Yes. Sorry to hear that. I've some project concerned.
     
  3. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    @Gargi I responded to your bug, but I'll post response here for others information.

    Marshalling of a string as a return value should never worked correctly. The marshalling code cannot know if it should free the string, and if so what deallocator it should use. Please mark the return type as an IntPtr and manually marshal the string as indicated at:

    http://www.mono-project.com/Interop_with_Native_Libraries#Strings_as_Return_Values

    So, something like:

    [DllImport("delphiDLL.dll")]
    private static extern IntPtr delphi_getString();

    ...

    {
    IntPtr pStr = delphi_getString();
    string str = Marshal.PtrToStringAnsi(pStr);
    // now pinvoke to free string if needed
    delphi_freeString(pStr);
     
    Chella90 likes this.
  4. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    This solved my issue. Many thanks. :cool:

    ...but, it worked properly before v4. :p
     
    Last edited: Mar 22, 2013
  5. Gargi

    Gargi

    Joined:
    Jun 6, 2012
    Posts:
    26
    @joncham: Thank you very much. It works now.
     
  6. bk0606

    bk0606

    Joined:
    Aug 21, 2014
    Posts:
    4