Search Unity

Passing back void * pointer from native plugin to Unity

Discussion in 'Android' started by bleu, Jul 29, 2016.

  1. bleu

    bleu

    Joined:
    Apr 6, 2013
    Posts:
    41
    I was wondering if there was an easy way to pass a void * data type back to Unity without converting it to an invalid data type. A void * pointer can represent a Java ByteBuffer's direct address (obtained via GetDirectBufferAddress), and right now I convert the pointer to an int before passing it back to Unity (this works because I am running 32-bit code; 64-bit would require a long). Here's an example to make things more clear:

    C++ example function in native plugin code:
    extern "C" int getBufferAddress(){
    return (int)bufferAddress; // buffer address is a void * pointer
    }

    C# side:
    [DllImport("theplugin")]
    public static extern int getBufferAddress();

    The int returned from getBufferAddress can be converted to a (byte *) variable in C# via a cast (in unsafe code).

    Anyway, I'm not happy with this approach but it seems work ok for now. Is there a more "correct" approach?
     
  2. bleu

    bleu

    Joined:
    Apr 6, 2013
    Posts:
    41
    I guess I could make the function return a byte pointer and make the C# side do the following:

    [DllImport("theplugin")]
    public static extern byte * getBufferAddress();

    I was wondering if that's the best way, though.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I think you should use IntPtr. It represents a platform-specific pointer or handle (meaning - its size is not predetermined as 32-bit).

    Your method declaration with [DllImport] should return an IntPtr. This can be used to interact with the returned pointer (are you trying to get a pointer to a ByteBuffer? what would you do with it in C# ?)
     
    bleu likes this.
  4. bleu

    bleu

    Joined:
    Apr 6, 2013
    Posts:
    41
    I can't specify why I want to get a pointer to a ByteBuffer besides the fact that I need access to a shared buffer between C# and an Android plugin (the original code is private/not shareable so I can only ask the question by coming up with similar but fake code). There is a byte buffer that is used in the plugin that the C# code needs to know about too.

    I do remember learning about IntPtr a while ago so I feel a bit silly for not remembering that it can be used here. Thank you for the suggestion -- I will definitely try it out. I guess char * (to byte buffer) in C++ will map easily to IntPtr in C#.
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    bleu likes this.
  6. bleu

    bleu

    Joined:
    Apr 6, 2013
    Posts:
    41
  7. FireFlyD

    FireFlyD

    Joined:
    Nov 16, 2017
    Posts:
    1
    Hello, this is what I am trying to do as well. I use to have a DLL that fetches an image, does some operation with Opencv and callbacks to Unity. I am trying to convert this into an android plugin but for some reason declaring a stdcall function pointer in the android dynamic library is giving me a syntax error. I thought about passing the pointer of the image back to Unity as a return value but wouldn't the values stored in the buffer gets replaced/destroyed when the C++ function returns?